2011-02-07 34 views
2

無論如何,只要使用掃描碼就可以獲得鍵盤按鍵的狀態(是按下還是按下)?我無法在win32中找到任何函數。任何人都知道任何方式來實現這一目標如何通過掃描碼(非虛擬鍵碼)獲取關鍵狀態?

p.s.我需要鍵盤的實際狀態,而不是從GetKeyState返回的Windows消息派生的狀態。

回答

2

你將不得不使用MapVirtualKey功能,採用MAPVK_VSC_TO_VK爲模式,並通過輸出到GetKeyStateGetKeyboardState,因爲沒有的WinAPI的主要功能直接使用掃描碼

0

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; 
}