如何HOOK系统所有的键盘鼠标事件

dingfeng1977 2003-06-18 09:34:59
需要做一个等待多少空闲时间的程序,有点象屏幕保护的功能
...全文
133 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
henry2003 2003-06-18
  • 打赏
  • 举报
回复
我給你代碼:
//鼠標
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

EventArr:array[0..1000]of EVENTMSG;

EventLog:Integer;

PlayLog:Integer;

hHook,hPlay:Integer;

recOK:Integer;

canPlay:Integer;

bDelay:Bool;

implementation

{$R *.dfm}

Function PlayProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;

begin

canPlay:=1;

Result:=0;


if iCode < 0 then //必??消息??到消息?的下一?接受?元

Result := CallNextHookEx(hPlay,iCode,wParam,lParam)

else if iCode = HC_SYSMODALON then

canPlay:=0

else if iCode = HC_SYSMODALOFF then

canPlay:=1

else if ((canPlay =1 )and(iCode=HC_GETNEXT)) then begin

if bDelay then begin

bDelay:=False;

Result:=50;

end;

pEventMSG(lParam)^:=EventArr[PlayLog];

end

else if ((canPlay = 1)and(iCode = HC_SKIP))then begin

bDelay := True;

PlayLog:=PlayLog+1;

end;

if PlayLog>=EventLog then begin

UNHookWindowsHookEx(hPlay);

end;

end;


function HookProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;

begin
recOK:=1;
Result:=0;
if iCode < 0 then
Result := CallNextHookEx(hHook,iCode,wParam,lParam)
else if iCode = HC_SYSMODALON then
recOK:=0
else if iCode = HC_SYSMODALOFF then
recOK:=1
else if ((recOK>0) and (iCode = HC_ACTION)) then begin
EventArr[EventLog]:=pEventMSG(lParam)^;
EventLog:=EventLog+1;
if EventLog>=1000 then begin
UnHookWindowsHookEx(hHook);
end;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption:='記錄';
Button2.Caption:='停止';
Button3.Caption:='回放';
Button4.Caption:='范例';
Button2.Enabled:=False;
Button3.Enabled:=False;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
EventLog:=0;
hHook:=SetwindowsHookEx(WH_JOURNALRECORD,HookProc,HInstance,0);
Button2.Enabled:=True;
Button1.Enabled:=False;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
UnHookWindowsHookEx(hHook);
hHook:=0;
Button1.Enabled:=True;
Button2.Enabled:=False;
Button3.Enabled:=True;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
PlayLog:=0;
hPlay:=SetwindowsHookEx(WH_JOURNALPLAYBACK,PlayProc,
HInstance,0);
Button3.Enabled:=False;
end;

end.


//鍵盤
unit KB_u;

interface

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

Const MI_iconEvent=Wm_user+1;

type
TKBForm = class(TForm)
PopupMenu1: TPopupMenu;
SetHook1: TMenuItem;
EndHook1: TMenuItem;
Close1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure SetHook1Click(Sender: TObject);
procedure EndHook1Click(Sender: TObject);
procedure Close1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormPaint(Sender: TObject);

private
{ Private declarations }
nid:NotifyIconData;
NormalIcon:Ticon;
public
{ Public declarations }
Procedure Icontray(Var Msg:TMessage);
Message Mi_IConEvent;
end;

var
KBForm: TKBForm;

implementation

{$R *.dfm}

function SetKeyHook:bool;External 'KeySPY.dll';
function EndKeyHook:bool;External 'KeySPY.dll';

Procedure TKBForm.Icontray(Var Msg:TMessage);
var pt:Tpoint;
begin
if Msg.LParam=wm_lbuttondown then
sethook1Click(Self);

if Msg.LParam=Wm_rbuttondown then
begin
GetCursorPos(pt);
SetForegroundWindow(Handle);
PopupMenu1.Popup(Pt.x,Pt.Y);
end;
end;

procedure TKBForm.FormCreate(Sender: TObject);
begin
NormalIcon:=Ticon.Create;
Application.Title:=Caption;

nid.cbSize:=Sizeof(nil);
nid.Wnd:=Handle;
nid.uID:=0;
nid.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
nid.uCallbackMessage:=MI_iconEvent;
nid.hIcon:=Application.Icon.Handle;
StrCopy(nid.szTip,pchar(Caption));
nid.uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;

if not Shell_NotifyIcon(nim_add,@nid) then
begin
ShowMessage('失敗');
Application.Terminate;
end;
SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
end;

procedure TKBForm.SetHook1Click(Sender: TObject);
begin
Setkeyhook;
end;

procedure TKBForm.EndHook1Click(Sender: TObject);
begin
Endkeyhook;
end;

procedure TKBForm.Close1Click(Sender: TObject);
begin
Application.Terminate;
end;

procedure TKBForm.FormDestroy(Sender: TObject);
begin
nid.uFlags:=0;
Shell_NotifyIcon(nim_delete,@nid);
end;


procedure TKBForm.FormPaint(Sender: TObject);
begin
Hide;
end;

end.


布学无数 2003-06-18
  • 打赏
  • 举报
回复
补充:

1、计时可以在回调函数中实现。
2、因为是全局钩子,所以回调函数必须放在 DLL 中,否则这两个钩子只在本进程中有效:)
3、本人正在学习 DELPHI ,如果大虾把程序转成了 DELPHI 写法,请贴出来!!!
布学无数 2003-06-18
  • 打赏
  • 举报
回复
使用 SetWindowsHookEx(...) 函数挂接鼠标和键盘钩子,如:

HHOOK hHookMouse = NULL;
HHOOK hHookKeyBD = NULL;
HMODULE hHookDll = NULL;

hHookDll = LoadLibrary("Hook.dll");
//Hook.dll 中必须导出鼠标和键盘钩子的回调函数
//我们假定鼠标钩子的回调函数为 MouseHookProc
// 键盘钩子的回调函数为 KeybdHookProc

hHookMouse = SetWindowsHookEx(WH_MOUSE,MouseHookProc,hHookDll,0L);
hHookKeyBD = SetWindowsHookEx(WH_KEYBOARD,KeybdHookProc,hHookDll,0L);

//注意要将 hHookMouse 和 hHookKeyBD 两个句柄传入 Hook.dll 中,因为在回调函数中需要用到这两个句柄:)
//还有,我是用 C 语法写的,至于怎么转为 DELPHI 写法,应该很容易吧:)
dingfeng1977 2003-06-18
  • 打赏
  • 举报
回复
谁知道啊?

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧