如何通过扫描码(而不是虚拟键码)获得关键状态?

反正有没有使用扫描码来获得键盘按键的状态(是closures还是closures)? 我找不到在win32中的任何function。 任何人都知道任何方式来实现这个

PS我需要键盘的实际状态,而不是从GetKeyState返回的Windows消息派生的状态。

Solutions Collecting From Web of "如何通过扫描码(而不是虚拟键码)获得关键状态?"

您将不得不使用MapVirtualKey函数,使用MAPVK_VSC_TO_VK作为模式,并将输出传递给GetKeyStateGetKeyboardState ,因为没有任何WinAPI密钥函数直接使用扫描代码

也许你正在寻找GetAsyncKeyState 。 它从GetKeyState文档引用,似乎返回您所期望的。

MapVirtualKey不会翻译很多密钥,所以我想出了这个开关:

 int main() { directinput = new DirectInput(); directinput->init(); while(1) { int ret = directinput->ReadKeyboard(); if(!ret) continue; int keys_pressed = 0; for(int i = 0; i < 256; i++) { if((directinput->m_keyboardState[i] & 128) == 0) continue; unsigned char scancode = i; UINT key = MapVirtualKey(scancode, MAPVK_VSC_TO_VK_EX); //UINT key = MapVirtualKeyEx(scancode, MAPVK_VSC_TO_VK, GetKeyboardLayout(0)); // same as MapVirtualKey switch(scancode) { case 203: key = VK_LEFT; break; case 205: key = VK_RIGHT; break; case 200: key = VK_UP; break; case 208: key = VK_DOWN; break; case 211: key = VK_DELETE; break; case 207: key = VK_END; break; case 199: key = VK_HOME; break; // pos1 case 201: key = VK_PRIOR; break; // page up case 209: key = VK_NEXT; break; // page down case 210: key = VK_INSERT; break; case 184: key = VK_RMENU; break; // right alt case 157: key = VK_RCONTROL; break; // right control case 219: key = VK_LWIN; break; // left win case 220: key = VK_RWIN; break; // right win case 156: key = VK_RETURN; break; // right enter case 181: key = VK_DIVIDE; break; // numpad divide case 221: key = VK_APPS; break; // menu key } printf("keys_pressed=%d scancode=%d/0x%x key=%d char=%c hex=0x%x\n", keys_pressed, scancode, scancode, key, key, key); keys_pressed++; } Sleep((int)(1000.0 / 60.0)); } return 0; }