Послать нажатие клавиш
Автор: Xavier Pacheco
unit Main;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, Menus;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
MainMenu1: TMainMenu;
File1: TMenuItem;
Open1: TMenuItem;
Exit1: TMenuItem;
Button4: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Open1Click(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
uses SendKey, KeyDefs;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.SetFocus; // focus Edit1
SendKeys('^{DELETE}I love...'); // send keys to Edit1
WaitForHook; // let keys playback
Perform(WM_NEXTDLGCTL, 0, 0); // move to Edit2
SendKeys('~delphi ~developer''s ~guide!'); // send keys to Edit2
end;
procedure TForm1.Button2Click(Sender: TObject);
var
H: hWnd;
PI: TProcessInformation;
SI: TStartupInfo;
begin
FillChar(SI, SizeOf(SI), 0);
SI.cb := SizeOf(SI);
{ Invoke notepad }
if CreateProcess(nil, 'notepad', nil, nil, False, 0, nil, nil, SI,
PI) then
begin
{ wait until notepad is ready to receive keystrokes }
WaitForInputIdle(PI.hProcess, INFINITE);
{ find new notepad window }
H := FindWindow('Notepad', 'Untitled - Notepad');
if SetForegroundWindow(H) then // bring it to front
SendKeys('Hello from the Delphi Developer''s Guide SendKeys ' +
'example!{ENTER}'); // send keys!
end
else
MessageDlg(Format('Failed to invoke Notepad. Error code %d',
[GetLastError]), mtError, [mbOk], 0);
end;
procedure TForm1.Open1Click(Sender: TObject);
begin
ShowMessage('Open');
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
WaitForInputIdle(GetCurrentProcess, INFINITE);
SendKeys('@fx');
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
WaitForHook;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
WaitForInputIdle(GetCurrentProcess, INFINITE);
SendKeys('@fo');
end;
end.
Скачать пример
|