THook Delphi类与源代码

Code submitted by Jens Borrisholt. Text by Zarko Gajic.

Jens Borrisholt提交的代码。 Zarko Gajic的文字。

By Jens: Hooks, I’ve seen a lot of people trying to make a clean solution for hooking messages in an application. So I decided some time ago to implement hooks as a class, with nice events and stuff :)

Jens: Hooks,我已经看到很多人试图为在应用程序中挂钩消息提供一个干净的解决方案。 因此,我前段时间决定将钩子作为一个类来实现,它带有漂亮的事件和东西:)

Hook.pas makes it possible to assign a method pointer to a procedure pointer (with some help from assembler).

Hook.pas使得可以将方法指针分配给过程指针(在汇编器的帮助下)。

For example: if you want to trap ALL keystrokes in your application - simply declare an instance of TKeyboardHook, assign an event handler for OnPreExecute or OnPostExecute, or both. Set you KeyboadHook active (KeyboardHook.Active := True) and you are out and running ..

例如:如果要在应用程序中捕获所有击键-只需声明TKeyboardHook的实例,为OnPreExecute或OnPostExecute分配事件处理程序,或同时为两者分配。 将您的KeyboadHook设置为活动状态(KeyboardHook.Active:= True),然后退出并运行..

在Windows挂钩上 ( On Windows Hooks )

A hook is a point in the system message-handling mechanism where an application can install a subroutine to monitor the message traffic in the system and process certain types of messages before they reach the target window procedure.

挂钩是系统消息处理机制中的一个点,应用程序可以在该点上安装子例程,以监视系统中的消息流量并在消息到达目标窗口过程之前处理某些类型的消息。

Put shortly, a hook is a function you can create as part of a dll or your application to monitor the 'goings on' inside the Windows operating system.

简而言之,挂钩是一种功能,您可以将其创建为dll或应用程序的一部分,以监视Windows操作系统内部的“运行情况”。

The idea is to write a function that is called every time a certain event in windows occurs - for example when a user presses a key on the keyboard or moves the mouse.

这个想法是编写一个每次在Windows中发生特定事件时都会调用的函数-例如,当用户按下键盘上的键或移动鼠标时。

For a more in depth introduction to hooks, take a look at What Windows hooks are and how to use them within a Delphi application.

有关钩子的更深入介绍,请查看什么是Windows钩子以及如何在Delphi应用程序中使用它们。

Hooking mechanism relies on Windows messages and callback functions.

挂钩机制依赖于Windows消息回调函数

挂钩类型 ( Types of Hooks )

For example:You can use the WH_KEYBOARD hook to monitor keyboard input posted to a message queue;You can use the WH_MOUSE hook to monitor mouse input posted to a message queue;You can a WH_SHELL hook procedure when the shell application is about to be activated and when a top-level window is created or destroyed.

例如:您可以使用WH_KEYBOARD挂钩来监视发布到消息队列中的键盘输入;可以使用WH_MOUSE挂钩来监视发布到消息队列中的鼠标输入;可以在外壳程序应用程序即将被激活时使用WH_SHELL挂钩过程以及创建或销毁顶层窗口的时间。

Hooks.pas ( Hooks.pas )

  • TCBTHook - called before activating, creating, destroying, minimizing, maximizing, moving, or sizing a window; before completing a system command; before removing a mouse or keyboard event from the system message queue; before setting the input focus; or before synchronizing with the system message queue.

    TCBTHook-在激活,创建,销毁,最小化,最大化,移动或调整窗口大小之前调用; 在完成系统命令之前; 从系统消息队列中删除鼠标或键盘事件之前; 在设置输入焦点之前; 或与系统消息队列同步之前。
  • TDebugHook - called before calling hook procedures associated with any other hook in the system

    TDebugHook-在调用与系统中任何其他挂钩关联的挂钩过程之前调用
  • TGetMessageHook - enables an application to monitor messages about to be returned by the GetMessage or PeekMessage function

    TGetMessageHook-使应用程序可以监视即将由GetMessage或PeekMessage函数返回的消息
  • TJournalPlaybackHook - enables an application to insert messages into the system message queue.

    TJournalPlaybackHook-使应用程序可以将消息插入系统消息队列。
  • TJournalRecordHook - enables you to monitor and record input events (to record a sequence of mouse and keyboard events to play back later by using the WH_JOURNALPLAYBACK Hook).

    TJournalRecordHook-使您能够监视和记录输入事件(以记录鼠标和键盘事件的序列,以便以后使用WH_JOURNALPLAYBACK Hook进行回放)。
  • TKeyboardHook - enables an application to monitor message traffic for WM_KEYDOWN and WM_KEYUP messages.

    TKeyboardHook-使应用程序可以监视WM_KEYDOWN和WM_KEYUP消息的消息流量。
  • TMouseHook - enables you to monitor mouse messages about to be returned by the GetMessage or PeekMessage function.

    TMouseHook-使您可以监视GetMessage或PeekMessage函数将要返回的鼠标消息。
  • TLowLevelKeyboardHook - enables you to monitor keyboard input events about to be posted in a thread input queue.

    TLowLevelKeyboardHook-使您可以监视即将在线程输入队列中发布的键盘输入事件。
  • TLowLevelMouseHook - enables you to monitor mouse input events about to be posted in a thread input queue.

    TLowLevelMouseHook-使您可以监视即将在线程输入队列中发布的鼠标输入事件。

TKeyboardHook示例 ( TKeyboardHook example )

Download hooks.pas + demo application

下载hooks.pas +演示应用程序


uses hooks, ....
var
  KeyboardHook: TKeyboardHook;
....
//MainForm's OnCreate event handlerprocedure TMainForm.FormCreate(Sender: TObject) ;
begin
  KeyboardHook := TKeyboardHook.Create;
  KeyboardHook.OnPreExecute := KeyboardHookPREExecute;
  KeyboardHook.Active := True;
end;
//handles KeyboardHook's OnPREExecuteprocedure TMainForm.KeyboardHookPREExecute(Hook: THook; var Hookmsg: THookMsg) ;
var
  Key: Word;
begin
  //Here you can choose if you want to return   //the key stroke to the application or not
  Hookmsg.Result := IfThen(cbEatKeyStrokes.Checked, 1, 0) ;
  Key := Hookmsg.WPARAM;
  Caption := Char(key) ;
end;

Ready, set, hook :)

准备,设置,挂接:)

翻译自: https://www.thoughtco.com/thook-delphi-class-with-source-code-1058455

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值