gui: kb shortcuts win: faster VK to WX translation with a table

This commit is contained in:
Avi Halachmi (:avih) 2015-10-11 20:11:33 +03:00
parent 302bf29b6a
commit a76914a4b1
1 changed files with 133 additions and 122 deletions

View File

@ -20,133 +20,144 @@
#define PX_VK_A 0x41
#define PX_VK_Z 0x5A
#define PX_VK2WX_SIZE 256
static int vk2wx[PX_VK2WX_SIZE] = { 0 };
static bool initialized = false;
static void addTx(UINT vkCode, int wxCode)
{
pxAssert(vkCode < PX_VK2WX_SIZE);
pxAssert(!vk2wx[vkCode]); // we shouldn't override entries here
vk2wx[vkCode] = wxCode;
}
static void initTable()
{
// Shift, Control, Alt and Menu
addTx(VK_SHIFT, WXK_SHIFT);
addTx(VK_LSHIFT, WXK_SHIFT);
addTx(VK_RSHIFT, WXK_SHIFT);
addTx(VK_CONTROL, WXK_CONTROL);
addTx(VK_LCONTROL, WXK_CONTROL);
addTx(VK_RCONTROL, WXK_CONTROL);
addTx(VK_MENU, WXK_ALT);
addTx(VK_LMENU, WXK_ALT);
addTx(VK_RMENU, WXK_ALT);
addTx(VK_LWIN, WXK_WINDOWS_LEFT);
addTx(VK_RWIN, WXK_WINDOWS_RIGHT);
addTx(VK_APPS, WXK_WINDOWS_MENU);
// Scroll, Caps and numlocks
addTx(VK_SCROLL, WXK_SCROLL);
addTx(VK_CAPITAL, WXK_CAPITAL);
addTx(VK_NUMLOCK, WXK_NUMLOCK);
// cursor and other extended keyboard keys
addTx(VK_PRIOR, WXK_PAGEUP);
addTx(VK_NEXT, WXK_PAGEDOWN);
addTx(VK_HOME, WXK_HOME);
addTx(VK_END, WXK_END);
addTx(VK_LEFT, WXK_LEFT);
addTx(VK_UP, WXK_UP);
addTx(VK_RIGHT, WXK_RIGHT);
addTx(VK_DOWN, WXK_DOWN);
addTx(VK_INSERT, WXK_INSERT);
addTx(VK_DELETE, WXK_DELETE);
// numpad keys
addTx(VK_NUMPAD0, WXK_NUMPAD0);
addTx(VK_NUMPAD1, WXK_NUMPAD1);
addTx(VK_NUMPAD2, WXK_NUMPAD2);
addTx(VK_NUMPAD3, WXK_NUMPAD3);
addTx(VK_NUMPAD4, WXK_NUMPAD4);
addTx(VK_NUMPAD5, WXK_NUMPAD5);
addTx(VK_NUMPAD6, WXK_NUMPAD6);
addTx(VK_NUMPAD7, WXK_NUMPAD7);
addTx(VK_NUMPAD8, WXK_NUMPAD8);
addTx(VK_NUMPAD9, WXK_NUMPAD9);
addTx(VK_MULTIPLY, WXK_NUMPAD_MULTIPLY);
addTx(VK_ADD, WXK_NUMPAD_ADD);
addTx(VK_SEPARATOR, WXK_NUMPAD_SEPARATOR);
addTx(VK_SUBTRACT, WXK_NUMPAD_SUBTRACT);
addTx(VK_DECIMAL, WXK_NUMPAD_DECIMAL);
addTx(VK_DIVIDE, WXK_NUMPAD_DIVIDE);
// Function keys
addTx(VK_F1, WXK_F1);
addTx(VK_F2, WXK_F2);
addTx(VK_F3, WXK_F3);
addTx(VK_F4, WXK_F4);
addTx(VK_F5, WXK_F5);
addTx(VK_F6, WXK_F6);
addTx(VK_F7, WXK_F7);
addTx(VK_F8, WXK_F8);
addTx(VK_F9, WXK_F9);
addTx(VK_F10, WXK_F10);
addTx(VK_F11, WXK_F11);
addTx(VK_F12, WXK_F12);
addTx(VK_F13, WXK_F13);
addTx(VK_F14, WXK_F14);
addTx(VK_F15, WXK_F15);
addTx(VK_F16, WXK_F16);
addTx(VK_F17, WXK_F17);
addTx(VK_F18, WXK_F18);
addTx(VK_F19, WXK_F19);
addTx(VK_F20, WXK_F20);
addTx(VK_F21, WXK_F21);
addTx(VK_F22, WXK_F22);
addTx(VK_F23, WXK_F23);
addTx(VK_F24, WXK_F24);
// various other special keys
addTx(VK_BACK, WXK_BACK);
addTx(VK_TAB, WXK_TAB);
addTx(VK_RETURN, WXK_RETURN);
addTx(VK_ESCAPE, WXK_ESCAPE);
addTx(VK_SNAPSHOT, WXK_SNAPSHOT);
addTx(VK_PAUSE, WXK_PAUSE);
// Not sure what these keys are
addTx(VK_CLEAR, WXK_CLEAR);
addTx(VK_HELP, WXK_HELP);
addTx(VK_SELECT, WXK_SELECT);
addTx(VK_EXECUTE, WXK_EXECUTE);
addTx(VK_PRINT, WXK_PRINT);
// symbol-only keys on all keyboards - return ascii
addTx(VK_OEM_PERIOD, '.');
addTx(VK_OEM_PLUS, '=');
addTx(VK_OEM_MINUS, '-');
addTx(VK_OEM_COMMA, ',');
// symbol-only keys on US keyboards - return ascii
addTx(VK_OEM_1, ';');
addTx(VK_OEM_2, '/');
addTx(VK_OEM_3, '`');
addTx(VK_OEM_4, '[');
addTx(VK_OEM_5, '\\');
addTx(VK_OEM_6, ']');
addTx(VK_OEM_7, '\'');
pxAssert(PX_VK_A < PX_VK_Z);
// VK codes for letter keys - return lower case ascii
for (UINT i = PX_VK_A; i < PX_VK_Z; i++)
addTx(i, i + 'a' - PX_VK_A);
initialized = true;
}
// Returns a WXK_* keycode translated from a VK_* virtual key. wxCharCodeMSWToWx was
// removed from wxWidgets 3, this should work as a replacement.
// Where an ascii code for a printable char exists, we try to return it
int TranslateVKToWXK(u32 keysym)
{
int key_code;
if (!initialized)
initTable();
switch (keysym)
{
// Shift, Control, Alt and Menu
case VK_SHIFT:
case VK_LSHIFT:
case VK_RSHIFT: key_code = WXK_SHIFT; break;
case VK_CONTROL:
case VK_LCONTROL:
case VK_RCONTROL: key_code = WXK_CONTROL; break;
case VK_LMENU:
case VK_RMENU:
case VK_MENU: key_code = WXK_ALT; break;
case VK_LWIN: key_code = WXK_WINDOWS_LEFT; break;
case VK_RWIN: key_code = WXK_WINDOWS_RIGHT; break;
case VK_APPS: key_code = WXK_WINDOWS_MENU; break;
// Scroll, Caps and numlocks
case VK_SCROLL: key_code = WXK_SCROLL; break;
case VK_CAPITAL: key_code = WXK_CAPITAL; break;
case VK_NUMLOCK: key_code = WXK_NUMLOCK; break;
// cursor and other extended keyboard keys
case VK_PRIOR: key_code = WXK_PAGEUP; break;
case VK_NEXT: key_code = WXK_PAGEDOWN; break;
case VK_HOME: key_code = WXK_HOME; break;
case VK_END: key_code = WXK_END; break;
case VK_LEFT: key_code = WXK_LEFT; break;
case VK_UP: key_code = WXK_UP; break;
case VK_RIGHT: key_code = WXK_RIGHT; break;
case VK_DOWN: key_code = WXK_DOWN; break;
case VK_INSERT: key_code = WXK_INSERT; break;
case VK_DELETE: key_code = WXK_DELETE; break;
// numpad keys
case VK_NUMPAD0: key_code = WXK_NUMPAD0; break;
case VK_NUMPAD1: key_code = WXK_NUMPAD1; break;
case VK_NUMPAD2: key_code = WXK_NUMPAD2; break;
case VK_NUMPAD3: key_code = WXK_NUMPAD3; break;
case VK_NUMPAD4: key_code = WXK_NUMPAD4; break;
case VK_NUMPAD5: key_code = WXK_NUMPAD5; break;
case VK_NUMPAD6: key_code = WXK_NUMPAD6; break;
case VK_NUMPAD7: key_code = WXK_NUMPAD7; break;
case VK_NUMPAD8: key_code = WXK_NUMPAD8; break;
case VK_NUMPAD9: key_code = WXK_NUMPAD9; break;
case VK_MULTIPLY: key_code = WXK_NUMPAD_MULTIPLY; break;
case VK_ADD: key_code = WXK_NUMPAD_ADD; break;
case VK_SEPARATOR:key_code = WXK_NUMPAD_SEPARATOR; break;
case VK_SUBTRACT: key_code = WXK_NUMPAD_SUBTRACT; break;
case VK_DECIMAL: key_code = WXK_NUMPAD_DECIMAL; break;
case VK_DIVIDE: key_code = WXK_NUMPAD_DIVIDE; break;
// Function keys
case VK_F1: key_code = WXK_F1; break;
case VK_F2: key_code = WXK_F2; break;
case VK_F3: key_code = WXK_F3; break;
case VK_F4: key_code = WXK_F4; break;
case VK_F5: key_code = WXK_F5; break;
case VK_F6: key_code = WXK_F6; break;
case VK_F7: key_code = WXK_F7; break;
case VK_F8: key_code = WXK_F8; break;
case VK_F9: key_code = WXK_F9; break;
case VK_F10: key_code = WXK_F10; break;
case VK_F11: key_code = WXK_F11; break;
case VK_F12: key_code = WXK_F12; break;
case VK_F13: key_code = WXK_F13; break;
case VK_F14: key_code = WXK_F14; break;
case VK_F15: key_code = WXK_F15; break;
case VK_F16: key_code = WXK_F16; break;
case VK_F17: key_code = WXK_F17; break;
case VK_F18: key_code = WXK_F18; break;
case VK_F19: key_code = WXK_F19; break;
case VK_F20: key_code = WXK_F20; break;
case VK_F21: key_code = WXK_F21; break;
case VK_F22: key_code = WXK_F22; break;
case VK_F23: key_code = WXK_F23; break;
case VK_F24: key_code = WXK_F24; break;
// various other special keys
case VK_BACK: key_code = WXK_BACK; break;
case VK_TAB: key_code = WXK_TAB; break;
case VK_RETURN: key_code = WXK_RETURN; break;
case VK_ESCAPE: key_code = WXK_ESCAPE; break;
case VK_SNAPSHOT: key_code = WXK_SNAPSHOT; break;
case VK_PAUSE: key_code = WXK_PAUSE; break;
// Not sure what these keys are
case VK_CLEAR: key_code = WXK_CLEAR; break;
case VK_HELP: key_code = WXK_HELP; break;
case VK_SELECT: key_code = WXK_SELECT; break;
case VK_EXECUTE: key_code = WXK_EXECUTE; break;
case VK_PRINT: key_code = WXK_PRINT; break;
// symbol-only keys on all keyboards - return ascii
case VK_OEM_PERIOD: key_code = '.'; break;
case VK_OEM_PLUS: key_code = '='; break;
case VK_OEM_MINUS: key_code = '-'; break;
case VK_OEM_COMMA: key_code = ','; break;
// symbol-only keys on US keyboards - return ascii
case VK_OEM_1: key_code = ';'; break;
case VK_OEM_2: key_code = '/'; break;
case VK_OEM_3: key_code = '`'; break;
case VK_OEM_4: key_code = '['; break;
case VK_OEM_5: key_code = '\\'; break;
case VK_OEM_6: key_code = ']'; break;
case VK_OEM_7: key_code = '\''; break;
default:
if (keysym >= PX_VK_A && keysym <= PX_VK_Z) {
// VK codes for letter keys - return lower case ascii
key_code = keysym + 'a' - PX_VK_A;
} else {
key_code = 0;
}
}
return key_code;
return keysym < PX_VK2WX_SIZE ? vk2wx[keysym] : 0;
}