Some time ago I have written a "use of the underlying keyboard hook screen any button", and put it on my blog. The title of this article has changed "shielding" to "intercept", which is obviously stronger than the previous version. For the DLL that was written previously, there's a less-than-ideal place where you can only mask. If you want to add some "little gestures" before masking, you can only modify DLLs, add code to the LowLevelKeyboardProc function, and implement new functionality. But this is obviously not flexible enough, such a DLL is not a general. So I naturally think of callbacks, there are many APIs in Windows that need callback functions, and we can of course write APIs like this, and the benefit of this is that we can leave enough interfaces for the DLL to invoke the program. At this point, the DLL is like a valve, we do not care about the key message to put it in the past, only we care about the key message to intercept, and then further processing, and these processing code is written in the DLL invoke the callback function, this is the ideal.
As opposed to the previous version, the modified DLL source code is as follows:
/********************************************************************/< br/>/* filename: MaskKey.cpp */
/* */
/* Function: Standard DLL----Use the bottom of the keyboard hook to achieve the interception of keyboard arbitrary keys * * *
/* */
/* Author: Lupeper (goodname008) Time: 2005.1.18 * *
/* */
* blog:http://blog.csdn.net/goodname008 * *
/********************************************************************/
Export Function List
Startmaskkey
Stopmaskkey
#define _WIN32_WINNT 0x0500//Set the system version to ensure that the underlying keyboard hooks are available
#include "Windows.h"
callback function pointer
typedef BOOL (callback* Lpfnkeyboardproc) (WPARAM, kbdllhookstruct*);
Global variables
Lpdword G_lpdwvirtualkey = NULL; Pointers to KeyCode arrays
int g_nlength = 0; Size of keycode array
BOOL G_bdisablekeyboard = FALSE; Whether to block the entire keyboard
HINSTANCE g_hinstance = NULL; Module instance handle
Hhook G_hhook = NULL; Hook handle
Lpfnkeyboardproc G_lpfnkeyboardproc; Keyboard Hook callback function pointer
DLL entry function
BOOL apientry DllMain (HANDLE hmodule, DWORD ul_reason_for_call, LPVOID lpreserved)
{
Save a module instance handle
G_hinstance = (hinstance) hmodule;
Uninstall the hook at the end of the process or at the end of the thread
Switch (Ul_reason_for_call)
{
Case Dll_process_attach:
Break
Case Dll_thread_attach:
Break
Case Dll_process_detach:
Case Dll_thread_detach:
Free (G_lpdwvirtualkey);
if (G_hhook!= NULL) UnhookWindowsHookEx (G_hhook);
Break
}
return TRUE;
}
Underlying keyboard hook function
LRESULT CALLBACK lowlevelkeyboardproc (int ncode, WPARAM WPARAM, LPARAM LPARAM)
{
Blocks some keys on the keyboard and blocks the entire keyboard key if G_bdisablekeyboard is TRUE
if (ncode >= hc_action)
{
kbdllhookstruct* pstruct = (kbdllhookstruct*) LParam;
if (G_bdisablekeyboard)
if (G_lpfnkeyboardproc (WParam, pstruct))
Return CallNextHookEx (G_hhook, Ncode, WParam, LParam);
Else
return true;
Lpdword Tmpvirtualkey = G_lpdwvirtualkey;
for (int i = 0; i < g_nlength; i++)
{
if (Pstruct->vkcode = = *tmpvirtualkey++)
if (G_lpfnkeyboardproc (WParam, pstruct))
Return CallNextHookEx (G_hhook, Ncode, WParam, LParam);
Else
return true;
}
}
Call the next hook in the system
Return CallNextHookEx (G_hhook, Ncode, WParam, LParam);
}
/********************************************************************/
/* Start intercepting keyboard keys/
/* */
/* Parameters: */
/* Lpdwvirtualkey keycode array pointer/*
/* nlength keycode Array Size * *
/* Bdisablekeyboard whether to intercept the entire keyboard * *
/* */
/* Return value: TRUE success, FALSE failure
/********************************************************************/
BOOL WINAPI Startmaskkey (lpdword lpdwvirtualkey, int nlength,
Lpfnkeyboardproc Lpfnkeyboardproc, BOOL bdisablekeyboard = FALSE)
{
Returns FALSE if the keyboard hook is already installed
if (g_hhook!= NULL | | | nlength = 0) return FALSE;
Save the KeyCode array from the user in a global variable
G_lpdwvirtualkey = (lpdword) malloc (sizeof (DWORD) * nlength);
Lpdword Tmpvirtualkey = G_lpdwvirtualkey;
for (int i = 0; i < nlength; i++)
{
*tmpvirtualkey++ = *lpdwvirtualkey++;
}
G_nlength = nlength;
G_bdisablekeyboard = Bdisablekeyboard;
G_lpfnkeyboardproc = Lpfnkeyboardproc;
Install the underlying keyboard hook
G_hhook = SetWindowsHookEx (Wh_keyboard_ll, LowLevelKeyboardProc, g_hinstance, NULL);
if (G_hhook = NULL) return FALSE;
return TRUE;
}
/********************************************************************/
/* Stop intercepting keyboard keys
/* */
/* Parameter: (none) */
/* */
/* Return value: TRUE success, FALSE failure
/********************************************************************/
BOOL WINAPI Stopmaskkey ()
{
Uninstall Hook
if (UnhookWindowsHookEx (g_hhook) = = 0) return FALSE;
G_hhook = NULL;
return TRUE;
}