In Windows, there are usually few ways to interrupt some system events. the mechanism we first consider is a hook, because Microsoft provides comprehensive support for the hook, which also exists in the system so that it can interrupt an event. if the hook department works, I will consider API hook. microsoft does not want to officially support API hook, so if possible, I will try to avoid using API hook. fortunately, this special problem can be solved by setting the hook. in window nt 4.0 Service Pack 3, Microsoft added a low-level keyboard hook for the system, wh_keyboard_ll .. generally, the advanced keyboard hook and wh_keyboard are intercepted when a thread's message queue is removed. wh_keyboard Hook has a higher priority than most applications. however, some keys are not directly sent to the Message Queue of the thread. CTRL + ESC and ALT + TAB are good examples. these keys are internally processed in the raw input thread of the system. because the application cannot receive these messages, the application cannot intercept them to protect normal processes. this behavior enables the user to switch to another application, regardless of whether the application thread is already in a loop or suspended.
Of course, only a few applications really need to intercept these messages. to adapt to these applications, Microsoft introduced a wh_keyboard_ll hook. this hook will be used for processing after user input and before the system receives them. however, this hook has another serious drawback: it may cause infinite loops or suspension. if this happens, the system will not be able to process the key message correctly.
To alleviate this pain, Microsoft put a time limit on this hook. when the system sends a notification to a filter function of a low-level keyboard Hook, the system will give this function some time to execute. if no response is returned within the allowed time, the system ignores it. and perform normal processing. this time is implemented through the lowlevelhookstimeout value (HKEY_CURRENT_USER/control panel/desktop)
/*************************************** *********************************
Module: disablelowlevelkeys. cpp
Notices: Written 2000 Jeffrey Richter
**************************************** **********************************/
# DEFINE _ win32_winnt 0x0400
# Include <windows. h>
//////////////////////////////////////// /////////////////////////////////
Lresult callback lowlevelkeyboardproc (INT ncode,
Wparam, lparam ){
Bool featkeystroke = false;
If (ncode = hc_action ){
Switch (wparam ){
Case wm_keydown: Case wm_syskeydown:
Case wm_keyup: Case wm_syskeyup:
Pkbdllhookstruct P = (pkbdllhookstruct) lparam;
Featkeystroke =
(P-> vkcode = vk_tab) & (P-> flags & llkhf_altdown )! = 0) |
(P-> vkcode = vk_escape )&&
(P-> flags & llkhf_altdown )! = 0) |
(P-> vkcode = vk_escape) & (getkeystate (vk_control )&
0x8000 )! = 0 ));
Break;
}
}
Return (featkeystroke? 1: callnexthookex (null, ncode, wparam,
Lparam ));
}
//////////////////////////////////////// /////////////////////////////////
Int winapi winmain (hinstance hinstexe, hinstance, ptstr psz1_line, INT ){
// Install the low-level keyboard & mouse hooks
Hhook hhklowlevelkybd = setwindowshookex (wh_keyboard_ll,
Lowlevelkeyboardproc, hinstexe, 0 );
// Keep this app running until we're re told to stop
MessageBox (null,
Text ("Alt + ESC, CTRL + ESC, and ALT + TAB are now disabLed./N ")
Text ("click/" OK/"to terminate this application and re-enable
These keys ."),
Text ("Disable low-level keys"), mb_ OK );
Unhookwindowshookex (hhklowlevelkybd );
Return (0 );
}
/// // End of file //////// ///////////////////////