首页 > 其他分享 >ChatGPT:用delphi写一个键盘鼠标的全局钩子dll示例

ChatGPT:用delphi写一个键盘鼠标的全局钩子dll示例

时间:2023-02-16 21:33:29浏览次数:49  
标签:wParam begin end Hook delphi 示例 dll Callback KeyboardHook

library GlobalHooks;

uses
  Windows;

{$R *.res}

const
  WH_KEYBOARD_LL = 13;
  WH_MOUSE_LL = 14;

type
  TKeyboardHook = record
    Hook: HHOOK;
    Callback: function(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
  end;

  TMouseHook = record
    Hook: HHOOK;
    Callback: function(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
  end;

var
  KeyboardHook: TKeyboardHook;
  MouseHook: TMouseHook;

function KeyboardHookCallback(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
  if (nCode = HC_ACTION) and ((wParam = WM_KEYDOWN) or (wParam = WM_KEYUP)) then
  begin
    // 处理键盘事件
    // ...
  end;
  Result := CallNextHookEx(KeyboardHook.Hook, nCode, wParam, lParam);
end;

function MouseHookCallback(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
  if (nCode = HC_ACTION) and ((wParam = WM_LBUTTONDOWN) or (wParam = WM_LBUTTONUP) or
                              (wParam = WM_RBUTTONDOWN) or (wParam = WM_RBUTTONUP)) then
  begin
    // 处理鼠标事件
    // ...
  end;
  Result := CallNextHookEx(MouseHook.Hook, nCode, wParam, lParam);
end;

function InstallKeyboardHook(Callback: function(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT): Boolean;
begin
  Result := False;
  if not Assigned(Callback) then
    Exit;
  if Assigned(KeyboardHook.Callback) then
    Exit;
  KeyboardHook.Hook := SetWindowsHookEx(WH_KEYBOARD_LL, @KeyboardHookCallback, HInstance, 0);
  if KeyboardHook.Hook = 0 then
    Exit;
  KeyboardHook.Callback := Callback;
  Result := True;
end;

function InstallMouseHook(Callback: function(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT): Boolean;
begin
  Result := False;
  if not Assigned(Callback) then
    Exit;
  if Assigned(MouseHook.Callback) then
    Exit;
  MouseHook.Hook := SetWindowsHookEx(WH_MOUSE_LL, @MouseHookCallback, HInstance, 0);
  if MouseHook.Hook = 0 then
    Exit;
  MouseHook.Callback := Callback;
  Result := True;
end;

function RemoveKeyboardHook: Boolean;
begin
  Result := False;
  if KeyboardHook.Hook = 0 then
    Exit;
  if UnhookWindowsHookEx(KeyboardHook.Hook) then
  begin
    KeyboardHook.Hook := 0;
    KeyboardHook.Callback := nil;
    Result := True;
  end;
end;

function RemoveMouseHook: Boolean;
begin
  Result := False;
  if MouseHook.Hook = 0 then
    Exit;
  if UnhookWindowsHookEx(MouseHook.Hook) then
  begin
    MouseHook.Hook := 0;
    MouseHook.Callback := nil;
    Result := True;
  end;
end;

exports
  InstallKeyboardHook,
  InstallMouseHook,
  RemoveKeyboardHook,
  RemoveMouseHook;

begin
end.

 

标签:wParam,begin,end,Hook,delphi,示例,dll,Callback,KeyboardHook
From: https://www.cnblogs.com/onlyou13/p/17128388.html

相关文章

  • delphi cxGrid过滤器配合TFDQuery过滤器,完美!
    效果图:  设计图(数据库是ACCESS类型):   需求:1.cxGrid默认的模糊匹配符为%,太麻烦了,想用*号代替(ACCESS数据库,只认*,不认%)2.cxGrid默认是在用......
  • WinForm滚动条美化-Panel示例(一)
    关于滚动条美化网上翻阅相关资料大概分两种,一是覆盖原有滚动条;二是纯靠绘制+钩子实现;两种实现方式都依赖系统Api来实现;话不多说先上效果图该效果主要是隐藏滚动条;消息拦......
  • Delphi的编译指令
    写给Compiler看的特别指示,就是编译指令。编译指令时给计算机看的,用于区别在不同的场合下进行编译不同代码的文件一些常用的条件编译指令条件编译指令含义:$DEFINE//......
  • antd table 自定义折叠展开图标(示例)
    expandable={{expandedRowKeys,onExpandedRowsChange:(expandedRows)=>{updateState((draft)=......
  • 附录E示例-Tin_3
    示例概述意图这个场景有一个很小的三角网(只有1.5K个三角形),中间没有空隙。 内容为地理参考:-地图投影MGA9456区的水平坐标为东距和北距,-垂直坐标为AHD,-水平和垂直基准......
  • 【OpenCV】-边缘检测汇总示例
    汇总之前:这一节还有一个scharr滤波器(也是算子)没有介绍,先介绍一下。。。1计算图像差分:Scharr()函数使用Scharr滤波器运算符计算x或y方向的图像差分,包含的参数与Sobel算子基......
  • 【OpenCV】图片尺寸放缩示例程序
    这次示例程序分别演示了用resize、pyrUp、pyrDown来让源图像进行放大缩小操作#include<opencv2/opencv.hpp>#include<opencv2/imgproc/imgproc.hpp>#include<opencv2/highgu......
  • delphi 弹出选择目录对话框(选择文件夹)
    弹出选择目录对话框(选择文件夹)代码弹出Windows目录浏览器对话框usesVcl.FileCtrl;procedureTForm1.Button1Click(Sender:TObject);varvPath:string;begin......
  • vcruntime140_1.dll丢失怎么安装?
    许多用户在使用电脑的时候,在安装运行某些程序的时候,电脑突然提示由于找不到vcruntime140_1.dll,无法继续执行代码,遇到这种情况我们应该怎么解决呢?下面小编就带着大家一起看看......
  • awk多字符串分割用法示例
    多字符串分割用单引号,不要用“[]”,但多个直接仍然用竖号分隔,需要转义的也仍然用斜杠“\”,但注意需要两个斜杠“\”,因为斜杠本身也需要转义。示例:两个多字符串分割符,分别为......