所以我正在制作一个带有类项目GUI的计算器,我正在设置我的输入检测,所以我可以过滤掉我希望包含在用户输入中的键盘,以及我想要排除哪些。我一直在调试它一直顺利进行直到大约10次符合以前,当传递的wParam值从十六进制值切换为十二进制值时。我在发送十六进制代码和现在它在十二月发送的时间之间没有做任何重大更改,所以我无法弄清楚它为什么会改变,事实上我相信它在我写完评论给自己后发生了变化在我正在过滤消息的一个函数中,所以我很困惑为什么它改变了。相关代码发布在下面(我不能撤消我所做的更改,因为在某些时候我的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;
}
}
答案 0 :(得分:1)
消息不会给你十六进制/十进制,它们只给你二进制数,然后你可以根据需要解释为十六进制/十进制数。
我在此代码中看到的唯一错误是CheckKeyForNum()
对于允许的数字键没有返回TRUE:
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:
return TRUE; // <-- add this
default: return FALSE;
}
}