Question

i'm trying to send fake keyboard input to an application that's running in a Remote Desktop session. i'm using:

Byte key = Ord("A");

keybd_event(key, 0, 0, 0); // key goes down
keybd_event(key, 0, KEYEVENTF_KEYUP, 0); // key goes up

Now this code does send the character "a" to any local window, but it will not send to the remote desktop window.

What that means is i use Remote Desktop to connect to a server, and i then open Notepad on that server. If i manually punch keys on the keyboard: they appear in Notepad's editor window. But keybd_event's fake keyboard input not causing "a"'s to appear in Notepad.

How can i programtically send fake keyboard input to an application running inside a remote desktop connection, from an application running on the local machine?


Nitpickers Corner

In this particular case i want to do this becase i'm trying to defeat an idle-timeout. But i could just as well be trying to

  • perform UI automation tests
  • UI stress tests
  • UI fault finding tests
  • UI unit tests
  • UI data input tests
  • UI paint tests
  • or UI resiliance tests.

In other words, my reasons for wanting it aren't important

Note: The timeout may be from remote desktop inactivity, or perhaps not. i don't know, and it doesn't affect my question.

Was it helpful?

Solution

Answer

Although Microsft says you don't need to, and you should not, send the OEM code, you need to send the OEM scan codes. In this example i need to send the OEM scan codes for

  • key A goes down
  • key A goes up

There is a picture of a chart on CodeProject that lists the make and break scan codes for various keys:

alt text

In my case the original calls to keybd_event need to be changed to:

Byte key = Ord("A");

keybd_event(key, 0x1E, 0, 0); // key goes down
keybd_event(key, 0x9E, KEYEVENTF_KEYUP, 0); // key goes up

i tested this, and it works. So all is well.

OTHER TIPS

May be you can execute an autoit script with PsExec, a light-weight telnet-replacement that lets you execute processes on other systems, complete with full interactivity for console applications, without having to manually install client software.

(AutoIt is quite capable to send any signal (keys or other) to any window application, and could be launched with PsExec on the remote desktop)

An AutoIt script like KillSaver, for instance, is designed to move the mouse to avoid any prolong idle time on a computer!

This worked very well thank you. In order to get the keyboard scan code one can use:

int scan;
scan = MapVirtualKey(key & 0xff, 0);
keybd_event(key, scan, 0, 0); // key goes down
keybd_event(key, scan | 0x80, KEYEVENTF_KEYUP, 0); // key goes up

You could use SendMessage(); It's really a much better simulator for keys. Well, good luck on this!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top