PDA

View Full Version : سوال: هوک کیبرد-چرا دکمه های فشرده شده دوبار دریافت می شوند؟



hp1361
شنبه 25 آبان 1392, 09:23 صبح
با سلام

من از کدهایی که Zarco توی سایتش گذاشته استفاده کردم تا هوک رو امتحان کنم. اما در فایلی که هوک کیبرد ذخیره میشه، دکمه های فشرده شده دوبار ذخیره میشه. فکر کنم یکیش برای فشرده شدن و دیگری برای رها شدنش باشه در حالی که توی شرط اینو لحاظ کرده.

مشکل کجاست؟

اینم کدی که توی DLL ذخیره میشه

ممنون

library HookDll;

uses
SysUtils,
Classes, windows;

var
CurrentHook: HHook;
KeyArray: array [0 .. 19] of byte;
KeyArrayPtr: integer;
CurFile: file of byte;

{
GlobalKeyboardHook
------------
This is the hook procedure to be loaded from hooks.exe when you
try and create a global hook. It is similar in structure to that defined
in hook.dpr for creating a local hook, but this time it does not beep!
Instead it stores each key pressed in an array of bytes (20 long). Whenever
this array gets full, it writes it to a file, log.txt and starts again.
}
function GlobalKeyBoardHook(code: integer; wParam: word; lParam: longword): longword; stdcall;
var
AStreamWriter: TStreamWriter;
begin
if code < 0 then
begin // if code is <0 your keyboard hook should always run CallNextHookEx instantly and
GlobalKeyBoardHook := CallNextHookEx(CurrentHook, code, wParam, lParam); // then return the value from it.
Exit;
end;
// firstly, is the key being pressed, and is it between A and Z
// note that wParam contains the scan code of the key (which for a-z is the same as the ascii value)
if ((lParam and KF_UP) = 0) and (wParam >= 65) and (wParam <= 90) then
begin
// store the keycode in the list of keys pressed and increase the pointer
KeyArray[KeyArrayPtr] := wParam;
KeyArrayPtr := KeyArrayPtr + 1;
// if 20 keys have been recorded, save them to log.txt and start again
if KeyArrayPtr > 19 then
begin
assignfile(CurFile, 'C:\log.txt');
if fileexists('C:\log.txt') = false then
rewrite(CurFile)
else
reset(CurFile); // if log.txt exists, add to it, otherwise recreate it
blockwrite(CurFile, KeyArray[0], 20);
closefile(CurFile);
KeyArrayPtr := 0;
end;
end;
CallNextHookEx(CurrentHook, code, wParam, lParam); // call the next hook proc if there is one
GlobalKeyBoardHook := 0; // if GlobalKeyBoardHook returns a non-zero value, the window that should get
// the keyboard message doesnt get it.
Exit;
end;

{
SetHookHandle
-------------
This procedure is called by hooks.exe simply to 'inform' the dll of
the handle generated when creating the hook. This is required
if the hook procedure is to call CallNextHookEx. It also resets the
position in the key list to 0.

}
procedure SetHookHandle(HookHandle: HHook); stdcall;
begin
CurrentHook := HookHandle;
KeyArrayPtr := 0;
end;

exports GlobalKeyBoardHook index 1,
SetHookHandle index 2;

begin

end.