Alternative to SendKeys when running over Remote Desktop?

Cameron picture Cameron · Jul 16, 2009 · Viewed 23.2k times · Source

I have an application which injects keystrokes into applications via SendKeys.

Unfortunately, the application will not work when I am running it via Remote Desktop because of the well known issue that SendKeys doesn't work with Remote Desktop.

Has anyone solved this issue before, or have any good suggestions on how to resolve it?

Answer

Mrchief picture Mrchief · Aug 26, 2011

SendKeys is not a good fit mainly due to:

  • It can only send keys to active/focused application, which is never guaranteed to work because the the active application can change between the time the keys are actually sent.
  • RDP and many other libraries (e.g., DirectX) block them mainly for security reasons.

Better alternatives:

Sample code using SendMessage:

HWND hwndNotepad = FindWindow(_T("Notepad"), NULL);
HWND hwndEdit = FindWindowEx(hwndNotepad, NULL, _T("Edit"), NULL);
SendMessage(hwndEdit, WM_SETTEXT, NULL, (LPARAM)_T("hello"));