The following describes the API functions used:
Function setwindowshookex (idhook: integer; lpfn: tfnhookproc; hmod: hinst; dwthreadid: DWORD): hhook; stdcall;
Function: Set a hook. If a fixed thread is specified, only the thread is monitored, that is, the thread hook. If it is null, it is the global hook that monitors all threads.
Parameter: the first parameter is of the hook type;
The second parameter is the address of the hook function;
The third parameter is the module handle that contains the hook function;
The fourth parameter specifies the monitored thread;
Return Value:
If the handle of the hook is successfully returned, false is returned if the hook fails.
Function unhookwindowshookex (HHK: hhook): bool; stdcall;
Function: Uninstall a hook.
Parameter: handle of the hook to be detached.
Return Value: Success is true, failure is false.
Function getkeystate (optional rtkey: integer): short; stdcall;
Function: obtain the status of a key on the keyboard.
Parameter: Specifies the virtual key value of the key.
Return Value: A 16-digit number. If the value is pressed, the highest bit of the returned value is 1, that is, hexadecimal 800000. If the value is not pressed, 0 is returned.
For more details, see the Win32 SDK manual provided by Delphi.
I will not talk more about it. If I say more, I will deviate from the focus. If you don't even use hook functions, refer to my previous article "learn to use hook functions".
The procedure is as follows:
Remember to place two button controls on form1 and set the corresponding mouse click event.
Unit unit1;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls;
Type
Tform1 = Class (tform)
Button1: tbutton;
Button2: tbutton;
Label1: tlabel;
Procedure button1click (Sender: tobject );
Procedure button2click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;
{The structure of the key message is not found in Delphi. Define it yourself. That's why I use C to write
One of the reasons why such a program is better. Note that this structure is applicable to Windows NT 4 SP3 and later systems.
Can be used}
Tagkbdllhookstruct = packed record
Vkcode: DWORD; // virtual key value
Scancode: DWORD; // scan code value (I do not know ^_^)
{Some extension labels. This value is troublesome and I don't quite understand it on msdn,
According to this program, when the sixth digit (Binary) of the flag value is 1, the Alt key press is 0 .}
Flags: DWORD;
Time: DWORD; // message Timestamp
Dwextrainfo: DWORD; // message-related extended information
End;
KBDLLHOOKSTRUCT = tagkbdllhookstruct;
Pkbdllhookstruct = ^ KBDLLHOOKSTRUCT;
// This is the index value of the low-level keyboard hook, which is not in Delphi and must be defined by yourself
Const wh_keyboard_ll = 13;
// Compare a constant with the flags in the above structure to determine whether the Alt key is pressed
Const llkhf_altdown = $20;
VaR
Form1: tform1;
Hhklowlevelkybd: hhook;
Implementation
{
Function: callback function for low-level keyboard hooks to filter messages.
Parameter: ncode is the hook flag
Wparam indicates the message type.
Lparam is a pointer to which structure we defined above KBDLLHOOKSTRUCT
Return Value: if it is not 0, Windows will discard the message and the program will no longer receive the message.
}
Function lowlevelkeyboardproc (ncode: integer;
Wparam: wparam; lparam: lparam): lresult; stdcall;
VaR
Featkeystroke: bool;
P: pkbdllhookstruct;
Begin
Result: = 0;
Featkeystroke: = false;
P: = pkbdllhookstruct (lparam );
// When the ncode value is hc_action, it indicates that the wparam and lparam parameters include the key message.
If (ncode = hc_action) then
Begin
// Intercept the key message and test whether the key is Ctrl + ESC, ALT + tab, and ALT + ESC.
Case wparam
Wm_keydown,
Wm_syskeydown,
Wm_keyup,
Wm_syskeyup:
Featkeystroke: =
(P. vkcode = vk_tab) and (P. flags and llkhf_altdown) <> 0) or
(P. vkcode = vk_escape) and (P. flags and llkhf_altdown) <> 0) or
(P. vkcode = vk_escape) and (getkeystate (vk_control) and $8000) <> 0 ));
End;
End;
If featkeystroke = true then
Result: = 1;
If ncode <> 0 then
Result: = callnexthookex (0, ncode, wparam, lparam );
End;
{$ R *. DFM}
Procedure tform1.button1click (Sender: tobject );
Begin
// Set a low-level keyboard hook
If hhklowlevelkybd = 0 then
Begin
Hhklowlevelkybd: = setwindowshookexw (wh_keyboard_ll,
Lowlevelkeyboardproc, hinstance, 0 );
If hhklowlevelkybd <> 0 then
MessageBox (handle, 'low-level keyboard hook set successful! ',' Hint ', mb_ OK)
Else
MessageBox (handle, 'lower-level keyboard hook setting failed! ',' Hint ', mb_ OK );
End
Else
MessageBox (handle, 'low-level keyboard hook already set! ',' Hint ', mb_ OK );
End;
Procedure tform1.button2click (Sender: tobject );
Begin
// Uninstall the low-level keyboard hook
If hhklowlevelkybd <> 0 then
If unhookwindowshookex (hhklowlevelkybd) <> false then
Begin
MessageBox (handle, 'low-level keyboard hook unmounted successfully! ',' Hint ', mb_ OK );
Hhklowlevelkybd: = 0;
End
Else
MessageBox (handle, 'failed to uninstall the low-level keyboard hook! ',' Hint ', mb_ OK)
Else
MessageBox (handle, 'no low-level keyboard Hook found! ',' Hint ', mb_ OK );
End;
Procedure tform1.formclose (Sender: tobject; var action: tcloseaction );
Begin
// When the form is disabled, it is detected. If the hook is not uninstalled, it will be uninstalled.
If hhklowlevelkybd <> 0 then
Unhookwindowshookex (hhklowlevelkybd );
End;
End.
The program is compiled and tested in DELPHI6 + Windows2000 SP2.
Shield
VaR
Temp: integer;
Begin
Systemparametersinfo (spi_screensaverrunning, 1, @ temp, 0 );
End;
Restore
VaR
Temp: integer;
Begin
Systemparametersinfo (spi_screensaverrunning, 0, @ temp, 0 );
End;