Pressing a Key in a game which will make my program click 'Start'

左心房为你撑大大i 提交于 2019-12-08 07:07:43

问题


I am currently doing a program which will Announce and Countdown from example lets say 5 minutes, So when you press Start button it will count down from 5 minutes to 0,

The thing that I would like to have is this:

If I am in a full-screen game, for example World of Warcraft , League of Legends, or any game with full screen graphics and I press Numpad 8, I want the program to click start on Button 1.

This should be possible since I've seen it before but I don't know how to do it.


回答1:


I tested it on League of Legends and Terraria. The following works:

public static class WindowsAPI
{
    public enum HookType : int
    {
        WH_JOURNALRECORD = 0,
        WH_JOURNALPLAYBACK = 1,
        WH_KEYBOARD = 2,
        WH_GETMESSAGE = 3,
        WH_CALLWNDPROC = 4,
        WH_CBT = 5,
        WH_SYSMSGFILTER = 6,
        WH_MOUSE = 7,
        WH_HARDWARE = 8,
        WH_DEBUG = 9,
        WH_SHELL = 10,
        WH_FOREGROUNDIDLE = 11,
        WH_CALLWNDPROCRET = 12,
        WH_KEYBOARD_LL = 13,
        WH_MOUSE_LL = 14
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct KeyboardHookStruct
    {
        public int VirtualKeyCode;
        public int ScanCode;
        public int Flags;
        public int Time;
        public int ExtraInfo;
    }

    public delegate IntPtr HookProc(int nCode, IntPtr wp, IntPtr lp);

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern IntPtr SetWindowsHookEx(HookType idHook, HookProc lpfn, IntPtr hInstance, uint threadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern bool UnhookWindowsHookEx(IntPtr hHook);

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern IntPtr CallNextHookEx(HookType idHook, int nCode, IntPtr wParam, IntPtr lParam);
}

Then to "hook" it up:

public partial class Form1 : Form
{
    private IntPtr kbhook = IntPtr.Zero;

    private void Form1_Load(object sender, EventArgs e)
    {
        kbhook = WindowsAPI.SetWindowsHookEx(WindowsAPI.HookType.WH_KEYBOARD_LL, HandleKeyPress, IntPtr.Zero, 0);

        if (kbhook == IntPtr.Zero)
            Application.Exit();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        WindowsAPI.UnhookWindowsHookEx(kbhook);
    }


    private IntPtr HandleKeyPress(int nCode, IntPtr wp, IntPtr lp)
    {
        WindowsAPI.KeyboardHookStruct MyKeyboardHookStruct =
            (WindowsAPI.KeyboardHookStruct)Marshal.PtrToStructure(lp, typeof(WindowsAPI.KeyboardHookStruct));

        var key = (Keys)MyKeyboardHookStruct.VirtualKeyCode;

        // **********************************
        // if the pressed key is Keys.NumPad8
        if (key == Keys.NumPad8)
        {
            button1_Click(null, EventArgs.Empty);
        }

        return WindowsAPI.CallNextHookEx(WindowsAPI.HookType.WH_KEYBOARD_LL, nCode, wp, lp);
    }
}

pinvoke.net may have outdated code samples, but it helps to know the method signatures.



来源:https://stackoverflow.com/questions/5626021/pressing-a-key-in-a-game-which-will-make-my-program-click-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!