键盘事件回调函数声明:
LRESULT CALLBACK KeyboardHookProc(int nCode,WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
switch (wParam)
{
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
//case WM_KEYUP:
//case WM_SYSKEYUP:
{
if (p->vkCode>=0x30&&p->vkCode<=0x39){
// 数字0~9
}
else if(p->vkCode>=0x41&&p->vkCode<=0x5A){
// 字母a~z
}
if (p->vkCode>=0x61&&p->vkCode<=0x7A){
// 字母A~Z
}
//屏蔽
else if ((p->vkCode == VK_TAB) && ((p->flags & LLKHF_ALTDOWN) != 0)){
// ALT+TAB
}
//屏蔽
else if ((p->vkCode == VK_ESCAPE) && ((p->flags & LLKHF_ALTDOWN) != 0)){
// ALT+ESC
}
//屏蔽
else if ((p->vkCode == VK_ESCAPE) && ((GetKeyState(VK_CONTROL) & 0x8000) != 0)){
// CTRL+ESC
}
else if ((p->vkCode == VK_ESCAPE) &&
((GetKeyState(VK_CONTROL) & 0x8000) != 0) &&
((GetKeyState(VK_SHIFT) & 0x8000) != 0)){
// CTRL + SHIFT + ESC
}
else if (p->vkCode == VK_LWIN || p->vkCode == VK_RWIN){
// 左右WIN键
}
//此处无法屏蔽CTRL+ALT+DEL,已在SAS window中屏蔽
else if ((p->vkCode == VK_DELETE) &&
((GetKeyState(VK_CONTROL) & 0x8000) != 0) &&
((GetKeyState(VK_MENU) & 0x8000) != 0 )){
// CTRL+ALT+DEL
}
break;
}
}
}
return ::CallNextHookEx(hKeyboardHook, nCode, wParam, lParam); // 消息传递,否则键盘就不起作用了
}
注册键盘钩子回调函数:
HHOOK hKeyboardHook = ::SetWindowsHookEx(WM_HOUSE_LL, KeyboardHookProc, NULL, 0);
卸载钩子:
::UnhookWindowsHookEx(hMousehook);