simulating key press event using SendInput with C#

末鹿安然 提交于 2019-12-04 04:28:50

问题


Following the advice from this thread: distinguish between keyboard's Real and Virtual key presses

I'm trying to create a program, that will send keyboard's key-press events using SendInput() method.

However, the problem is that when I try to simulate a key press event - nothing happens whatsoever. So far that's my code:

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

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

    [StructLayout(LayoutKind.Explicit)]
    struct INPUT
    {
        [FieldOffset(0)] public int type;
        [FieldOffset(8)]  public KEYBDINPUT ki; //x64 - 8, x32 - 4
    }

    const int KEYEVENTF_DOWN = 0; //key UP
    const int KEYEVENTF_EXTENDEDKEY = 0x0001;
    const int KEYEVENTF_KEYUP = 0x0002; //key UP
    const int KEYEVENTF_UNICODE = 0x0004;
    const int KEYEVENTF_SCANCODE = 0x0008; // scancode

    public void Send_Key(short Keycode)
    {
        INPUT[] InputData = new INPUT[1];
        InputData[0].type = 1;
        InputData[0].ki.wScan = Keycode;
        InputData[0].ki.dwFlags = KEYEVENTF_KEYUP | KEYEVENTF_SCANCODE;
        InputData[0].ki.time = 0;
        InputData[0].ki.dwExtraInfo = IntPtr.Zero;
        SendInput(1, InputData, Marshal.SizeOf(typeof(INPUT)));
    }

To decipher the scancodes I've downloaded the program that was suggested in this thread:

https://superuser.com/questions/293609/windows-7-tool-to-capture-keyboard-scan-codes

according to this program - "a" key's code for my keyboard is "65"

My program is supposed to trigger a key press event when textbox text is changed, so if we enter "q" - the text should change to "q" plus the result of send_key(), i.e. "qa":

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        Send_Key(0x0065); // nothing happens (0x65 also fails)
    }

what am I doing wrong? In the future I'll be changing this code so I can specify key-hold time (between DOWN and UP events). but for now, for testing purposes I'm simulating only keyUP event.

EDIT: Hans, according to your advice here is the edit - but it doesn't work, unfortunately.

 public void Send_Key(short Keycode)
        {
            INPUT[] InputData = new INPUT[1];
            InputData[0].type = 1;
            InputData[0].ki.wScan = Keycode;
            InputData[0].ki.dwFlags = KEYEVENTF_DOWN;
            InputData[0].ki.time = 0;
            InputData[0].ki.dwExtraInfo = IntPtr.Zero;

            uint intReturn = SendInput(1, InputData, Marshal.SizeOf(typeof(INPUT)));

            if (intReturn == 0) //!=1
            {
                throw new Exception("Could not send keyDOWN: " + Keycode);
            }

            INPUT[] InputData2 = new INPUT[1];
            InputData2[0].type = 1;
            InputData2[0].ki.wScan = Keycode;
            InputData2[0].mi.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP | KEYEVENTF_SCANCODE;
            InputData2[0].ki.time = 0;
            InputData2[0].ki.dwExtraInfo = IntPtr.Zero;

            uint intReturn2 = SendInput(1, InputData2, Marshal.SizeOf(typeof(INPUT)));

            if (intReturn2 == 0) //!=1
            {
                throw new Exception("Could not send keyUP: " + Keycode);
            }
        }

no errors.. and no output.


回答1:


but for now, for testing purposes I'm simulating only keyUP event

Which is okay I guess but that is not going to make Windows do anything. To generate a keystroke, you must first send a KEYDOWN. A "hold-time" doesn't have any useful effect so just put the two SendInput calls in the same method.

You should add error checking, winapi calls don't generate any exceptions. Throw a Win32Exception if the function returns 0. And fix the [DllImport] declaration with SetLastError = true.

You can avoid using that program you found at superuser.com by setting the ki.wVk field instead and omitting the KEYEVENTF_SCANCODE flag. So that you specify virtual keys instead of scan codes. The .NET Keys enumeration type gives you the values you need. Like Keys.A to generate a key press for the A key.



来源:https://stackoverflow.com/questions/10375264/simulating-key-press-event-using-sendinput-with-c-sharp

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