别人写的一个不用写DLL文件的示例<br>const<br> KeyPressMask=$80000000; //键盘掩码常量<br>var<br> MainForm: TMainForm;<br> g_hLogHook:HHOOK=0; //钩子变量<br> g_hLastFocus:HWnd=0; //记录上一次得到焦点的窗口句柄<br> g_TFocus:HWnd=0;<br> g_PrvChar:Char; //保存上一次按键值<br><br>implementation<br><br>function JournalLogProc(iCode:Integer;wParam:WPARAM;lParam:LPARAM): LRESULT stdcall;<br>var<br> color: TColor;<br> pEvt:^EVENTMSG;<br> hFocus, TFocus:HWND; //保存当前活动窗口句柄<br> szTitle:array[0..255]of Char; //当前窗口名称<br> classname : array[0..255] of Char;<br> szaTitle : array[0..255] of Char;<br> classaname : array[0..255] of Char;<br> vKey:Integer;<br> str:string;<br> Mouse1, Mouse2:String;<br> iShift:Integer;<br> iCapital:Integer;<br> iNumLock:Integer;<br> bShift:Boolean;<br> bCapital:Boolean;<br> bNumLock:Boolean;<br>begin<br> Result:=0;<br> if iCode<0 then<br> exit;<br> CallNextHookEx(g_hLogHook,iCode,wParam,lParam);<br> if iCode=HC_ACTION then<br> begin<br> pEvt:=Pointer(DWord(lParam));<br> if (pEvt.message=WM_KEYDOWN) or (pEvt.message=WM_KEYUP) OR (pEvt.message=260) then<br> begin<br> MainForm.KeyLabel1.Caption:='事 件:';<br> case pEvt.message of<br> WM_KEYDOWN: MainForm.KeyLabel1.Caption:='按下事件:';<br> WM_KEYUP: MainForm.KeyLabel1.Caption:='松开事件:';<br> end;<br> MainForm.KeyEdit1.Clear;<br> MainForm.KeyEdit2.Clear;<br> MainForm.KeyEdit3.Clear;<br> vKey:=LOBYTE(pEvt.paramL); // 取得虚拟键值<br> hFocus:=GetActiveWindow(); //取得当前活动窗口句柄<br> if g_hLastFocus<>hFocus then //当前活动窗口是否改变<br> begin<br> GetWindowText(hFocus,szTitle,256);<br> g_hLastFocus:=hFocus;<br> getClassName(hFocus,classname,256);<br> MainForm.OthEdit1.Text:='局柄:'+intTostr(hFocus)+Format(' 标题:%s',[szTitle])+Format(' 类:%s',[classname]);<br> end;<br> iShift:=GetKeyState($10); //测试SHIFT,CAPTION,NUMLOCK等键是否按下<br> iCapital:=GetKeyState($14);<br> iNumLock:=GetKeyState($90);<br> bShift:=(iShift and KeyPressMask)=KeyPressMask;<br> bCapital:=(iCapital and 1)=1;<br> bNumLock:=(iNumLock and 1)=1;<br> MainForm.KeyEdit2.Text:=IntToStr(vKey);<br> CASE vKey OF<br> 8..46: case vKey of<br> 8: str:='BackSpace';<br> 9: str:='TAB';<br> 12: str:='Clear';<br> 13: str:='Enter';<br> 16: str:='Shift';<br> 17: str:='Ctrl';<br> 18: str:='Alt';<br> 19: str:='Pause Break';<br> 20: str:='Caps Lock';<br> 27: str:='Esc';<br> 32: str:='Space';<br> 33: str:='Page Up';<br> 34: str:='Page Down';<br> 35: str:='End';<br> 36: str:='Home';<br> 37: str:='Left Arrow';<br> 38: str:='Up Arrow';<br> 39: str:='Right Arrow';<br> 40: str:='Down Arrow';<br> 41: str:='Select';<br> 42: str:='Print';<br> 43: str:='Execute';<br> 44: str:='Print Screen';<br> 45: str:='Insert';<br> 46: str:='Delete';<br> 47: str:='Help';<br> END;<br> 48..57: if not bShift then str:=Char(vKey)// 数字0-9<br> else<br> case vKey of<br> 48: str:=')';<br> 49: str:='!';<br> 50: str:='@';<br> 51: str:='#';<br> 52: str:='$';<br> 53: str:='%';<br> 54: str:='^';<br> 55: str:='&';<br> 56: str:='*';<br> 57: str:='(';<br> end;<br> 65..90: if not bCapital then // A-Z a-z<br> begin<br> if bShift then str:=Char(vKey)<br> else str:=Char(vKey+32);<br> end<br> else<br> begin<br> if bShift then str:=Char(vKey+32)<br> else str:=Char(vKey);<br> end;<br> 91..93: case vKey of<br> 91: str:='Left Windows';<br> 92: str:='Right Windows';<br> 93: str:='Applications';<br> end;<br> 96..105: if bNumLock then str:=Char(vKey-96+48);// 小键盘0-9<br> 106..111: case vKey of<br> 106: str:='*';<br> 107: str:='+';<br> 108: str:='';<br> 109: str:='-';<br> 110: str:='.';<br> 111: str:='/';<br> END;<br> 112..135: case vKey of // 功能键 [F1]-[F12]<br> 112: str:='F1';<br> 113: str:='F2';<br> 114: str:='F3';<br> 115: str:='F4';<br> 116: str:='F5';<br> 117: str:='F6';<br> 118: str:='F7';<br> 119: str:='F8';<br> 120: str:='F9';<br> 121: str:='F10';<br> 122: str:='F11';<br> 123: str:='F12';<br> 124: str:='F13';<br> 125: str:='F14';<br> 126: str:='F15';<br> 127: str:='F16';<br> 128: str:='F17';<br> 129: str:='F18';<br> 130: str:='F19';<br> 131: str:='F20';<br> 132: str:='F21';<br> 133: str:='F22';<br> 134: str:='F23';<br> 135: str:='F24';<br> end;<br> 144..165: case vKey of<br> 144: str:='Num Lock';<br> 145: str:='Scroll Lock';<br> 160: str:='Left Shift';<br> 161: str:='Right Shift';<br> 162: str:='Left Ctrl';<br> 163: str:='Right Ctrl';<br> 164: str:='Left Alt';<br> 165: str:='Right Alt';<br> end; <br> 186..222: case vKey of // 其他键<br> 186: if not bShift then str:=';' else str:=':';<br> 187: if not bShift then str:='=' else str:='+';<br> 188: if not bShift then str:=',' else str:='<';<br> 189: if not bShift then str:='-' else str:='_';<br> 190: if not bShift then str:='.' else str:='>';<br> 191: if not bShift then str:='/' else str:='?';<br> 192: if not bShift then str:='`' else str:='~';<br> 219: if not bShift then str:='[' else str:='{';<br> 220: if not bShift then str:='/' else str:='|';<br> 221: if not bShift then str:=']' else str:='}';<br> 222: if not bShift then str:=char(39) else str:='"';<br> END;<br> 229..254: case vKey of<br> 229: str:='Process';<br> 246: str:='Attn';<br> 247: str:='CrSel';<br> 248: str:='ExSel';<br> 249: str:='Erase EOF';<br> 250: str:='Play';<br> 251: str:='Zoom';<br> 252: str:='Reserved for future use';<br> 253: str:='PA1';<br> 254: str:='Clear';<br> end;<br> else str:='';<br> END;<br> MainForm.KeyEdit1.Text:=str;<br> if g_PrvChar<>Char(vKey) then g_PrvChar:=Char(vKey);<br> end<br> else<br> begin<br> case pEvt.message of<br> WM_MOUSEWHEEL: BEGIN<br> Mouse2:='WM_MOUSEWHEEL';<br> Mouse1:='鼠标滚轮滚动';<br> END;<br> WM_MOUSEMOVE: begin<br> MainForm.OthEdit3.Text:=Format('X:%d, Y:%d',[pEvt.paramL,pEvt.paramH]);<br> color:=GetPixel(getdc(0),pEvt.paramL,pEvt.paramH);<br> MainForm.OthEdit4.Color:=color;<br> MainForm.OthEdit4.Font.Color:=TColor(ColorToRGB(Color) xor $ffffff);<br> MainForm.OthEdit4.Text:='R='+inttostr(color and $FF) +<br> ' G='+inttostr(hi(color and $FF00)) +<br> ' B='+inttostr(hiword(color and $FF0000)) +<br> ' HEX='+inttohex(color, 6);<br> end;<br> WM_LBUTTONDOWN: begin<br> Mouse2:='WM_LBUTTONDOWN';<br> Mouse1:='按下鼠标左键';<br> end;<br> WM_LBUTTONUP: begin<br> Mouse2:='WM_LBUTTONUP';<br> Mouse1:='释放鼠标左键';<br> end;<br> WM_LBUTTONDBLCLK: begin<br> Mouse2:='WM_LBUTTONDBLCLK';<br> Mouse1:='双击鼠标左键';<br> end;<br> WM_RBUTTONDOWN: begin<br> Mouse2:='WM_RBUTTONDOWN';<br> Mouse1:='按下鼠标右键';<br> end;<br> WM_RBUTTONUP: begin<br> Mouse2:='WM_RBUTTONUP';<br> Mouse1:='释放鼠标右键';<br> end;<br> WM_RBUTTONDBLCLK: begin<br> Mouse2:='WM_RBUTTONDBLCLK';<br> Mouse1:='双击鼠标右键';<br> end;<br> WM_MBUTTONDOWN: begin<br> Mouse2:='WM_MBUTTONDOWN';<br> Mouse1:='按下鼠标中键';<br> end;<br> WM_MBUTTONUP: begin<br> Mouse2:='WM_MBUTTONUP';<br> Mouse1:='释放鼠标中键';<br> end;<br> WM_MBUTTONDBLCLK: begin<br> Mouse2:='WM_MBUTTONDBLCLK';<br> Mouse1:='双击鼠标中键';<br> end;<br> end;<br> if (Mouse1<>'') and (Mouse2<>'') then<br> begin<br> MainForm.MouseEdit2.Text:=Mouse2;<br> MainForm.MouseEdit1.Text:=Mouse1;<br> end;<br> end;<br> end;<br> Result:=CallNextHookEx(g_hLogHook,iCode,wParam,lParam);<br>end;<br><br>//使用:<br>procedure TMainForm.KeyCapButClick(Sender: TObject);<br>begin<br> OthBox.SetFocus;<br> if KeyCapBut.Caption='开始捕获' then<br> begin<br> if (g_hLogHook=0) then<br> g_hLogHook:=SetWindowsHookEx(WH_JOURNALRECORD, JournalLogProc,HInstance,0);<br> KeyCapBut.Caption:='停止捕获';<br> end<br> else<br> begin<br> if (g_hLogHook<>0) then<br> begin<br> UnhookWindowsHookEx(g_hLogHook);<br> g_hLogHook:=0;<br> end;<br> KeyCapBut.Caption:='开始捕获';<br> end;<br>end;