sending messages to windows
Posted on 2003-02-28
I'm trying to build an application, in which you can press some keys, and then, send the key to an application by typing in its window title. I have written an event handler attached to application.onmessage, which handles all keyup and keydown messages, it stores the given tMsg-Object in a global array, called Msgs. When clicking on the SendBtn, I send the messages in the right order TimesEdit.Value-times to the window with TitleEdit.Text in the titlebar.
Example: User presses a;b;c; and wants these key to be sent twice to a window called "untitled - notepad", the notepad editfield should then contain "abcabc".
here's the code:
procedure TMainForm.SendBtnClick(Sender: TObject);
var I: integer;
H, N, X: Cardinal;
H := GetHandleFromWindowTitle (TitleEdit.Text);
N := TimesEdit.Value;
X := 0;
// SetActiveWindow (H);
while (not Cancel) and (X < N)do begin
for I := 0 to high (Msgs) do begin
PostMessage (H, Msgs [I].message, Msgs [I].wParam, Msgs [I].lParam);
// DefWindowProc (H, Msgs [I].message, Msgs [I].wParam, Msgs [I].lParam);
// SendMessage (H, Msgs [I].message, Msgs [I].wParam, Msgs [I].lParam);
// SetActiveWindow (Handle);
I tried SendMessage, DefWindowProc and PostMessage, and none of them seem to work (no error, but also no effect). I have the right handle (using findwindow).
What could be wrong? None of the applications i tested this with seems to react to the messages. Do they get the messages at all?