unit HookProc;
{CASE 语句中的值按键盘的矩阵排列:
第一排从左到右;第二排从左到右;第三排从左到右;第四排从
左到右;左面从上到下;右面从上到下,。。。把CASE 值换成
要处理的键盘的对应键值。本程序适合的键盘排列为:“123上
移”;“456修改”;“789下移”;“取消 0 。确认”;“左
面从上到下四个键”;“右面从上到下四个键”。}
interface
uses
IniFiles,Windows, SysUtils;
const
afilename='c:/key.txt';
var
HNextHookProc:hhook;
ProcSaveExit
ointer;
{定义键盘的键值变量}
type
PKey = ^TKey;
TKey = packed record
KeyEnter:Longint;
KeyEscape:Longint;
KeyClear:Longint;
KeyUp:Longint;
KeyDown:Longint;
end;
var
MapHandle: THandle;
Keys: PKey;
function KeyBoardHook(iCode:Integer;wparam:WPARAM;lparam:LPARAM):LResult;stdcall;export;
function SetKeyHook:bool;export;//加载钩子
function EndKeyHook:bool;export;//卸载钩子
procedure KeyHookExit;far;
procedure MapFileMemory(dwAllocSize : DWORD);
procedure UnMapFileMemory;
procedure wrec(str:string);
procedure ReadIni;
implementation
procedure MapFileMemory(dwAllocSize : DWORD);
begin
{Create a process wide memory mapped variable}
MapHandle := CreateFileMapping($FFFFFFFF,
NIL,
PAGE_READWRITE,
0,
dwAllocSize,
'HookRecMemBlock');
if (MapHandle = 0) then
begin
exit;
end;
{Get a pointer to our process wide memory mapped variable}
Keys := MapViewOfFile(MapHandle,
FILE_MAP_WRITE,
0,
0,
dwAllocSize);
if (Keys = NIL) then begin
CloseHandle(MapHandle);
exit;
end;
end;
procedure UnMapFileMemory;
begin
{Delete our process wide memory mapped variable}
if (Keys <> NIL) then begin
UnMapViewOfFile(Keys);
Keys := NIL;
end;
if (MapHandle > 0) then begin
CloseHandle(MapHandle);
MapHandle := 0;
end;
end;
Procedure ReadIni;
Var
IniF:TIniFile;
begin
IniF:=TIniFile.Create('./Ini/KeyOpt.ini');
Try
Keys.KeyEnter:=IniF.ReadInteger('Param','Enter',0);
Keys.KeyEscape:=IniF.ReadInteger('Param','Esc',0);
Keys.KeyClear:=IniF.ReadInteger('Param','Clear',0);
Keys.KeyUp:=IniF.ReadInteger('Param','Up',0);
Keys.KeyDown:=IniF.ReadInteger('Param','Down',0);
Finally
Inif.Free;
end;
end;
procedure wrec( str:string);
var
debugfile:TextFile;
begin
if FileExists(afilename) then
begin
assignfile(debugfile,afilename);
append(debugfile);
write(debugfile,#10);
write(debugfile,#13);
write(debugfile,'[');
write(debugfile,str);
write(debugfile,']');
closefile(debugfile);
end
else begin
assignfile(debugfile,afilename);
rewrite(debugfile);
write(debugfile,'[');
write(debugfile,str);
write(debugfile,']');
closefile(debugfile);
end;
end;
function KeyBoardHook(iCode:Integer;wparam:WPARAM;lparam:LPARAM):LResult;
var
I:tagINPUT;
bp:^Byte;
Inif:TIniFile;
begin
Result:=0;
if iCode<0 then
begin
Result:=CallNextHookEx(HNextHookProc,iCode,wParam,lParam);
Exit;
end;
bp:=@lParam;
inc(bp);
inc(bp);
//wrec(inttostr(wParam));
if lParam>0 then
begin
if bp^<>$FE then
begin
sleep(5);
wrec(IntTostr(Keys.KeyEnter));
if wParam=Keys.KeyClear then
begin
//wrec('I have Reciver back key');
i.Itype:=input_keyboard;
i.ki.wVk:=221;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end
else if wParam=Keys.KeyEnter then
begin
i.Itype:=input_keyboard;
i.ki.wVk:=$48;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end
else if wParam=Keys.KeyEscape then
begin
//wrec('I have Rec Tab key');
i.Itype:=input_keyboard;
i.ki.wVk:=82;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end
else if wParam=Keys.KeyUp then
begin
i.Itype:=input_keyboard;
i.ki.wVk:=38;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end
else if wParam=Keys.KeyDown then
begin
i.Itype:=input_keyboard;
i.ki.wVk:=40;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end
else
case wParam of
97: // '1'
begin
i.Itype:=input_keyboard;
i.ki.wVk:=$42;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
98: // '2'
begin
i.Itype:=input_keyboard;
i.ki.wVk:=$4E;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
99: //'3'
begin
i.Itype:=input_keyboard;
i.ki.wVk:=77;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
100: //'4'
begin
i.Itype:=input_keyboard;
i.ki.wVk:=79;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
101: //'5'
begin
i.Itype:=input_keyboard;
i.ki.wVk:=$50;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
102: //'6'
begin
i.Itype:=input_keyboard;
i.ki.wVk:=219;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
103: //'7'
begin
i.Itype:=input_keyboard;
i.ki.wVk:=$51;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
104: //'8'
begin
i.Itype:=input_keyboard;
i.ki.wVk:=$57;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
105: //'9'
begin
i.Itype:=input_keyboard;
i.ki.wVk:=69;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
96:
begin // ‘0’
i.Itype:=input_keyboard;
i.ki.wVk:=68;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
110: // dot
begin
i.Itype:=input_keyboard;
i.ki.wVk:=70;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
119: //F8 左1
begin
i.Itype:=input_keyboard;
i.ki.wVk:=84;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
118: //左2
begin
i.Itype:=input_keyboard;
i.ki.wVk:=89;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
117: //左3
begin
i.Itype:=input_keyboard;
i.ki.wVk:=85;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
116: //左4
begin
i.Itype:=input_keyboard;
i.ki.wVk:=73;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
115: //右1
begin
i.Itype:=input_keyboard;
i.ki.wVk:=74;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
114: //右2
begin
i.Itype:=input_keyboard;
i.ki.wVk:=75;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
113: //右3
begin
i.Itype:=input_keyboard;
i.ki.wVk:=76;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
112: // F1 //右4
begin
i.Itype:=input_keyboard;
i.ki.wVk:=186;//ord('d');//字符0变成1;
i.ki.wScan:=$FE;
i.ki.dwFlags:=1;
SendInput(1, i,sizeof(i)); //模拟键盘
Result:=1;
Exit;
end;
else
Result:=0;
end;
end;
end
else
Result:=1;
end;
function SetKeyHook:bool;
var
bResult:BOOL;
Init: Boolean;
begin
bResult:=False;
//-----------------------------------------
MapHandle := 0;
Keys := NIL;
MapFileMemory(sizeof(Keys^));
//----------------------------------------
ReadIni;
HNextHookProc:=SetWindowsHookEx(WH_KEYBOARD,keyboardhook,HInstance,0);
if(HNextHookProc <> 0) then
bResult:=True;
Result:=bResult;
end;
function EndKeyHook:bool;
begin
if HNextHookProc<>0 then
begin
UnhookWindowsHookEx(HNextHookProc);
HNextHookProc:=0;
Result:=True;
Exit;
end;
HNextHookProc:=0;
//----------------------
UnMapFileMemory;
//----------------------
Result:=False;
end;
procedure KeyHookExit;
begin
if HNextHookProc<>0 then
EndKeyHook;
ExitProc:=ProcSaveExit;
end;
end.
你看一下我的程序为什么不行,我用你的内存映射文件也不行。你帮我看一下吧谢谢!