simulating key press event using SendInput with C#

后端 未结 1 428
星月不相逢
星月不相逢 2021-01-20 03:47

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 ke

相关标签:
1条回答
  • 2021-01-20 03:58

    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.

    0 讨论(0)
提交回复
热议问题