From 62e4ff1e91e4dfc60246b733e0e7d7822e395905 Mon Sep 17 00:00:00 2001 From: Marcus Date: Mon, 10 Jan 2011 21:19:27 +0100 Subject: [PATCH] Updated the key translation logic for Win32. --- src/win32/win32_window.c | 127 +++++++++++++++++++++++++-------------- 1 file changed, 81 insertions(+), 46 deletions(-) diff --git a/src/win32/win32_window.c b/src/win32/win32_window.c index 1fcad004..cd6295a0 100644 --- a/src/win32/win32_window.c +++ b/src/win32/win32_window.c @@ -440,11 +440,11 @@ static int translateKey(WPARAM wParam, LPARAM lParam) DWORD msg_time; DWORD scan_code; - // Check for numeric keypad keys - // Note: This way we always force "NumLock = ON", which at least - // enables GLFW users to detect numeric keypad keys + // Check for numeric keypad keys. + // Note: This way we always force "NumLock = ON", which is intentional + // since the returned key code should correspond to a physical + // location. int hiFlags = HIWORD(lParam); - if (!(hiFlags & 0x100)) { switch (MapVirtualKey(hiFlags & 0xFF, 1)) @@ -464,6 +464,7 @@ static int translateKey(WPARAM wParam, LPARAM lParam) case VK_SUBTRACT: return GLFW_KEY_KP_SUBTRACT; case VK_ADD: return GLFW_KEY_KP_ADD; case VK_DELETE: return GLFW_KEY_KP_DECIMAL; + default: break; } } @@ -478,9 +479,9 @@ static int translateKey(WPARAM wParam, LPARAM lParam) // right) scan_code = MapVirtualKey(VK_RSHIFT, 0); if (((lParam & 0x01ff0000) >> 16) == scan_code) - return GLFW_KEY_RSHIFT; + return GLFW_KEY_RIGHT_SHIFT; - return GLFW_KEY_LSHIFT; + return GLFW_KEY_LEFT_SHIFT; } // The CTRL keys require special handling @@ -488,7 +489,7 @@ static int translateKey(WPARAM wParam, LPARAM lParam) { // Is this an extended key (i.e. right key)? if (lParam & 0x01000000) - return GLFW_KEY_RCTRL; + return GLFW_KEY_RIGHT_CTRL; // Here is a trick: "Alt Gr" sends LCTRL, then RALT. We only // want the RALT message, so we try to see if the next message @@ -505,12 +506,12 @@ static int translateKey(WPARAM wParam, LPARAM lParam) { // Next message is a RALT down message, which // means that this is NOT a proper LCTRL message! - return GLFW_KEY_UNKNOWN; + return -1; } } } - return GLFW_KEY_LCTRL; + return GLFW_KEY_LEFT_CTRL; } // The ALT keys require special handling @@ -518,9 +519,9 @@ static int translateKey(WPARAM wParam, LPARAM lParam) { // Is this an extended key (i.e. right key)? if (lParam & 0x01000000) - return GLFW_KEY_RALT; + return GLFW_KEY_RIGHT_ALT; - return GLFW_KEY_LALT; + return GLFW_KEY_LEFT_ALT; } // The ENTER keys require special handling @@ -533,16 +534,16 @@ static int translateKey(WPARAM wParam, LPARAM lParam) return GLFW_KEY_ENTER; } - // Special keys (non character keys) - case VK_ESCAPE: return GLFW_KEY_ESC; + // Funcion keys (non-printable keys) + case VK_ESCAPE: return GLFW_KEY_ESCAPE; case VK_TAB: return GLFW_KEY_TAB; case VK_BACK: return GLFW_KEY_BACKSPACE; case VK_HOME: return GLFW_KEY_HOME; case VK_END: return GLFW_KEY_END; - case VK_PRIOR: return GLFW_KEY_PAGEUP; - case VK_NEXT: return GLFW_KEY_PAGEDOWN; + case VK_PRIOR: return GLFW_KEY_PAGE_UP; + case VK_NEXT: return GLFW_KEY_PAGE_DOWN; case VK_INSERT: return GLFW_KEY_INSERT; - case VK_DELETE: return GLFW_KEY_DEL; + case VK_DELETE: return GLFW_KEY_DELETE; case VK_LEFT: return GLFW_KEY_LEFT; case VK_UP: return GLFW_KEY_UP; case VK_RIGHT: return GLFW_KEY_RIGHT; @@ -571,7 +572,13 @@ static int translateKey(WPARAM wParam, LPARAM lParam) case VK_F22: return GLFW_KEY_F22; case VK_F23: return GLFW_KEY_F23; case VK_F24: return GLFW_KEY_F24; - case VK_SPACE: return GLFW_KEY_SPACE; + case VK_NUMLOCK: return GLFW_KEY_NUM_LOCK; + case VK_CAPITAL: return GLFW_KEY_CAPS_LOCK; + case VK_SCROLL: return GLFW_KEY_SCROLL_LOCK; + case VK_PAUSE: return GLFW_KEY_PAUSE; + case VK_LWIN: return GLFW_KEY_LEFT_SUPER; + case VK_RWIN: return GLFW_KEY_RIGHT_SUPER; + case VK_APPS: return GLFW_KEY_MENU; // Numeric keypad case VK_NUMPAD0: return GLFW_KEY_KP_0; @@ -589,35 +596,63 @@ static int translateKey(WPARAM wParam, LPARAM lParam) case VK_SUBTRACT: return GLFW_KEY_KP_SUBTRACT; case VK_ADD: return GLFW_KEY_KP_ADD; case VK_DECIMAL: return GLFW_KEY_KP_DECIMAL; - case VK_NUMLOCK: return GLFW_KEY_KP_NUM_LOCK; - case VK_CAPITAL: return GLFW_KEY_CAPS_LOCK; - case VK_SCROLL: return GLFW_KEY_SCROLL_LOCK; - case VK_PAUSE: return GLFW_KEY_PAUSE; - - case VK_LWIN: return GLFW_KEY_LSUPER; - case VK_RWIN: return GLFW_KEY_RSUPER; - case VK_APPS: return GLFW_KEY_MENU; - - // The rest (should be printable keys) - default: - { - // Convert to printable character (ISO-8859-1 or Unicode) - wParam = MapVirtualKey((UINT) wParam, 2) & 0x0000FFFF; - - // Make sure that the character is uppercase - wParam = (WPARAM) CharUpperW((LPWSTR) wParam); - - // Valid ISO-8859-1 character? - if ((wParam >= 32 && wParam <= 126) || - (wParam >= 160 && wParam <= 255)) - { - return (int) wParam; - } - - return GLFW_KEY_UNKNOWN; - } + // Printable keys are mapped according to US layout + case VK_SPACE: return GLFW_KEY_SPACE; + case 0x30: return GLFW_KEY_0; + case 0x31: return GLFW_KEY_1; + case 0x32: return GLFW_KEY_2; + case 0x33: return GLFW_KEY_3; + case 0x34: return GLFW_KEY_4; + case 0x35: return GLFW_KEY_5; + case 0x36: return GLFW_KEY_6; + case 0x37: return GLFW_KEY_7; + case 0x38: return GLFW_KEY_8; + case 0x39: return GLFW_KEY_9; + case 0x41: return GLFW_KEY_A; + case 0x42: return GLFW_KEY_B; + case 0x43: return GLFW_KEY_C; + case 0x44: return GLFW_KEY_D; + case 0x45: return GLFW_KEY_E; + case 0x46: return GLFW_KEY_F; + case 0x47: return GLFW_KEY_G; + case 0x48: return GLFW_KEY_H; + case 0x49: return GLFW_KEY_I; + case 0x4A: return GLFW_KEY_J; + case 0x4B: return GLFW_KEY_K; + case 0x4C: return GLFW_KEY_L; + case 0x4D: return GLFW_KEY_M; + case 0x4E: return GLFW_KEY_N; + case 0x4F: return GLFW_KEY_O; + case 0x50: return GLFW_KEY_P; + case 0x51: return GLFW_KEY_Q; + case 0x52: return GLFW_KEY_R; + case 0x53: return GLFW_KEY_S; + case 0x54: return GLFW_KEY_T; + case 0x55: return GLFW_KEY_U; + case 0x56: return GLFW_KEY_V; + case 0x57: return GLFW_KEY_W; + case 0x58: return GLFW_KEY_X; + case 0x59: return GLFW_KEY_Y; + case 0x5A: return GLFW_KEY_Z; + case 0xBD: return GLFW_KEY_MINUS; + case 0xBB: return GLFW_KEY_EQUAL; + case 0xDB: return GLFW_KEY_LEFT_BRACKET; + case 0xDD: return GLFW_KEY_RIGHT_BRACKET; + case 0xDC: return GLFW_KEY_BACKSLASH; + case 0xBA: return GLFW_KEY_SEMICOLON; + case 0xDE: return GLFW_KEY_APOSTROPHE; + case 0xC0: return GLFW_KEY_GRAVE_ACCENT; + case 0xBC: return GLFW_KEY_COMMA; + case 0xBE: return GLFW_KEY_PERIOD; + case 0xBF: return GLFW_KEY_SLASH; + case 0xDF: return GLFW_KEY_WORLD_1; + case 0xE2: return GLFW_KEY_WORLD_2; + default: break; } + + // No matching translation was found, so return -1 + return -1; } @@ -791,8 +826,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, // Special trick: release both shift keys on SHIFT up event if (wParam == VK_SHIFT) { - _glfwInputKey(window, GLFW_KEY_LSHIFT, GLFW_RELEASE); - _glfwInputKey(window, GLFW_KEY_RSHIFT, GLFW_RELEASE); + _glfwInputKey(window, GLFW_KEY_LEFT_SHIFT, GLFW_RELEASE); + _glfwInputKey(window, GLFW_KEY_RIGHT_SHIFT, GLFW_RELEASE); } else _glfwInputKey(window, translateKey(wParam, lParam), GLFW_RELEASE);