I need to control another application by sending him keystrokes, such as CTRL S or CTRL SHIFT C or CTRL F.
I tried a lot of things, but I can't get it to work. Therefore, I am trying to do it correctly in a simpler case.
This successfully sends Hey to 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 it successfully sends the F5 key to notepad, and also works with F3 , opening the search dialog.
notepad := FindWindow('notepad', nil); PostMessage(notepad, WM_KEYDOWN, VK_F5, 0); PostMessage(notepad, WM_KEYUP, VK_F5, 0);
But I do not know why using SendMessage does not work in the example above.
The best I could find is something like this that 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 found somewhere here a library that kind of emulates VBScript key transfer functions, but just looking at the code seems to just pass the keys to the current application or all applications, since there is no Handle parameter.