Many people know that if any button on the system-wide screen needs to use the global keyboard hook, but the "stubborn" button is not a normal keyboard hook like the WIN button. Here I provide a method using the underlying keyboard hook shielding any button (including the WIN button), and is made of a .dll dynamic link library for easy use. Hook is a relatively complicated technology that is often used to monitor a type of event in the system, which can be related to a thread (thread hook), or all threads in the system (global hook). Regarding the theory of hooks, I don't want to say too much, I can't say too much, because it is not a three words.
This article focuses on the application of the underlying keyboard hook, the 10 days of the 9CBS VB version, someone asks how to implement the WIN button, to be honest, this thing can be done with VB, only the hook function of the global hook must be written in standard DLL In, VB can only make standard DLLs through the change method, which is a bit trouble, so I have written a DLL with VC, so that VC, VB or Delphi, etc. can be called, and I also leave enough interface. , Will see later.
One thing must be declared, the underlying keyboard hook has a half-pointed shortcoming, that is, it can only be used in NT and above, but it is absolutely not in a small number of people in 2000, XP, 2003, and the people who use Longhorn in the future. It is estimated that it is less, so this is not too worried. :)
Ok, gossip less, source code here:
DLL header file (when using the function in the DLL in the VC, you need to include this header file, just like using the API to include Windows.h):
/ ************************************************** ****************** /
/ * File name: MaskKey.h * /
/ * * /
/ * Function: Standard DLL export function header file, contain this file in the program that uses this DLL * /
/ * * /
/ * Author: Lu Peipei (goodname008) Time: 2004.8.21 * /
/ * * /
/ * Blog: http://blog.9cbs.net/goodname008 * /
/ ************************************************** ****************** /
DECLSPEC_IMPORT
Bool
WinAPI
StartmaskKey
LPDWORD LPDWVIRTUALKEY,
INT NLENGTH,
Bool bdisableKeyboard = FALSE
);
DECLSPEC_IMPORT
Bool
WinAPI
STOPMASKKEY ();
DLL master file:
/ ************************************************** ****************** /
/ * File name: Maskkey.cpp * // * * /
/ * Function: Standard DLL ---- Using the underlying keyboard hook to achieve the blocking keyboard any button * /
/ * * /
/ * Author: Lu Peipei (goodname008) Time: 2004.8.21 * /
/ * * /
/ * Blog: http://blog.9cbs.net/goodname008 * /
/ ************************************************** ****************** /
/ / Export function list
// StartmaskKey
// stopmaskkey
#define _win32_winnt 0x0500 // Set the system version, make sure you can use the underlying keyboard hook
#include "windows.h"
// Global variable
LPDWORD G_LPDWVIRTUALKEY = NULL; / / KeyCode array pointer
INT g_nlength = 0; // KeyCode array size
BOOL g_bdisablekeyboard = false; // Whether to block the entire keyboard
Hinstance g_hinstance = null; // module instance handle
HHOOK G_HHOOK = NULL; // Hook handle
// DLL entry function
Bool Apientry Dllmain (Handle Hmodule, DWORD UL_REASON_FOR_CALL, LPVOID LPRESERVED)
{
/ / Save Module Instance Handle
G_HINSTANCE = (hinstance) hmodule;
/ / Unload hook at the end of the process or thread
Switch (ul_reason_for_call)
{
Case DLL_Process_attach:
Break;
Case DLL_THREAD_ATTACH:
Break;
Case DLL_PROCESS_DETACH:
Case DLL_THREAD_DETACH:
Delete g_lpdwvirtualkey;
IF (g_hhook! = null) UnHookWindowsHookex (g_hhook);
Break;
}
Return True;
}
// Bottom keyboard hook function
LResult Callback LowlevelKeyboardProc (int Ncode, WPARAM WPARAM, LPARAM LPARAM)
{
/ / Disable a button in the keyboard if g_bdisablekeyboard is true, or disable the entire keyboard
IF (ncode == hc_action)
{
IF (g_bdisableKeyboard) return true;
KBDLLHOOKSTRUCT * PSTRUCT = (KBDLLHOKSTRUCT *) LPARAM;
LPDWORD TMPVIRTUALKEY = g_lpdwvirtualkey; for (INT i = 0; i { IF (pstruct-> vkcode == * tmpvirtualkey ) Return True; } } // Pass the next hook in the system Return CallNexthookex (G_HHHOOK, NCODE, WPARAM, LPARAM); } / ************************************************** ****************** / / * Start Shielding Keyboard Buttons * / / * * / / * Parameters: * / / * LPDWVIRTUALKEY KeyCode array pointer * / / * NLENGTH Keycode array size * / / * BdisableKeyBoard is shielding the entire keyboard * / / * * / / * Return value: true success, False failed * / / ************************************************** ****************** / Bool WinApi StartmaskKey (LPDWord LPDWVIRTUALKEY, INT NLENGTH, BOOL BDISABLEKEYBOARD = FALSE) { // Return False if the keyboard hook has been installed IF (g_hhook! = null) Return False; // Save the keycode array from the user in the global variable g_lpdwvirtualkey = (lpdword) Malloc (Sizeof (DWORD) * NLENGTH); LPDWORD TMPVIRTUALKEY = G_LPDWVIRTUALKEY; For (int i = 0; i { * TMPVIRTUALKEY = * LPDWVIRTUALKEY ; } g_nlength = NLENGTH; g_bdisableKeyboard = bdisableKeyboard; / / Install the underlying keyboard hook g_hook = setwindowshookex (wh_keyboard_ll, loewlerybookardproc, g_hinstance, null); IF (g_hhook == null) Return False; Return True; } / ************************************************** ****************** / / * Stop Shielding Keyboard Buttons * / / * * // * Parameters: (none) * / / * * / / * Return value: true success, False failed * / / ************************************************** ****************** / Bool WinApi StopmaskKey () { // Uninstall the hook IF (unhookwindowshookex (g_hhook) == 0) Return False; g_hook = NULL; Return True; } DEF file (MaskKey.def): Exports StartmaskKey @ 1 StopMaskKey @ 2 The above is the main three files in the DLL project, and the engineering type is Win32 Dynamic-Link Library. As you can see from the DEF file, DLL has two export functions: StartmaskKey and StopMaskKey. StartmaskKey has three parameters. LPDWVIRTUALKEY is a pointer to the DWORD array. This DWORD array is used to store virtual-key code. NLENGTH is the size of this array. Calling StartmaskKey using the correct parameter, the DLL can shield each Virtual-Key Code in the DWORD array with the corresponding button corresponding to the keyboard, pressing these keys without reacting (including WIN). In fact, as long as a byte is a byte, the VKCode in the KBDLLHOOKSTRUCT structure is DWORD type, so I also use 4 bytes (DWORD) for unity. Even so, Microsoft stresses in MSDN, and the value of Virtual-Key Code must be a value between 1 and 254, which must be noted. StopmaskKey does not have a parameter, indicating the stop shielding keyboard button. If you do not call StopMaskKey in the program to stop the mask automatically when the process or thread exits, restore the original state. Of course, the processes and threads must be properly exited. If it is made by other programs to end the process or thread, it is not good to end the process or thread, if you are not recommended by Microsoft, such as TerminateProcess or TerminateThread, etc. () The following is an example of calling in the VC: (two Dialog member functions, corresponding two buttons) Void CMaskKeyAppdlg :: onstartmaskKey () { // Shield a, b, c, upper, lower left, left, right and two WIN keys DWORD DWVK [] = {'A', 'B', 'C', VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN, VK_LWIN, VK_RWIN}; INT NLENGTH = SIZEOF (DWVK) / SizeOf (DWORD); StartmaskKey (DWVK, NLENGTH); Void CMaskKeyAppdlg :: onstopmaskKey () { STOPMASKKEY (); } Here is an example in which you call in VB: (2 CommandButton added on the form, and renamed cmdstartmask and cmdstopmask, respectively) Option expedition Private Declare Function StartmaskKey Lib "Maskkey" (LPDWVIRTUALKEY As Long, Byval NLENGTH AS Long, Optional Byval BdisableKeyboard As Boolean = FALSE) AS Long Private Declare Function StopmaskKey Lib "Maskkey" () AS Long Private sub cmdstartmask_click () 'Shield a, b, c, upper, bottom, left, right and two WIN keys DIM Key (8) as long Key (0) = VBKEYA Key (1) = VBKEYB Key (2) = VBEYC Key (3) = VBEYLEFT Key (4) = VBEYRIGHT Key (5) = VBEYUP Key (6) = VBKEYDOWN Key (7) = & H5B 'left WIN button Key (8) = & h 5C 'WIN button on the right StartmaskKey Key (0), Ubound (Key) 1 End Sub Private sub cmdstopmask_click () StopmaskKey End Sub For VB, if you press the F5 launch in the VB IDE environment, you must call STOPMASKKEY to restore the status of the keyboard. If there is no call, you will recover the keyboard status when exiting the VB's IDE environment. For VB programs that are independently executed after compiling, the same as the VC compiled program, whether it is called StopMaskKey, will automatically uninstall the hook by DLL when the program exits, and restores the keyboard state. In fact, the hook is not very profound, I wrote this DLL's purpose, and for us to use "brought" when we use it later. Download address of DLL source code and VC and VB call routines: http://9cbsgoodname008.51.net/maskkey.zip * ------------------------------------------- * * Please inform the author and indicate the source, 9CBS welcomes you! * * Author: Lu Peipei (goodname008) * * Email: GoodName008@163.com * * Column: http://blog.9cbs.net/goodname008 * * ------------------------------------------- *