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.