void ConvertKeyToCurrentKeyboard(char* sNewKey, char* sOldKey, int nNewKeyMaxSize) { if (_mbstrlen(sOldKey) == 1) { HKL nDefaultKeyboard = GetKeyboardLayout(0); SHORT nCharCode = sOldKey[0]; if ((nCharCode > 0) && (nCharCode <= 255)) { SHORT nScanCode = g_pEnglishCharToScanCodeTable[nCharCode]; if (nScanCode != -1) { SHORT nKeyCode2 = MapVirtualKeyEx(nScanCode & 0xff, 1, nDefaultKeyboard); if (nKeyCode2 != -1) { UINT nNewChar = MapVirtualKeyEx( nKeyCode2 & 0xff, 2, nDefaultKeyboard); if (nNewChar != 0) { sNewKey[0] = (char)nNewChar; sNewKey[1] = '\0'; return; } } } } } _mbsncpy((unsigned char*)sNewKey, (const unsigned char*)sOldKey, nNewKeyMaxSize); sNewKey[nNewKeyMaxSize-1] = '\0'; }
// If we are using a non-qwerty layout, find the localized key that matches this letter's position in qwerty. int TranslateKeyFromQwerty(int iKey) { HKL iCurrent = GetKeyboardLayout(0); if (iCurrent == g_iEnglish) return iKey; UINT i = MapVirtualKeyEx(GetVKForChar(iKey), MAPVK_VK_TO_VSC, g_iEnglish); return (int)GetCharForVK(MapVirtualKeyEx(i, MAPVK_VSC_TO_VK, iCurrent)); }
static SDL_keysym *TranslateKey(WPARAM vkey, UINT scancode, SDL_keysym *keysym, int pressed) { /* Set the keysym information */ keysym->scancode = (unsigned char) scancode; keysym->mod = KMOD_NONE; keysym->unicode = 0; if ((vkey == VK_RETURN) && (scancode & 0x100)) { /* No VK_ code for the keypad enter key */ keysym->sym = SDLK_KP_ENTER; } else { keysym->sym = VK_keymap[SDL_MapVirtualKey(scancode, vkey)]; } if ( pressed && SDL_TranslateUNICODE ) { #ifdef NO_GETKEYBOARDSTATE /* Uh oh, better hope the vkey is close enough.. */ if((keysym->sym == vkey) || (vkey > 0x7f)) keysym->unicode = vkey; #else BYTE keystate[256]; Uint16 wchars[2]; GetKeyboardState(keystate); /* Numlock isn't taken into account in ToUnicode, * so we handle it as a special case here */ if ((keystate[VK_NUMLOCK] & 1) && vkey >= VK_NUMPAD0 && vkey <= VK_NUMPAD9) { keysym->unicode = vkey - VK_NUMPAD0 + '0'; } else if (SDL_ToUnicode((UINT)vkey, scancode, keystate, wchars, sizeof(wchars)/sizeof(wchars[0]), 0) > 0) { keysym->unicode = wchars[0]; } #endif /* NO_GETKEYBOARDSTATE */ } #if 0 { HKL hLayoutCurrent = GetKeyboardLayout(0); int sc = scancode & 0xFF; printf("SYM:%d, VK:0x%02X, SC:0x%04X, US:(1:0x%02X, 3:0x%02X), " "Current:(1:0x%02X, 3:0x%02X)\n", keysym->sym, vkey, scancode, MapVirtualKeyEx(sc, 1, hLayoutUS), MapVirtualKeyEx(sc, 3, hLayoutUS), MapVirtualKeyEx(sc, 1, hLayoutCurrent), MapVirtualKeyEx(sc, 3, hLayoutCurrent) ); } #endif return(keysym); }
static SDL_keysym *TranslateKey(WPARAM vkey, UINT scancode, SDL_keysym *keysym, int pressed) { keysym->scancode = (unsigned char) scancode; keysym->mod = KMOD_NONE; keysym->unicode = 0; if ((vkey == VK_RETURN) && (scancode & 0x100)) { keysym->sym = SDLK_KP_ENTER; } else { keysym->sym = VK_keymap[SDL_MapVirtualKey(scancode, vkey)]; } if ( pressed && SDL_TranslateUNICODE ) { #ifdef NO_GETKEYBOARDSTATE if((keysym->sym == vkey) || (vkey > 0x7f)) keysym->unicode = vkey; #else BYTE keystate[256]; Uint16 wchars[2]; GetKeyboardState(keystate); if ((keystate[VK_NUMLOCK] & 1) && vkey >= VK_NUMPAD0 && vkey <= VK_NUMPAD9) { keysym->unicode = vkey - VK_NUMPAD0 + '0'; } else if (SDL_ToUnicode((UINT)vkey, scancode, keystate, wchars, sizeof(wchars)/sizeof(wchars[0]), 0) > 0) { keysym->unicode = wchars[0]; } #endif } #if 0 { HKL hLayoutCurrent = GetKeyboardLayout(0); int sc = scancode & 0xFF; printf("SYM:%d, VK:0x%02X, SC:0x%04X, US:(1:0x%02X, 3:0x%02X), " "Current:(1:0x%02X, 3:0x%02X)\n", keysym->sym, vkey, scancode, MapVirtualKeyEx(sc, 1, hLayoutUS), MapVirtualKeyEx(sc, 3, hLayoutUS), MapVirtualKeyEx(sc, 1, hLayoutCurrent), MapVirtualKeyEx(sc, 3, hLayoutCurrent) ); } #endif return(keysym); }
static UInt8 _dx2vk(UINT dx){ if (dx >= VK_TABLE_SIZE) return NOKEY; UInt8 vkCode = NOKEY; HKL kbLayout = GetKeyboardLayout(0); vkCode = MapVirtualKeyEx(dx, 3, kbLayout); if (!vkCode) { switch (dx) { DX2VK(DIVIDE); DX2VK(RCONTROL); DX2VK(RMENU); DX2VK(HOME); DX2VK(PRIOR); DX2VK(UP); DX2VK(DOWN); DX2VK(LEFT); DX2VK(RIGHT); DX2VK(END); DX2VK(NEXT); DX2VK(INSERT); DX2VK(DELETE); case DIK_NUMPADENTER: vkCode = VK_SEPARATOR; break; default: vkCode = NOKEY; } } return vkCode; }
UInt32 MapScanCodeToChar( UINT scanCode , BYTE *charCode ) { HKL hklKeyboardLayout = GetKeyboardLayout(0); // 0 means current thread // This seemingly cannot fail // If this value is cached then the application must respond to WM_INPUTLANGCHANGE /* * Notes on second arg beeing 2: * uCode is a virtual-key code and is translated into an unshifted character value in * the low-order word of the return value. Dead keys (diacritics) are indicated by * setting the top bit of the return value. If there is no translation, the function returns 0. */ UINT uiCharCode = MapVirtualKeyEx( scanCode , 2 , // Convert scan code to char code hklKeyboardLayout ); if( 0 == uiCharCode ) { // Error converting to a char return 0; } *charCode = ((BYTE)uiCharCode); return 1; }
UINT VkToVsc( DWORD vk ) { UINT vsc = MapVirtualKeyEx( vk & 0x7fffffffUL, MAPVK_VK_TO_VSC, GetKeyboardLayout( 0 ) ); if ( 0 == vsc ) return 0; return MAKELONG( 1, vsc ); }
std::wstring cil::CILKeyboardState::ToUnicode( CIL_KEY key ) { std::wstring out; int scan; WCHAR buff[32]; UINT size = 32; scan = MapVirtualKeyEx(key, 0, m_keyboardLayout); int numChars = ToUnicodeEx(key, scan, m_keys, buff, size, 0, m_keyboardLayout); if (numChars == 1) { out.push_back(buff[0]); } else if (numChars > 1) { WCHAR* pTemp = new WCHAR[numChars + 1]; memcpy(pTemp, buff, numChars * sizeof(WCHAR)); pTemp[numChars] = L'\0'; out += std::wstring(pTemp); } return out; }
/** * Utility function for converting from virtual DI key values into * keyboard scancodes. * * @return 0 on failure 1 on success */ UInt32 MapDIKeyToScancode( UINT keyCode , UINT *scanCode ) { HKL hklKeyboardLayout = GetKeyboardLayout(0); // 0 means current thread // This seemingly cannot fail // If this value is cached then the application must respond to WM_INPUTLANGCHANGE /* * Notes on second arg beeing 0: * uCode is a virtual-key code and is translated into a scan code. * If it is a virtual-key code that does not distinguish between left- and right-hand keys, * the left-hand scan code is returned. If there is no translation, the function returns 0. */ UINT uiScanCode = MapVirtualKeyEx( keyCode , 0 , // Convert DIK_ code to scan code hklKeyboardLayout ); if( 0 == uiScanCode ) { // Error converting to a scancode return 0; } *scanCode = uiScanCode; return 1; }
void CKeySendImpl::OldSendChar(TCHAR c) { BOOL shiftDown = false; // Assume shift key is up at start. BOOL ctrlDown = false; BOOL altDown = false; SHORT keyScanCode = VkKeyScanEx(c, m_hlocale); // high order byte of keyscancode indicates if SHIFT, CTRL etc keys should be down if (keyScanCode & 0x100) { shiftDown = true; //send a shift down - Fixes bug #1208955 keybd_event(VK_SHIFT, (BYTE) MapVirtualKeyEx(VK_SHIFT, 0, m_hlocale), 0, 3); } if (keyScanCode & 0x200) { ctrlDown = true; //send a ctrl down keybd_event(VK_CONTROL, (BYTE) MapVirtualKeyEx(VK_CONTROL, 0, m_hlocale), KEYEVENTF_EXTENDEDKEY, 0); } if (keyScanCode & 0x400) { altDown = true; //send a alt down keybd_event(VK_MENU, (BYTE) MapVirtualKeyEx(VK_MENU, 0, m_hlocale), KEYEVENTF_EXTENDEDKEY, 0); } // the lower order byte has the key scan code we need. keyScanCode = (SHORT)(keyScanCode & 0xFF); keybd_event((BYTE)keyScanCode, (BYTE) MapVirtualKeyEx(keyScanCode, 0, m_hlocale), 0, 0); keybd_event((BYTE)keyScanCode, (BYTE) MapVirtualKeyEx(keyScanCode, 0, m_hlocale), KEYEVENTF_KEYUP, 0); if (shiftDown) { //send a shift up keybd_event(VK_SHIFT, (BYTE) MapVirtualKeyEx(VK_SHIFT, 0, m_hlocale), KEYEVENTF_KEYUP, 3); //Fixes bug #1208955 shiftDown = false; } if (ctrlDown) { //send a ctrl up keybd_event(VK_CONTROL, (BYTE) MapVirtualKeyEx(VK_CONTROL, 0, m_hlocale), KEYEVENTF_KEYUP |KEYEVENTF_EXTENDEDKEY, 0); ctrlDown = false; } if (altDown) { //send a alt up keybd_event(VK_MENU, (BYTE) MapVirtualKeyEx(VK_MENU, 0, m_hlocale), KEYEVENTF_KEYUP |KEYEVENTF_EXTENDEDKEY, 0); altDown = false; } ::Sleep(m_delay); }
bool KeyboardDevice::doKeyAction(Action action, int nativeKey, bool alterPressedKeys) { bool result = true; #ifdef Q_OS_UNIX KeyCode keyCode = XKeysymToKeycode(QX11Info::display(), nativeKey); if(action == Press || action == Trigger) result &= XTestFakeKeyEvent(QX11Info::display(), keyCode, True, CurrentTime); if(action == Release || action == Trigger) result &= XTestFakeKeyEvent(QX11Info::display(), keyCode, False, CurrentTime); XFlush(QX11Info::display()); #endif #ifdef Q_OS_WIN INPUT input; SecureZeroMemory(&input, sizeof(INPUT)); input.type = INPUT_KEYBOARD; switch(mType) { case Win32: { input.ki.wVk = nativeKey; HKL keyboardLayout = GetKeyboardLayout(0); input.ki.wScan = MapVirtualKeyEx(nativeKey, MAPVK_VK_TO_VSC, keyboardLayout); if(extendedKeys.count(nativeKey) > 0) input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY; } break; case DirectX: input.ki.wVk = 0; input.ki.wScan = ActionTools::KeyMapper::toDirectXKey(nativeKey); break; } if(action == Press || action == Trigger) result &= (SendInput(1, &input, sizeof(INPUT)) != 0); if(action == Release || action == Trigger) { input.ki.dwFlags |= KEYEVENTF_KEYUP; result &= (SendInput(1, &input, sizeof(INPUT)) != 0); } #endif if(alterPressedKeys) { if(action == Press) mPressedKeys.insert(nativeKey); else if(action == Release) mPressedKeys.remove(nativeKey); } return result; }
MyGUI::Char translateWin32Text(MyGUI::KeyCode kc) { static WCHAR deadKey = 0; BYTE keyState[256]; HKL layout = GetKeyboardLayout(0); if ( GetKeyboardState(keyState) == 0 ) return 0; int code = *((int*)&kc); unsigned int vk = MapVirtualKeyEx((UINT)code, 3, layout); if ( vk == 0 ) return 0; WCHAR buff[3] = { 0, 0, 0 }; int ascii = ToUnicodeEx(vk, (UINT)code, keyState, buff, 3, 0, layout); if (ascii == 1 && deadKey != '\0' ) { // A dead key is stored and we have just converted a character key // Combine the two into a single character WCHAR wcBuff[3] = { buff[0], deadKey, '\0' }; WCHAR out[3]; deadKey = '\0'; if (FoldStringW(MAP_PRECOMPOSED, (LPWSTR)wcBuff, 3, (LPWSTR)out, 3)) return out[0]; } else if (ascii == 1) { // We have a single character deadKey = '\0'; return buff[0]; } else if (ascii == 2) { // Convert a non-combining diacritical mark into a combining diacritical mark // Combining versions range from 0x300 to 0x36F; only 5 (for French) have been mapped below // http://www.fileformat.info/info/unicode/block/combining_diacritical_marks/images.htm switch (buff[0]) { case 0x5E: // Circumflex accent: ? deadKey = 0x302; break; case 0x60: // Grave accent: ? deadKey = 0x300; break; case 0xA8: // Diaeresis: ? deadKey = 0x308; break; case 0xB4: // Acute accent: ? deadKey = 0x301; break; case 0xB8: // Cedilla: ? deadKey = 0x327; break; default: deadKey = buff[0]; break; } } return 0; }
void CKeySend::ResetKeyboardState() const { // We need to make sure that the Control Key is still not down. // It will be down while the user presses ctrl-T the shortcut for autotype. BYTE keys[256]; GetKeyboardState((LPBYTE)&keys); while((keys[VK_CONTROL] & 0x80) != 0) { // VK_CONTROL is down so send a key down and an key up... if (m_impl->m_isOldOS) { keybd_event(VK_CONTROL, (BYTE)MapVirtualKeyEx(VK_CONTROL, 0, m_impl->m_hlocale), KEYEVENTF_EXTENDEDKEY, 0); keybd_event(VK_CONTROL, (BYTE) MapVirtualKeyEx(VK_CONTROL, 0, m_impl->m_hlocale), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } else { newSendVK(VK_CONTROL); // Send Ctrl keydown/keyup via SendInput } //now we let the messages be processed by the applications to set the keyboard state MSG msg; while (::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) { // so there is a message process it. #ifndef __WX__ if (!AfxGetThread()->PumpMessage()) break; #else // Not sure this is correct! if (msg.message == WM_QUIT) { // Put it back on the queue and leave now ::PostQuitMessage(0); return; } ::TranslateMessage(&msg); ::DispatchMessage(&msg); #endif } ::Sleep(10); SecureZeroMemory(keys, sizeof(keys)); GetKeyboardState((LPBYTE)&keys); } // while }
int VKToChar(unsigned int vk, WCHAR *s) { int scan; HKL layout = GetKeyboardLayout(0); BYTE kbd[256]; if(!GetKeyboardState(kbd)) return 0; scan = MapVirtualKeyEx(vk, 0, layout); return ToUnicode(vk, scan, kbd, s, 2, 0); }
int32 CInputManager::Scan2ascii (uint32 scancode, uint16* result) { static HKL layout=GetKeyboardLayout(0); static uint8 State[256]; if (GetKeyboardState(State)==FALSE) return 0; uint32 vk=MapVirtualKeyEx(scancode,1,layout); return ToAsciiEx(vk,scancode,State,result,0,layout); }
WORD scanCodeToASCII(DWORD scancode) { HKL layout = GetKeyboardLayout(0); UINT vk=MapVirtualKeyEx(scancode,1,layout); UCHAR State[256]; WORD res = 0; if (GetKeyboardState(State) && ToAsciiEx(vk,scancode,State,&res,0,layout) == 1); return res; }
HWND GlobalCopy() { HWND hCurrGlobalActiveWindow = GetForegroundWindow(); if (hCurrGlobalActiveWindow!=NULL) { //DWORD remoteThreadId = GetWindowThreadProcessId(hCurrGlobalActiveWindow,0); //DWORD currentThreadId = GetCurrentThreadId(); //AttachThreadInput(remoteThreadId, currentThreadId, true); //HWND hFocusedWindow = GetFocus(); //AttachThreadInput(remoteThreadId, currentThreadId, false); keybd_event(VK_CONTROL, MapVirtualKeyEx(VK_CONTROL, 0, GetKeyboardLayout(0)), 0, 0); keybd_event(0x43, MapVirtualKeyEx(0x43, 0, GetKeyboardLayout(0)), 0, 0); keybd_event(0x43, MapVirtualKeyEx(0x43, 0, GetKeyboardLayout(0)), KEYEVENTF_KEYUP, 0); keybd_event(VK_CONTROL, MapVirtualKeyEx(VK_CONTROL, 0, GetKeyboardLayout(0)), KEYEVENTF_KEYUP, 0); Sleep(100); } return hCurrGlobalActiveWindow; }
int winTranslateKey(WPARAM wParam, LPARAM lParam) { int iKeyFixup = g_iKeyMap[wParam * WIN_KEYMAP_COLS + 1]; int iKeyFixupEx = g_iKeyMap[wParam * WIN_KEYMAP_COLS + 2]; int iParam = HIWORD(lParam); int iParamScanCode = LOBYTE(iParam); int iScanCode; winDebug("winTranslateKey: wParam %08x lParam %08x\n", wParam, lParam); /* WM_ key messages faked by Vista speech recognition (WSR) don't have a * scan code. * * Vocola 3 (Rick Mohr's supplement to WSR) uses * System.Windows.Forms.SendKeys.SendWait(), which appears always to give a * scan code of 1 */ if (iParamScanCode <= 1) { if (VK_PRIOR <= wParam && wParam <= VK_DOWN) /* Trigger special case table to translate to extended * keycode, otherwise if num_lock is on, we can get keypad * numbers instead of navigation keys. */ iParam |= KF_EXTENDED; else iParamScanCode = MapVirtualKeyEx(wParam, /*MAPVK_VK_TO_VSC */ 0, GetKeyboardLayout(0)); } /* Branch on special extended, special non-extended, or normal key */ if ((iParam & KF_EXTENDED) && iKeyFixupEx) iScanCode = iKeyFixupEx; else if (iKeyFixup) iScanCode = iKeyFixup; else if (wParam == 0 && iParamScanCode == 0x70) iScanCode = KEY_HKTG; else switch (iParamScanCode) { case 0x70: iScanCode = KEY_HKTG; break; case 0x73: iScanCode = KEY_BSlash2; break; default: iScanCode = iParamScanCode; break; } return iScanCode; }
//================================================================================ void ATHInputManager::SendKeyboardEvent() { ATHKeyList m_liKeysDown = CheckKeys(); ATHKeyList::iterator itrCurr = m_liKeysDown.begin(); ATHKeyList::iterator itrEnd = m_liKeysDown.end(); ATHEvent keyEvent( AET_KEYBOARD ); unsigned int unKeyDownIndex = 0; unsigned int unKeyUpIndex = 0; while( itrCurr != itrEnd ) { unsigned int szDIKKey = (*itrCurr); if (KeyPressed( szDIKKey ) && unKeyDownIndex < 8) { BYTE chAsciiKeys[ATH_NUM_KEYS] = {}; if (GetKeyboardState(chAsciiKeys)) { unsigned short szAsciiKey = 0; // Why do I have to do both opposite conversions???? int nCharCount = ToAsciiEx(MapVirtualKeyEx(szDIKKey, MAPVK_VSC_TO_VK, NULL), MapVirtualKeyEx(szDIKKey, MAPVK_VK_TO_VSC, NULL), chAsciiKeys, &szAsciiKey, 0, NULL); if (nCharCount > 0) { keyEvent.KEY_szKeysPressed[unKeyDownIndex] = (char)szAsciiKey; unKeyDownIndex++; } } } itrCurr++; } if( unKeyDownIndex > 0|| unKeyUpIndex > 0 ) { keyEvent.m_EventID = AEI_KEYDOWN; m_pEventManager->SendEvent( keyEvent, AEP_IMMEDIATE ); } }
LPTSTR GenerateLayoutString(HKL hklLayout) { BYTE bState[256] = {0}; LPTSTR ptszLayStr = (LPTSTR)mir_alloc(100 * sizeof(TCHAR)); LPTSTR ptszTemp = (LPTSTR)mir_alloc(3 * sizeof(TCHAR)); ptszTemp[0] = 0; DWORD i; for (i = 0; i < _tcslen(ptszKeybEng); i++) { SHORT shVirtualKey = VkKeyScanEx(ptszKeybEng[i], hklEng); UINT iScanCode = MapVirtualKeyEx(shVirtualKey & 0x00FF, 0, hklEng); for (DWORD j = 0; j < 256; j++) bState[j] = 0; if (shVirtualKey & 0x0100) bState[VK_SHIFT] = 0x80; if (shVirtualKey & 0x0200) bState[VK_CONTROL] = 0x80; if (shVirtualKey & 0x0400) bState[VK_MENU] = 0x80; shVirtualKey = MapVirtualKeyEx(iScanCode, 1, hklLayout); bState[shVirtualKey & 0x00FF] = 0x80; int iRes = ToUnicodeEx(shVirtualKey, iScanCode, bState, ptszTemp, 3, 0, hklLayout); // Защита от дэд-кеев if (iRes < 0) ToUnicodeEx(shVirtualKey, iScanCode, bState, ptszTemp, 3, 0, hklLayout); // Если нам вернули нулевой символ, или не вернули ничего, то присвоим "звоночек" if (ptszTemp[0] == 0) ptszLayStr[i] = 3; else ptszLayStr[i] = ptszTemp[0]; } ptszLayStr[i] = 0; mir_free(ptszTemp); return ptszLayStr; }
wchar_t Keyboard::GetCurrentPressedChar(uint64_t lockID) const { if(m_LockID != 0 && m_LockID != lockID) { return L''; } //ToUnicodeEx returns a key for ctrl + ANYKEY, we do not want any, so it if it is press, return no key if(m_CurrentKeyboardState[(uint32_t)AEKeys::LCTRL] & AE_INPUT_PRESS || m_CurrentKeyboardState[(uint32_t)AEKeys::RCTRL] & AE_INPUT_PRESS) { return L''; } //ToUnicodeEx returns a key for backspace, we do not want any, so it if it is press, return no key if(m_CurrentKeyboardState[(uint32_t)AEKeys::BACKSPACE] & AE_INPUT_PRESS) { return L''; } BYTE state[256]; memset(state, 0, sizeof(BYTE) * 256); if (GetKeyboardState(state) == FALSE) { return L''; } //For Shift/Ctrl/Alt/Caps Keys we need to set them //0x80 = down //0x01 = toggled (for Caps) state[VK_CAPITAL] = (BYTE)GetKeyState(VK_CAPITAL); state[VK_SHIFT] = (BYTE)GetKeyState(VK_SHIFT); for(uint32_t i = 0; i < AE_MAX_KEYS; ++i) { if(!(m_PreviousKeyboardState[i] & AE_INPUT_PRESS) && (m_CurrentKeyboardState[i] & AE_INPUT_PRESS)) { uint32_t vk = (uint32_t)m_KeyMapVK[i]; uint32_t scanCode = MapVirtualKeyEx(vk, MAPVK_VK_TO_VSC, m_KeyboardLayout); wchar_t result[] = { 0, 0 }; if (ToUnicodeEx(vk, scanCode, state, result, 2, 0, m_KeyboardLayout) > 0) { return result[0]; } } } return L''; }
std::string cil::CILKeyboardState::ToAscii( CIL_KEY key ) { std::string out; int scan; USHORT buff[2]; scan = MapVirtualKeyEx(key, 0, m_keyboardLayout); if ( ToAsciiEx(key, scan, m_keys, buff, 0, m_keyboardLayout) > 0) { out += (char)buff[0]; }; return out; }
CString VkToString( DWORD vk ) { UINT vsc = MapVirtualKeyEx( vk & 0x7fffffffUL, MAPVK_VK_TO_VSC, GetKeyboardLayout( 0 ) ); if ( ! vsc ) { return CString( ); } BYTE keyState[256] = { 0, }; wchar_t keyBuf[8] = { 0, }; if ( 0 != ( vk & 0x80000000 ) ) { keyState[VK_SHIFT] = 0x80; } ToUnicodeEx( vk, vsc, keyState, keyBuf, 256, 0, GetKeyboardLayout( 0 ) ); return CString( keyBuf ); }
CString NameFromVKey(UINT nVK) { CString str; if (0x00 == nVK) { str = _T("None"); } else if (VK_SNAPSHOT == nVK) { str = _T("Prt Scr"); } else { UINT nScanCode = MapVirtualKeyEx(nVK, 0, GetKeyboardLayout(0)); switch(nVK) { // Keys which are "extended" (except for Return which is Numeric Enter as extended) case VK_INSERT: case VK_DELETE: case VK_HOME: case VK_END: case VK_NEXT: // Page down case VK_PRIOR: // Page up case VK_LEFT: case VK_RIGHT: case VK_UP: case VK_DOWN: nScanCode |= 0x100; // Add extended bit } // GetKeyNameText() expects the scan code to be on the same format as WM_KEYDOWN // Hence the left shift LPTSTR prb = str.GetBuffer(80); BOOL bResult = GetKeyNameText(nScanCode << 16, prb, 79); // these key names are capitalized and look a bit daft int len = lstrlen(prb); if(len > 1) { LPTSTR p2 = CharNext(prb); CharLowerBuff(p2, len - (p2 - prb) ); } str.ReleaseBuffer(); ATLASSERT(str.GetLength()); } return str; // internationalization ready, sweet! }
void WritesScannedKeyToFile(short sScannedKey) { HKL hkl; DWORD dwThreadId; DWORD dwProcessId; hWindowHandle = GetForegroundWindow(); dwThreadId = GetWindowThreadProcessId(hWindowHandle, &dwProcessId); BYTE *kState = (BYTE*)malloc(256); GetKeyboardState(kState); hkl = GetKeyboardLayout(dwThreadId); wchar_t besmallah[16] = {0}; UINT virtualKey = MapVirtualKeyEx((UINT)sScannedKey, MAPVK_VK_TO_CHAR, hkl); ToUnicodeEx(virtualKey, sScannedKey, (BYTE*)kState, besmallah, 16, NULL, hkl); WriteToFile(besmallah); //CloseHandle(hkl); free(kState); }
static int SDL_MapVirtualKey(int scancode, int vkey) { #ifndef _WIN32_WCE int mvke = MapVirtualKeyEx(scancode & 0xFF, 1, hLayoutUS); #else int mvke = MapVirtualKey(scancode & 0xFF, 1); #endif switch(vkey) { case VK_DIVIDE: case VK_MULTIPLY: case VK_SUBTRACT: case VK_ADD: case VK_LWIN: case VK_RWIN: case VK_APPS: case VK_LCONTROL: case VK_RCONTROL: case VK_LSHIFT: case VK_RSHIFT: case VK_LMENU: case VK_RMENU: case VK_SNAPSHOT: case VK_PAUSE: return vkey; } switch(mvke) { case VK_INSERT: return EXTKEYPAD(VK_NUMPAD0); case VK_DELETE: return EXTKEYPAD(VK_DECIMAL); case VK_END: return EXTKEYPAD(VK_NUMPAD1); case VK_DOWN: return EXTKEYPAD(VK_NUMPAD2); case VK_NEXT: return EXTKEYPAD(VK_NUMPAD3); case VK_LEFT: return EXTKEYPAD(VK_NUMPAD4); case VK_CLEAR: return EXTKEYPAD(VK_NUMPAD5); case VK_RIGHT: return EXTKEYPAD(VK_NUMPAD6); case VK_HOME: return EXTKEYPAD(VK_NUMPAD7); case VK_UP: return EXTKEYPAD(VK_NUMPAD8); case VK_PRIOR: return EXTKEYPAD(VK_NUMPAD9); } return mvke?mvke:vkey; }
static int XBMC_MapVirtualKey(int scancode, int vkey) { // It isn't clear why the US keyboard layout was being used. This causes // problems with e.g. the \ key. I have provisionally switched the code // to use the Windows layout. // int mvke = MapVirtualKeyEx(scancode & 0xFF, 1, hLayoutUS); int mvke = MapVirtualKeyEx(scancode & 0xFF, 1, NULL); switch(vkey) { /* These are always correct */ case VK_DIVIDE: case VK_MULTIPLY: case VK_SUBTRACT: case VK_ADD: case VK_LWIN: case VK_RWIN: case VK_APPS: /* These are already handled */ case VK_LCONTROL: case VK_RCONTROL: case VK_LSHIFT: case VK_RSHIFT: case VK_LMENU: case VK_RMENU: case VK_SNAPSHOT: case VK_PAUSE: return vkey; } switch(mvke) { /* Distinguish between keypad and extended keys */ case VK_INSERT: return EXTKEYPAD(VK_NUMPAD0); case VK_DELETE: return EXTKEYPAD(VK_DECIMAL); case VK_END: return EXTKEYPAD(VK_NUMPAD1); case VK_DOWN: return EXTKEYPAD(VK_NUMPAD2); case VK_NEXT: return EXTKEYPAD(VK_NUMPAD3); case VK_LEFT: return EXTKEYPAD(VK_NUMPAD4); case VK_CLEAR: return EXTKEYPAD(VK_NUMPAD5); case VK_RIGHT: return EXTKEYPAD(VK_NUMPAD6); case VK_HOME: return EXTKEYPAD(VK_NUMPAD7); case VK_UP: return EXTKEYPAD(VK_NUMPAD8); case VK_PRIOR: return EXTKEYPAD(VK_NUMPAD9); } return mvke ? mvke : vkey; }
unsigned short CP3DInput::GetKeyChar() { bool repeat = m_pTimer->IsTimeElapsed(m_timRepeat, true); for (int i=0; i < 256; i++) { if (i==1) continue; // nepøevádìj esc klávesu if (m_pKeyboardDown[i] || (repeat && m_pKeyboardCurr[i] == true)) // je klávesa poprvé dole? Nebo je uz dost dlouho dole(=opakovat)? { //NUMPAD switch(i) { case DIK_NUMPAD0: return '0'; break; case DIK_NUMPAD1: return '1'; break; case DIK_NUMPAD2: return '2'; break; case DIK_NUMPAD3: return '3'; break; case DIK_NUMPAD4: return '4'; break; case DIK_NUMPAD5: return '5'; break; case DIK_NUMPAD6: return '6'; break; case DIK_NUMPAD7: return '7'; break; case DIK_NUMPAD8: return '8'; break; case DIK_NUMPAD9: return '9'; break; case 83: return '.'; break; case 181: return '/'; break; case DIK_NUMPADCOMMA: return '.'; break; case DIK_NUMPADENTER: return 13; break; case DIK_NUMPADEQUALS: return '='; break; } unsigned short result=0; // výsledný znak HKL layout=GetKeyboardLayout(0); // získej aktuální rozložení klávesnice unsigned char State[256]; if (GetKeyboardState(State)==FALSE) // získej stav všech virt. kláves return 0; // chyba unsigned int vk=MapVirtualKeyEx(i,1,layout); // namapuj virtual-code ToAsciiEx(vk,i,State,&result, 0,layout); // získej Ascii znak //CON(MSG_CON_INFO, "%d => %d", i, result); // DEBUG! return result; } } return 0; }
//================================================================================ ATHKeyList ATHInputManager::CheckKeys() { std::list< unsigned int > liKeysDown; BYTE chAsciiKeys[ ATH_NUM_KEYS ] = {}; if( GetKeyboardState( chAsciiKeys ) ) { for(unsigned int i = 0; i < ATH_NUM_KEYS; i++) { if( KeyState( (unsigned char)(i) ) ) { unsigned int nVKCode = MapVirtualKeyEx(i, MAPVK_VSC_TO_VK, NULL ); liKeysDown.push_back(i); } } } return liKeysDown; }
static int SDL_MapVirtualKey(int scancode, int vkey) { int mvke = MapVirtualKeyEx(scancode & 0xFF, 1, hLayoutUS); switch(vkey) { /* These are always correct */ case VK_DIVIDE: case VK_MULTIPLY: case VK_SUBTRACT: case VK_ADD: case VK_LWIN: case VK_RWIN: case VK_APPS: /* These are already handled */ case VK_LCONTROL: case VK_RCONTROL: case VK_LSHIFT: case VK_RSHIFT: case VK_LMENU: case VK_RMENU: case VK_SNAPSHOT: case VK_PAUSE: return vkey; } switch(mvke) { /* Distinguish between keypad and extended keys */ case VK_INSERT: return EXTKEYPAD(VK_NUMPAD0); case VK_DELETE: return EXTKEYPAD(VK_DECIMAL); case VK_END: return EXTKEYPAD(VK_NUMPAD1); case VK_DOWN: return EXTKEYPAD(VK_NUMPAD2); case VK_NEXT: return EXTKEYPAD(VK_NUMPAD3); case VK_LEFT: return EXTKEYPAD(VK_NUMPAD4); case VK_CLEAR: return EXTKEYPAD(VK_NUMPAD5); case VK_RIGHT: return EXTKEYPAD(VK_NUMPAD6); case VK_HOME: return EXTKEYPAD(VK_NUMPAD7); case VK_UP: return EXTKEYPAD(VK_NUMPAD8); case VK_PRIOR: return EXTKEYPAD(VK_NUMPAD9); } return mvke?mvke:vkey; }