0

I have a C++ program that simulates a single key down key up every several minutes. The program is basically a console application and I run the program and it will press the given key to wherever the current focus is, not necessarily inside the application itself.

I do this by using SendInput.

The same program doesn't work when I RDP to a computer and run the program directly on the remote host. My search for an answer led me to concepts like scan codes and mapping keys, but I'm not entirely sure if these are specific to my issue.

My original code is below. How can I edit this to make it work when launching the program in an RDP session?

INPUT inputs[2] = {};
ZeroMemory(inputs, sizeof(inputs));
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wVk = VK_F22;
inputs[1].type = INPUT_KEYBOARD;
inputs[1].ki.wVk = VK_F22;
inputs[1].ki.dwFlags = KEYEVENTF_KEYUP;

UINT uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
Neptune
  • 11
  • 2

1 Answers1

2

Use scancodes, not virtual keys. Use the KEYEVENTF_SCANCODE flag with SendInput.

Scancodes get injected into a lower level into the stack and come up the stack mostly identical to actual keyboard events. While vkeys are queued differently and might be interpreted differently by the app or shell.

If you aren't sure what scan code to inject, you can use the MapVirtualKey function.

selbie
  • 100,020
  • 15
  • 103
  • 173
  • Looks like I've answered this question before: [here](https://stackoverflow.com/questions/49224390/c-sendinput-doesnt-manage-alt-codes-properly/49225608#49225608) and in a bunch of other places [here too](https://stackoverflow.com/questions/58787345/is-there-a-way-to-manually-send-messages-to-a-usb-keyboard-under-windows/58789038#58789038) – selbie Jan 06 '22 at 05:06
  • I need a key like F22, which simulates a press but doesn't really do anything. MapVirtualKey isn't mapping it. And I've searched, but can't find the scan code for it. Any suggestions? – Neptune Jan 07 '22 at 04:58