这是我在delphi精要中抄来的钩子的讲解,但是我的问题不是要把按下的键记录下来,而是要屏蔽掉某个按键,譬如CTRL+ALT+DEL、或ALT+F4等键,如何改呢?
谢谢!!!
unit HKProc;interfaceuses
Windows; var
F: File of Char;
function EnableHotKeyHook: BOOL; stdcall;
function DisableHotKeyHook: BOOL; stdcall;
procedure HotKeyHookExit;implementationconst
_KeyPressMask = $80000000 ; var
C: Char;
ShiftDown,CapsDown: Boolean;
hNextHookProc: HHook;function KeyboardHookHandler(
iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
Result := 0;
if iCode < 0 then
begin
Result := CallNextHookEx(hNextHookProc,iCode,wParam,lParam);
Exit;
end;
{ if (lParam and _KeyPressMask) = 0 then //第32位为0,表示KeyDown状态
begin
ShiftDown := (GetKeyState($10) and _KeyPressMask) = _KeyPressMask;
CapsDown := (GetKeyState($14) and 1) = 1; if wParam < 65 then
begin
if ShiftDown then
C := Chr(wParam-16)
else
C := Chr(wParam);
end else
begin
if wParam in [96..105] then
C := Chr(wParam-48) //数字键盘
else if ShiftDown xor CapsDown then
C := Chr(wParam)
else
C := Chr(wParam+32);
end;
Seek(F,FileSize(F));
Write(F,C);
end; }
end;
function EnableHotKeyHook: BOOL; stdcall;
begin
if hNextHookProc = 0 then
begin
// ReWrite(F);
hNextHookProc := SetWindowsHookEx(
WH_KEYBOARD,KeyboardHookHandler,Hinstance,0);
end;
Result := hNextHookProc <> 0;
end;
function DisableHotKeyHook: BOOL; stdcall;
begin
if hNextHookPRoc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc);
hNextHookProc := 0;
end;
Result := hNextHookPRoc = 0;
end;
procedure HotKeyHookExit;
begin
DisableHotKeyHook;
// Close(F);
end;
end.library KeyboardHOOK;uses
Windows,
HKProc in 'HKProc.pas';
exports
EnableHotKeyHook,
DisableHotKeyHook;{$R *.res}procedure LibraryProc(Reason: Integer);
var
P: PChar;
begin
case Reason of
DLL_PROCESS_ATTACH: ;
{ begin
GetMem(P,MAXBYTE);
GetSystemDirectory(P,MAXBYTE);
Assign(F,'c:\result.txt');
ReWrite(F);
FreeMem(P);
end;}
DLL_PROCESS_DETACH: HotKeyHookExit;
end;
end;begin
DLLProc := @LibraryProc;
LibraryProc(DLL_PROCESS_ATTACH);
end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TEnableHotKeyHook = function: BOOL; stdcall;
TDisableHotKeyHook = function: BOOL; stdcall; TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationvar
EnableHotKeyHook: TEnableHotKeyHook;
DisableHotKeyHook: TDisableHotKeyHook;
DllHandle: THandle;{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
EnableHotKeyHook;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
DisableHotKeyHook;
end;initialization DllHandle := LoadLibrary('KeyboardHOOK.dll');
if DllHandle <> 0 then
begin
@EnableHotKeyHook := GetProcAddress(DllHandle,'EnableHotKeyHook');
@DisableHotKeyHook := GetProcAddress(DllHandle,'DisableHotKeyHook');
end;finalization
FreeLibrary(DllHandle);end.
谢谢!!!
unit HKProc;interfaceuses
Windows; var
F: File of Char;
function EnableHotKeyHook: BOOL; stdcall;
function DisableHotKeyHook: BOOL; stdcall;
procedure HotKeyHookExit;implementationconst
_KeyPressMask = $80000000 ; var
C: Char;
ShiftDown,CapsDown: Boolean;
hNextHookProc: HHook;function KeyboardHookHandler(
iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
Result := 0;
if iCode < 0 then
begin
Result := CallNextHookEx(hNextHookProc,iCode,wParam,lParam);
Exit;
end;
{ if (lParam and _KeyPressMask) = 0 then //第32位为0,表示KeyDown状态
begin
ShiftDown := (GetKeyState($10) and _KeyPressMask) = _KeyPressMask;
CapsDown := (GetKeyState($14) and 1) = 1; if wParam < 65 then
begin
if ShiftDown then
C := Chr(wParam-16)
else
C := Chr(wParam);
end else
begin
if wParam in [96..105] then
C := Chr(wParam-48) //数字键盘
else if ShiftDown xor CapsDown then
C := Chr(wParam)
else
C := Chr(wParam+32);
end;
Seek(F,FileSize(F));
Write(F,C);
end; }
end;
function EnableHotKeyHook: BOOL; stdcall;
begin
if hNextHookProc = 0 then
begin
// ReWrite(F);
hNextHookProc := SetWindowsHookEx(
WH_KEYBOARD,KeyboardHookHandler,Hinstance,0);
end;
Result := hNextHookProc <> 0;
end;
function DisableHotKeyHook: BOOL; stdcall;
begin
if hNextHookPRoc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc);
hNextHookProc := 0;
end;
Result := hNextHookPRoc = 0;
end;
procedure HotKeyHookExit;
begin
DisableHotKeyHook;
// Close(F);
end;
end.library KeyboardHOOK;uses
Windows,
HKProc in 'HKProc.pas';
exports
EnableHotKeyHook,
DisableHotKeyHook;{$R *.res}procedure LibraryProc(Reason: Integer);
var
P: PChar;
begin
case Reason of
DLL_PROCESS_ATTACH: ;
{ begin
GetMem(P,MAXBYTE);
GetSystemDirectory(P,MAXBYTE);
Assign(F,'c:\result.txt');
ReWrite(F);
FreeMem(P);
end;}
DLL_PROCESS_DETACH: HotKeyHookExit;
end;
end;begin
DLLProc := @LibraryProc;
LibraryProc(DLL_PROCESS_ATTACH);
end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TEnableHotKeyHook = function: BOOL; stdcall;
TDisableHotKeyHook = function: BOOL; stdcall; TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationvar
EnableHotKeyHook: TEnableHotKeyHook;
DisableHotKeyHook: TDisableHotKeyHook;
DllHandle: THandle;{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
EnableHotKeyHook;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
DisableHotKeyHook;
end;initialization DllHandle := LoadLibrary('KeyboardHOOK.dll');
if DllHandle <> 0 then
begin
@EnableHotKeyHook := GetProcAddress(DllHandle,'EnableHotKeyHook');
@DisableHotKeyHook := GetProcAddress(DllHandle,'DisableHotKeyHook');
end;finalization
FreeLibrary(DllHandle);end.
解决方案 »
- Statement expected but 'PROCEDURE' found,请高手解决
- 各位老大看看为什么数据库连接失败?
- AdoQuery 执行脚本,在线等待
- 高手请指教:如何让黑白扫描的图片清晰
- 如何用二个按钮代替,richedit的滚动条按钮???
- 从Access数据库中读出数据然后插到Microsoft Visual FoxPro 6.0中去的问题!
- excel的数据导入到sql server中
- 关于delphi如何实现对网络视频流的解码与显示,请大家指点!多谢!!!
- 请问高手一个关于RAVE的打印问题,急呀,请求帮助.
- 一次Show出所有Form!!!!!!!!!!
- "cannot perform this opertation on a closed dataset",啥意思?
- 散分专注DELPHI分布式开发现招收版主
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=211955LYSoft上也有的
if (key=VK_F4) and ([ssALT]=shift) then key:=0;
就可以屏蔽ALT+F4了,
要屏蔽其它键原理一样
总是编译不能通过
[Error] Unit1.pas(30): Incompatible types