To emulate some keys I have to send there scancodes, to do this I use SendInput() function from WinAPI and it's work fine in most cases, but how to use it for sending scancodes for such keys as "Pause"? This key generates 6 byte scancode (E1 1D 45 E1 9D C5), as scancode in KEYBDINPUT structer has WORD type, i.e. allows to use scancodes up to 2 bytes only. I tried 1D45 scancode and 9DC5 with KEYEVENTF_EXTENDEDKEY flag, suggesting that it apply E0\E1 code depending on content. I also tried to send sequences of six 1 byte commands. But all that doesn't work.
- Faking input is usually the wrong thing to do.David Heffernan– David Heffernan2014-10-09 19:31:05 +00:00Commented Oct 9, 2014 at 19:31
- It depends on task. I can agree with you in case if you develop own software, but in case when you need to make some tool that will emulate keyboard in other applications it'ss single way. I know there are some more easy ways for example SendMessage or PostMessage, but they send messages directly to window message handler and keyboards hooks don't recive this messages. Also application can use some others API like Direct input to work with keyboard. Any way it's work perfect, but I can't understand how to use it for keys with large scancodes like Pause key.StaticZ– StaticZ2014-10-10 10:22:15 +00:00Commented Oct 10, 2014 at 10:22
Add a comment |
1 Answer
I found the answer. You have to send two inputs into your array for SendInput. The first entry will be 0xE0, and the second-entry will be your scancode.
I actually found the solution here, embedded in the graphic: https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-6.0/aa299374(v=vs.60)
See code. It's C#, but you'll get the idea.
inputs = new[] { new WinAPI.INPUT { type = WinAPI.INPUT_KEYBOARD, u = new WinAPI.InputUnion { ki = new WinAPI.KEYBDINPUT() { wScan = (ushort) 0xe0, wVk = (ushort) 0, dwFlags = (ushort) dwFlags, dwExtraInfo = WinAPI.GetMessageExtraInfo() } } }, new WinAPI.INPUT { type = WinAPI.INPUT_KEYBOARD, u = new WinAPI.InputUnion { ki = new WinAPI.KEYBDINPUT() { wScan = (ushort) tscancode, wVk = (ushort) tvk, dwFlags = (ushort) dwFlags, dwExtraInfo = WinAPI.GetMessageExtraInfo() } } } };