【发布时间】:2014-07-21 21:38:07
【问题描述】:
所以我正在为一个班级项目制作一个带有 GUI 的计算器,我目前正在设置我的输入检测,以便我可以过滤掉我想要包含在用户输入中的按键以及我想要排除的按键.我一直在调试它,直到大约 10 年前,当传入的 wParam 值从十六进制值切换到十进制值时,它一直顺利进行。在以十六进制发送代码和现在在 12 月发送代码之间,我没有做任何重大更改,所以我无法弄清楚它为什么会改变,事实上,我相信在我给自己写完评论后它就改变了在我过滤消息的功能之一中,我很困惑为什么它会改变。相关代码发布在下面(我不能只是撤消我所做的更改,因为在某些时候我的 IDE 关闭了),如果有人能告诉我为什么它会改变,如何将它改回来,如果我什至过滤击键正确的方式,将不胜感激。
ShiftPressed 变量存储在使用此 WinProc 方法的类中,并且可供所有这些函数访问。下面的所有代码都在处理 wParam 以十六进制发送但在我完成 CheckKeyForOp() 方法中较长的注释后,wParam 开始在 12 月发送。
LRESULT CALLBACK EditBoxClass::WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_KEYUP:
if( wParam == VK_SHIFT )
ShiftPressed = FALSE;
return 0;
case WM_KEYDOWN:
if( wParam == VK_SHIFT )
{
ShiftPressed = TRUE;
return 1;
}
else if( CheckKeyForNum( wParam ) )
return 1;
else if( CheckKeyForOp( wParam ) )
return 1;
else
return 0;
...
}
BOOL EditBoxClass::CheckKeyForNum( WPARAM wParam )
{
switch( wParam )
{
case 0x30: case VK_NUMPAD0:
case 0x31: case VK_NUMPAD1:
case 0x32: case VK_NUMPAD2:
case 0x33: case VK_NUMPAD3:
case 0x34: case VK_NUMPAD4:
case 0x35: case VK_NUMPAD5:
case 0x36: case VK_NUMPAD6:
case 0x37: case VK_NUMPAD7:
case 0x38: case VK_NUMPAD8:
case 0x39: case VK_NUMPAD9:
default: return FALSE;
}
}
BOOL EditBoxClass::CheckKeyForOp( WPARAM wParam )
{
switch( wParam )
{
case VK_OEM_2: // For both of these keys, VK_OEM_2: "/ and ?" and VL_OEM_MINUS: "- and _" the
case VK_OEM_MINUS: // regular keystroke can be used in the calculator but the "second version"
if( ShiftPressed == TRUE ) return FALSE; // denoted by holding the shift key during the keystroke, cannot be used; so filter.
case VK_OEM_PLUS: // This key acts in the same way the VK_OEM_MINUS key does, having both "+/=" register under the
// VK code VK_OEM_PLUS, but both are operators used by the calculator so allow both
case VK_ADD: case VK_SUBTRACT:
case VK_MULTIPLY: case VK_DIVIDE:
case VK_DECIMAL: case VK_OEM_PERIOD: return TRUE;
default: return FALSE;
}
}
【问题讨论】:
标签: winapi input keyboard windows-messages