views:

1674

answers:

4

Found my own answer. If someone could copy/paste it into an answer so i can mark it as the answer, and answer this question, to take it off the unanswered list - i would appreciate it.


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.

A: 

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!

VonC
i only have Remote Desktop.
Ian Boyd
Could you not install Autoit on your main desktop, build the script into an exe and remotely launch that exe on the remote desktop ?
VonC
keybd_event, right... you mean "How can i problematically" **from my C++ program** send [...]. Not sure you can (see http://www.tech-archive.net/Archive/VC/microsoft.public.vc.language/2005-01/0846.html)
VonC
Does the language make any difference? And no, i didn't use C, C++, C#, or VB to make my program.
Ian Boyd
Good, I though you were limited by one language/techno. Then my answer stands.
VonC
i see what you're saying now, and you are right. i should have more specifically stated that i had a Win32 programming question.
Ian Boyd
+2  A: 
Ian Boyd
Rather than using the pre-fab table, you're better off using MapVirtualKey.
P Daddy
@Piddy: i didn't now such a an API existed, that's great! i had always wondered why the OEM scan tables were so hard to find. Probably because you can ask the OS for them.
Ian Boyd
A: 

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

Guus Geurkink
You'd need to provide more information than that. Which message, with what wparam and lparam?
Ian Boyd
...and what window?
Ian Boyd
use HWND window = FindWindow(classname, windowname);(You just need one of those; classname or windowname)SendMessage(window, wParam, lParam);(where wParam is the virual key (http://msdn.microsoft.com/en-us/library/aa926323.aspx) and lParam is just NULL)~Guus
Guus Geurkink
+1  A: 

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
selwyn