Sending Keystroke to another application using WinAPI

Vitim.us picture Vitim.us · Apr 6, 2013 · Viewed 22.2k times · Source

I have to control another application by sending keystrokes to it like CTRLS or CTRLSHIFTC or CTRLF.

I've tried a lot of things, but I can't get it working. So I'm trying to get this right on a simpler case.

This successfully sends Hey to the notepad:

procedure TForm1.Button1Click(Sender: TObject);
  var notepad, edit: HWND;
begin
  notepad := FindWindow('notepad', nil);
  edit := FindWindowEx(notepad, FindWindow('Edit', nil), nil, nil);

  SendMessage(edit, WM_CHAR, dword('H'), 0);
  SendMessage(edit, WM_CHAR, dword('e'), 0);
  SendMessage(edit, WM_CHAR, dword('y'), 0);
end;

And this successfully sends the F5 key to the notepad, and also works with F3 poping up the Find dialog.

notepad := FindWindow('notepad', nil);
PostMessage(notepad, WM_KEYDOWN, VK_F5, 0);
PostMessage(notepad, WM_KEYUP, VK_F5, 0);

But I don't know why using SendMessage doesn't work on the exemple above.

The best thing I could came up was something like this, which does nothing.

notepad := FindWindow('notepad', nil);
PostMessage(notepad, WM_KEYDOWN, VK_CONTROL, 0);
PostMessage(notepad, WM_KEYDOWN, VkKeyScan('F'), 0);
PostMessage(notepad, WM_KEYUP, VkKeyScan('F'), 0);
PostMessage(notepad, WM_KEYUP, VK_CONTROL, 0);

I've found somewhere here a library which kinda emulate the VBScript send key functions, but just looking the code, it seems to just broadcast keys to the current application or all applications, since there's no Handle parameter.

Answer

Andreas Rejbrand picture Andreas Rejbrand · Apr 6, 2013

Warning: This method depends on implementation details, and should not be used if you need to guarantee the correctness of your program. (On the other hand, you are already on that path. For instance, IIRC, in Windows 95 there isn't even a Go to dialog.)

I opened notepad.exe in my favourite Resource Editor, and investigated the menu bar. I noticed that the ID of the Save menu item is 3. Hence, the following code executes the Save menu command in notepad:

var
  notepad: HWND;
begin
  notepad := FindWindow('notepad', nil);

  SendMessage(notepad, WM_COMMAND, 3, 0);

Similarly, Find is 21 in my version of notepad.exe. Go to is 24.

Update, according to comment: If you need to send Ctrl+Key, you can use SendInput:

var
  notepad: HWND;
  inputArray: array[0..3] of TInput;
begin
  notepad := FindWindow('notepad', nil);

  // TODO: Either exit if notepad isn't focused, or set focus to notepad

  FillChar(inputArray, length(inputArray) * sizeof(TInput), 0);

  inputArray[0].Itype := INPUT_KEYBOARD;
  inputArray[0].ki.wVk := VK_LCONTROL;
  inputArray[1].Itype := INPUT_KEYBOARD;
  inputArray[1].ki.wVk := VkKeyScan('S');
  inputArray[2].Itype := INPUT_KEYBOARD;
  inputArray[2].ki.wVk := VkKeyScan('S');
  inputArray[2].ki.dwFlags := KEYEVENTF_KEYUP;
  inputArray[3].Itype := INPUT_KEYBOARD;
  inputArray[3].ki.wVk := VK_LCONTROL;
  inputArray[3].ki.dwFlags := KEYEVENTF_KEYUP;

  SendInput(length(inputArray), inputArray[0], sizeof(TInput));