如何以编程方式模拟按钮按下?

时间:2016-02-08 19:41:19

标签: c# .net

如何在.NET中模拟按下按钮?

我可以轻松地使用AutoHotKey:

#Persistent
SetTimer,PressTheKey,1000
Return

PressTheKey:
Send, {F5}
Return

任何应用程序都会知道按钮被按下了(就像我按下了按钮一样)。我尝试过这样做:

[DllImport("user32.dll", SetLastError = true)]
public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

或:

[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

或使用Windows输入模拟器(https://inputsimulator.codeplex.com/):

inputSimulator.Keyboard.KeyDown(VirtualKeyCode.VK_H)

他们都在记事本或其他一些应用程序中工作,但他们从未在我的全屏游戏中工作过。知道如何实现它吗?我想要一个在系统级别全局模拟点击的解决方案。

2 个答案:

答案 0 :(得分:1)

有一个名为Interceptor的lib是Windows键盘驱动程序的包装器,但是有一些限制 - 即不支持win8或更高版本 - 并且它的工作似乎停止了(11个月)以前)。如果它符合您的需求,请查看它。

我已经通过DirectInput API了解了有哪些方法可以做到这一点 - 至于这是否是我不确定的情况。我已经使用了Interceptor几次,当时它可以满足我的需求。

希望这有帮助

另外 Link

修改:固定链接

答案 1 :(得分:1)

我了解到我的应用程序正在使用DirectInput(感谢Gabe)。这让我更专注于SendInput,我设法发送了一个" A"像我这样的应用程序的关键:

// A = 30
keyboardDirectInput.Press(30);

所有关键代码均可在此处获取:https://msdn.microsoft.com/en-us/library/windows/desktop/bb321074%28v=vs.85%29.aspx

KeyboardDirectInput类:

public class KeyboardDirectInput
{
    [DllImport("user32.dll")]
    static extern UInt32 SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] INPUT[] pInputs, Int32 cbSize);

    public void Press(VirtualKeys key)
    {
        INPUT[] InputData = new INPUT[2];

        InputData[0].type = (int)InputType.INPUT_KEYBOARD;
        InputData[0].ki.wScan = (short)key;
        InputData[0].ki.dwFlags = (int)KEYEVENTF.SCANCODE;

        InputData[1].type = (int)InputType.INPUT_KEYBOARD;
        InputData[1].ki.wScan = (short)key;
        InputData[1].ki.dwFlags = (int)(KEYEVENTF.KEYUP | KEYEVENTF.SCANCODE);

        if (SendInput(2, InputData, Marshal.SizeOf(InputData[1])) == 0)
        {
            Logger.GetInstance().Warn("SendInput failed (CODE {0})", Marshal.GetLastWin32Error());
        }
    }

    [StructLayout(LayoutKind.Explicit)]
    public struct INPUT
    {
        [FieldOffset(4)]
        public HARDWAREINPUT hi;
        [FieldOffset(4)]
        public KEYBDINPUT ki;
        [FieldOffset(4)]
        public MOUSEINPUT mi;
        [FieldOffset(0)]
        public int type;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct HARDWAREINPUT
    {
        public int uMsg;
        public short wParamL;
        public short wParamH;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct MOUSEINPUT
    {
        public int dx;
        public int dy;
        public int mouseData;
        public int dwFlags;
        public int time;
        public IntPtr dwExtraInfo;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct KEYBDINPUT
    {
        public short wVk;
        public short wScan;
        public int dwFlags;
        public int time;
        public IntPtr dwExtraInfo;
    }

    [Flags]
    public enum KEYEVENTF
    {
        KEYDOWN = 0,
        EXTENDEDKEY = 0x0001,
        KEYUP = 0x0002,
        UNICODE = 0x0004,
        SCANCODE = 0x0008,
    }

    [Flags]
    public enum InputType
    {
        INPUT_MOUSE = 0,
        INPUT_KEYBOARD = 1,
        INPUT_HARDWARE = 2
    }
}