How to shield the system hotkey

zhaozj2021-02-08  363

How to shield the system hotkey

In the process of program development, in order to achieve some special requirements, it is sometimes necessary to shield the system hotkey. These system hot keys include Alt Tab, Ctrl Alt Del, Alt Esc, Ctrl Esc, WIN key, etc. Since the shield system hotkey involves the security of the system, Microsoft does not recommend this, we can only explore it in some unapproved documents. Below we are divided into three cases to discuss the method of shielding system hotkeys.

First, under Win95 / Win98 operating system:

The blocking system hotkey under Win95 / Win98 operating system can be done with a very simple method, which is to let the system think that the current is currently in a state. Let the system think that the current mode is:

SystemParametersInfo (SPI_SETSCREENSAVERRUNNING, TRUE, & POLD, SPIF_UPDATEINIFILE);

SPI_SETSCREENSAVERRUNNING This parameter is not recommended in Microsoft's documentation, because in this state, the system cannot pop-up task list, once a process is suspended, can only restart, it is possible to cause data loss, but can reach the required Effect, we still use it.

When the program is to be canceled:

SystemParametersInfo (SPI_SETSCREENSAVERRUNNING, FALSE, & POLD, SPIF_UPDATEINIFILE);

Pold is the Boolean variable we define.

Second, under WinNT 4.0 Service Pack 3 or Win2000

Maybe you think that using a keyboard hook wh_keyboard does not solve the problem? The problem is not that simple. This is because the keyboard hook wh_keyboard does not intercept the input to the system key! Fortunately, in WinNT4.0 Service Pack3 or Win2000, it provides us with a bottom system hook wh_keyboard_ll. WH_KEYBOARD_LL workflow as shown in the right:

From the right figure we can see that the underlying keyboard hook exists between the user tap keyboard and system processing, and the normal keyboard hook exists after the system generates WM_Keyxxx. It is clear that the ordinary keyboard hook can only intercept the WM_KEYXXX message without operating the system key. However, the underlying keyboard hook has a fatal weakness, which is if the process or thread is called, the system will not process any keyboard operations. In order to solve this problem, Microsoft gives a limited time for a bottom keyboard hook processing in the registry, and if it exceeds this time, the system will process it. The key value of this time is given under the hkey_current_user / control panel / desktop / loWevelHookstimeout of the registry.

Let's discuss the usage of the underlying keyboard hook. First need to press the hook, this requires an API function:

HHOOK SETWINDOWSHOKEX

Int iHOOKCODE,

HookProc LPFN,

Hinstance hmodule,

DWORD DWTHREADID

);

Where the first parameter is the type of hook; the second parameter is the address of the hook function; the third parameter is a module handle including the hook function; the fourth parameter specifies the monitoring thread. If the determined thread is specified, it is a thread dedicated hook; if specified is empty, it is a global hook. Where the global hook function must be included in the DLL (dynamic link library), and the thread dedicated hook can also be included in the executable. The hook function obtained by the control is after the processing of the message, if you want the message to continue to pass, it must call the API function callNexthooKex in another SDK to pass it. The hook function can also discard the message by directly returning True and blocks the passage of the message. Below is a complete source code for realizing the underlying keyboard hook, which is debugged under VC5.0 and VC6.0.

#include

LResult Callback LowlevelKeyboardProc (int Ncode,

WPARAM WPARAM, LPARAM LPARAM) {

Bool featkeyStroke = false;

I f (ncode == hc_Action) {

Switch (wparam) {

Case WM_KeyDown: Case WM_SYSKEYDOWN:

Case WM_KEYUP: Case WM_SYSKEYUP:

PkbdllhookStruct P = (pkbdllhookingtruct) 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 PSZCMDLINE, INT) {

/ / Press the underlying keyboard hook

Hhook hhklowlevelkybd = setwindowshookex (wh_keyboard_ll,

LowlevelKeyboardProc, hinstexe, 0);

Messagebox (NULL,

TEXT ("Alt ESC, Ctrl ESC, And Alt Tab Are Now Disabled./N")

TEXT ("Click /" OK / "To Terminate this Application and Re-enable

"),

Text ("Disable Low-Level Keys", MB_OK);

UnHookWindowsHookex (HHKLOWLEVELKYBD);

Return (0);

}

Third, under the NT operating system below Winnt 4.0 Service Pack 2 or lower version

Unfortunately, in the NT operating system below Winnt 4.0 Service Pack 2 or lower, we can do without shielding all system hot keys, and only shields Alt Tab, Alt Esc. The method of completing this feature is to use the RegisterhotKey function. After calling this function, your process will be notified before the ALT TAB is pressed. The message you need to handle is WM_HOTKEY, below is the relevant code explanation.

RegisterhotKey function original and instructions:

Bool registerhotkey

HWND HWND, // WINDOW TO receive hot-key notification

INT ID, // Identifier Of Hot Key

Uint fsmodifiers, // key-modifier flags

Uint vk // virtual-key code);

The parameter ID is a ID value that you define your own, and the value is required for a thread. It must be within the range of 0x0000 - 0xBFFF. It is necessary to in the 0xC000 - 0xFFF range, and this value must be unique within the same process. .

Parameters fsmodifiers indicate that the button is combined with the hotkey, which can be valued: mod_alt mod_control mod_win mod_shift.

Parameter VK specifies the virtual key code of the hotkey.

// Initialization

CMAINFRAME :: CMAINFRAME ()

{

m_nhotKeyId = 100;

Bool m_iskeyregister = registerhotkey (getsafehwnd (), m_nhotkeyid,

MOD_ALT, VK_TAB);

Assert (m_iskeyregistered! = False);

}

//cancel

CMAINFRAME :: ~ cmainframe ()

{

Bool m_iskeyunregistered = unregisterhotkey (getsafehwnd (), m_nhotkeyid;

Assert (m_iskeyunundered! = False);

}

WM_HOTKEY message meaning:

IDHOTKEY = (int) WPARAM; // Identifier of Hot Key

Fumodifiers = (uint) Loword (LPARAM); // Key-Modifier Flags

Uvirtkey = (uint) HiWord (LPARAM); // Virtual-Key Code

These three values ​​correspond to the ID, FSMODIFIERS, VK when recisterhotKey, respectively.

Finally, you need to define an ON_MESSAGE message mapping in a file.

In the header file:

Class CMAINFRAME: PUBLIC XXXX

{

AFX_MSG Long OnhotKey (WPARAM WP, LPARAM LP);

}

Add: Message_MAP in the CPP file:

ON_MESSAGE (WM_HOTKEY, INHOTKEY)

This hotkey has been valid at the time of your process, and its status will be restored after the process is completed.

Through the analysis of the above three cases, we basically cover all methods of the shielding system hotkey, please contact zhangyunchao@citiz.net.

转载请注明原文地址:https://www.9cbs.com/read-782.html

New Post(0)