简单全局HOOK拦截大部分键盘消息
前言:学习HOOK中,
万一老师讲解HOOK入门教程:http://www.cnblogs.com/del/category/124150.html
http://www.cnblogs.com/del/category/124150.html
正题:全局HOOK拦截大部分键盘消息,Ctrl+alt+del 同Num Lock 目前测试无法截获。
写全局Hook基本步骤:
因为为全局Hook,要用到dll,以后就不知道需要不…………
一.创建dll
1.声明同实现回调函数
2.创建安装钩子函数
3.创建卸载钩子函数
4.按照dll格式输出函数
二、创建exe调用dll
源代码:
///////////////////////////dll代码////////////////////////////////////
//Delphi代码: dll代码
library MyHook;
uses
SysUtils,
Windows,
Messages,
Classes;
{$R *.res}
var
hook: HHOOK; {钩子变量}
//声明实现回调函数
function KeyHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
Result := 1; //1为杀掉,0为放过
end;
//创建安装钩子函数
function SetHook: Boolean; stdcall;
begin
hook := SetWindowsHookEx(WH_KEYBOARD, @keyHook, HInstance, 0);
Result := hook <> 0;
end;
//创建卸载钩子函数
function DelHook: Boolean; stdcall;
begin
Result := UnhookWindowsHookEx(hook);
end;
{按 DLL 的要求输出函数}
exports
SetHook name 'SetHook',
DelHook name 'DelHook',
keyHook name 'keyHook';
end.
uses
SysUtils,
Windows,
Messages,
Classes;
{$R *.res}
var
hook: HHOOK; {钩子变量}
//声明实现回调函数
function KeyHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
Result := 1; //1为杀掉,0为放过
end;
//创建安装钩子函数
function SetHook: Boolean; stdcall;
begin
hook := SetWindowsHookEx(WH_KEYBOARD, @keyHook, HInstance, 0);
Result := hook <> 0;
end;
//创建卸载钩子函数
function DelHook: Boolean; stdcall;
begin
Result := UnhookWindowsHookEx(hook);
end;
{按 DLL 的要求输出函数}
exports
SetHook name 'SetHook',
DelHook name 'DelHook',
keyHook name 'keyHook';
end.
///////////////////////////EXE代码////////////////////////////////////
//Delphi代码: EXE
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
end;
{DLL 中的函数声明}
function SetHook: Boolean; stdcall;
function DelHook: Boolean; stdcall;
var
Form1: TForm1;
implementation
{$R *.dfm}
{DLL 中的函数实现}
function SetHook; external 'MyHook.dll' name 'SetHook';
function DelHook; external 'MyHook.dll' name 'DelHook';
{安装钩子}
procedure TForm1.Button1Click(Sender: TObject);
begin
SetHook;
end;
{卸载钩子}
procedure TForm1.Button2Click(Sender: TObject);
begin
DelHook;
end;
{载次卸载钩子}
procedure TForm1.FormDestroy(Sender: TObject);
begin
DelHook;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
end;
{DLL 中的函数声明}
function SetHook: Boolean; stdcall;
function DelHook: Boolean; stdcall;
var
Form1: TForm1;
implementation
{$R *.dfm}
{DLL 中的函数实现}
function SetHook; external 'MyHook.dll' name 'SetHook';
function DelHook; external 'MyHook.dll' name 'DelHook';
{安装钩子}
procedure TForm1.Button1Click(Sender: TObject);
begin
SetHook;
end;
{卸载钩子}
procedure TForm1.Button2Click(Sender: TObject);
begin
DelHook;
end;
{载次卸载钩子}
procedure TForm1.FormDestroy(Sender: TObject);
begin
DelHook;
end;
end.
//看了这么多例程,只有这个最让人懂~ 全文:http://www.cnblogs.com/piaohun/archive/2008/07/06/1237031.html