begin PostMessage(hWindow, WM_KEYDOWN, key, lParam); PostMessage(hWindow, WM_KEYUP, key, lParam or $C0000000); end; (* process the messages *) Application.ProcessMessages;
(* restore the old key state map *) SetKeyboardState(pKeyBuffers^[1]); finally (* free the memory for the key state buffers *) if pKeyBuffers <> nilthen Dispose(pKeyBuffers); end; { If } end; end; { PostKeyEx }
// Example:
procedure TForm1.Button1Click(Sender: TObject); var targetWnd: HWND; begin targetWnd := FindWindow(''''notepad'''', nil) if targetWnd <> 0 then begin PostKeyExHWND(targetWnd, Ord(''''I''''), [ssAlt], False); end; end;
{***********************************************************} {3. With SendInput API}
// Example: Send text procedure TForm1.Button1Click(Sender: TObject); const Str: string = ''''writing writing writing''''; var Inp: TInput; I: Integer; begin Edit1.SetFocus;
for I := 1 to Length(Str) do begin // press Inp.Itype := INPUT_KEYBOARD; Inp.ki.wVk := Ord(UpCase(Str[i])); Inp.ki.dwFlags := 0; SendInput(1, Inp, SizeOf(Inp));