精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>C/C++>>一般性编程问题>>Windows消息:鼠标、键盘等>>如何在对话框中响应按键

主题:如何在对话框中响应按键
发信人: lightsabre()
整理人: wenbobo(2002-12-06 22:40:21), 站内信件
Windows sucks! 

The window procedure for a dialogbox catches all the arrow keys 
and the tab key to allow control navigation, but there is no way
to alter this behaviour.

The easiest way to catch the key codes is to use a keyboard hook .....

.

HHOOK ghKbrdHook = NULL;

LONG FAR PASCAL WindProcKeyboardHook
(int iCode, WPARAM wParam, LPARAM lParam) { 

   if (iCode < 0 || iCode != HC_ACTION)
return CallNextHookEx(ghKbrdHook, iCode, wParam, lParam);

// If this is a repeat or the key is being released, ignore it.
if (lParam & 0x80000000 || lParam & 0x40000000)
return CallNextHookEx(ghKbrdHook, iCode, wParam, lParam);

if (wParam == VK_F12)
{
AfxMessageBox("F12");

}

return CallNextHookEx(ghKbrdHook, iCode, wParam, lParam);

}


//install the hook somewhere, maybe in CYourDialog::OnInitDialog:

ghKbrdHook = SetWindowsHookEx(WH_KEYBOARD, WindProcKeyboardHook,
AfxGetInstanceHandle(), 0);



//before the application returns,
//maybe in the destructor of CYourDialog,
//add the following code to remove the hook:
if (ghKbrdHook!=NULL)
UnhookWindowsHookEx(ghKbrdHook);


--
※ 修改:.lightsabre 于 Oct  8 03:54:53 修改本文.[FROM: 204.170.177.231]
※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 204.170.177.231]

[关闭][返回]