45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:在Dialog程序中使用WH_KEYBOARDWHLL类型HOOK的步骤

在Dialog程序中使用WH_KEYBOARDWHLL类型HOOK的步骤

2016-08-30 14:28:27 来源:www.45fan.com 【

在Dialog程序中使用WH_KEYBOARDWHLL类型HOOK的步骤

在Dialog程序中使用WH_KEYBOARD_LL类型HOOK的方法:

SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)LowLevelKeyboardProc, AfxGetApp()->m_hInstance, NULL);

LRESULT CALLBACK LowLevelKeyboardProc (INT nCode, WPARAM wParam, LPARAM lParam)
{
// By returning a non-zero value from the hook procedure, the
// message does not get passed to the target window

KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam;
BOOL bControlKeyDown = 0;

switch (nCode)
{
case HC_ACTION:
{
// Check to see if the CTRL key is pressed
bControlKeyDown = GetAsyncKeyState (VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1);

// Disable CTRL+ESC
if (pkbhs->vkCode == VK_ESCAPE && bControlKeyDown)
return 1;

// Disable ALT+TAB
if (pkbhs->vkCode == VK_TAB && pkbhs->flags & LLKHF_ALTDOWN)
return 1;

// Disable ALT+ESC
if (pkbhs->vkCode == VK_ESCAPE && pkbhs->flags & LLKHF_ALTDOWN)
return 1;

// Response to Up
if ((pkbhs->vkCode == VK_UP) && (WM_KEYUP == wParam))
{
HWND hwnd=::FindWindow(NULL, "WindowCaption");
::PostMessage(hwnd, WM_COMMAND, ID_MENU_ITEM, 0L);
}
}
default:
break;
}
return CallNextHookEx (NULL, nCode, wParam, lParam);
}

BOOL InstallKbHook( )
{

if (hhookKb )
UninstallKbHook();

hhookKb = SetWindowsHookEx(WH_KEYBOARD_LL,
(HOOKPROC)LowLevelKeyboardProc, AfxGetApp()->m_hInstance, NULL);

return(hhookKb != NULL);

}

BOOL UninstallKbHook()
{

BOOL fOk = FALSE;
if (hhookKb ) {
fOk = UnhookWindowsHookEx(hhookKb );
hhookKb = NULL;
}

return(fOk);
}

 

本文地址:http://www.45fan.com/dnjc/69880.html
Tags: 程序 dialog KEYBOARD
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部