diff --git a/src/Magnum/Platform/EmscriptenApplication.cpp b/src/Magnum/Platform/EmscriptenApplication.cpp index 35b06613a1..4fd093c0a8 100644 --- a/src/Magnum/Platform/EmscriptenApplication.cpp +++ b/src/Magnum/Platform/EmscriptenApplication.cpp @@ -29,6 +29,7 @@ #include #include +#include #include "Magnum/GL/Version.h" #include "Magnum/Platform/GLContext.h" @@ -197,7 +198,239 @@ void EmscriptenApplication::setupCallbacks() { } EmscriptenApplication::KeyEvent::Key EmscriptenApplication::KeyEvent::toKey(const EM_UTF8* keyCode) { - return Key::A; + /* A - Z */ + if(Utility::String::beginsWith(keyCode, "Key")) { + return Key(keyCode[3] - Int(Key::A)); + } + /* Zero - Nine */ + if(Utility::String::beginsWith(keyCode, "Digit")) { + return Key(keyCode[5] - Int(Key::Zero)); + } + /* ArrowDown - ArrowUp */ + if(Utility::String::beginsWith(keyCode, "Arrow")) { + switch(keyCode[5]) { + case 'D': return Key::Down; + case 'L': return Key::Left; + case 'R': return Key::Right; + case 'U': return Key::Up; + default: CORRADE_ASSERT_UNREACHABLE(); + } + } + /* Numpad keys */ + if(Utility::String::beginsWith(keyCode, "Numpad")) { + std::string numKey(keyCode + 6); + if(numKey == "Add") { + return Key::NumAdd; + } + /* Backspace is not NumBackspace + if(numKey == "Backspace") { + return Key::NumBackspace; + } + if(numKey == "Clear") { + return Key::NumClear; + } + if(numKey == "ClearEntry") { + return Key::NumClearEntry; + } + if(numKey == "Comma") { + return Key::NumComma; + } + */ + if(numKey == "Decimal") { + return Key::NumDecimal; + } + if(numKey == "Divide") { + return Key::NumDivide; + } + if(numKey == "Enter") { + return Key::NumEnter; + } + if(numKey == "Equal") { + return Key::NumEqual; + } + /* + if(numKey == "Hash") { + return Key::NumHash; + } + if(numKey == "MemoryAdd") { + return Key::NumMemoryAdd; + } + if(numKey == "MemoryClear") { + return Key::NumMemoryClear; + } + if(numKey == "MemoryRecall") { + return Key::NumMemoryRecall; + } + if(numKey == "MemoryStore") { + return Key::NumMemoryStore; + } + if(numKey == "MemorySubtract") { + return Key::NumMemorySubtract; + } + if(numKey == "ParenLeft") { + return Key::NumParenLeft; + } + if(numKey == "ParenRight") { + return Key::NumParenRight; + } + if(numKey == "Star") { + return Key::NumStar; + } + */ + if(numKey == "Multiply") { + return Key::NumMultiply; + } + if(numKey == "Subtract") { + return Key::NumSubtract; + } + /* Num0 - Num9 */ + Int num = numKey[6] - '0'; + if(num >= 0 && num < 9) { + return Key(num + Int(Key::Zero)); + } + return Key::Unknown; + } + + /* Uh... I don't know these... map? */ + if(strcmp(keyCode, "Backspace") == 0) { + return Key::Backspace; + } + /* + if(strcmp(keyCode, "Backquote") == 0) { + return Key::Backquote; + } + if(strcmp(keyCode, "Backslash") == 0) { + return Key::Backslash; + } + if(strcmp(keyCode, "BracketLeft") == 0) { + return Key::BracketLeft; + } + if(strcmp(keyCode, "BracketRight") == 0) { + return Key::BracketRight; + } + if(strcmp(keyCode, "IntlRo") == 0) { + return Key::IntlRo; + } + if(strcmp(keyCode, "IntlYen") == 0) { + return Key::IntlYen; + } + */ + if(strcmp(keyCode, "Comma") == 0) { + return Key::Comma; + } + if(strcmp(keyCode, "Equal") == 0) { + return Key::Equal; + } + if(strcmp(keyCode, "Minus") == 0) { + return Key::Minus; + } + if(strcmp(keyCode, "Period") == 0) { + return Key::Period; + } + /* + if(strcmp(keyCode, "Quote") == 0) { + return Key::Quote; + } + */ + if(strcmp(keyCode, "Semicolon") == 0) { + return Key::Semicolon; + } + if(strcmp(keyCode, "Slash") == 0) { + return Key::Slash; + } + if(strcmp(keyCode, "AltLeft") == 0) { + return Key::LeftAlt; + } + if(strcmp(keyCode, "AltRight") == 0) { + return Key::RightAlt; + } + if(strcmp(keyCode, "CapsLock") == 0) { + return Key::CapsLock; + } + if(strcmp(keyCode, "ContextMenu") == 0) { + return Key::Menu; + } + if(strcmp(keyCode, "ControlLeft") == 0) { + return Key::LeftCtrl; + } + if(strcmp(keyCode, "ControlRight") == 0) { + return Key::RightCtrl; + } + if(strcmp(keyCode, "Enter") == 0) { + return Key::Enter; + } + if(strcmp(keyCode, "MetaLeft") == 0) { + return Key::LeftSuper; + } + if(strcmp(keyCode, "MetaRight") == 0) { + return Key::RightSuper; + } + if(strcmp(keyCode, "ShiftLeft") == 0) { + return Key::LeftShift; + } + if(strcmp(keyCode, "ShiftRight") == 0) { + return Key::RightShift; + } + if(strcmp(keyCode, "Space") == 0) { + return Key::Space; + } + if(strcmp(keyCode, "Tab") == 0) { + return Key::Tab; + } + if(strcmp(keyCode, "Delete") == 0) { + return Key::Delete; + } + if(strcmp(keyCode, "End") == 0) { + return Key::End; + } + /* + if(strcmp(keyCode, "Help") == 0) { + return Key::Help; + } + */ + if(strcmp(keyCode, "Home") == 0) { + return Key::Home; + } + if(strcmp(keyCode, "Insert") == 0) { + return Key::Insert; + } + if(strcmp(keyCode, "PageDown") == 0) { + return Key::PageDown; + } + if(strcmp(keyCode, "PageUp") == 0) { + return Key::PageUp; + } + if(strcmp(keyCode, "Escape") == 0) { + return Key::Esc; + } + /* + if(strcmp(keyCode, "Fn") == 0) { + return Key::Fn; + } + if(strcmp(keyCode, "FnLock") == 0) { + return Key::FnLock; + } + */ + if(strcmp(keyCode, "PrintScreen") == 0) { + return Key::PrintScreen; + } + if(strcmp(keyCode, "ScrollLock") == 0) { + return Key::ScrollLock; + } + if(strcmp(keyCode, "Pause") == 0) { + return Key::Pause; + } + /* F1 - F12 */ + if(Utility::String::beginsWith(keyCode, "F")) { + if(keyCode[2] != '\0') { + const Int num = keyCode[2] - '0'; + return Key(Int(Key::F10) + num); + } + const Int num = keyCode[1] - '1'; + return Key(Int(Key::F1) + num); + } + + return Key::Unknown; } void EmscriptenApplication::viewportEvent(ViewportEvent&) {} diff --git a/src/Magnum/Platform/EmscriptenApplication.h b/src/Magnum/Platform/EmscriptenApplication.h index 57e7241afd..7798e67de7 100644 --- a/src/Magnum/Platform/EmscriptenApplication.h +++ b/src/Magnum/Platform/EmscriptenApplication.h @@ -821,90 +821,90 @@ class EmscriptenApplication::KeyEvent: public EmscriptenApplication::InputEvent */ RightSuper, - Enter, /**< Enter */ - Esc, /**< Escape */ - - Up, /**< Up arrow */ - Down, /**< Down arrow */ - Left, /**< Left arrow */ - Right, /**< Right arrow */ - Home, /**< Home */ - End, /**< End */ - PageUp, /**< Page up */ - PageDown, /**< Page down */ - Backspace, /**< Backspace */ - Insert, /**< Insert */ - Delete, /**< Delete */ - - F1, /**< F1 */ - F2, /**< F2 */ - F3, /**< F3 */ - F4, /**< F4 */ - F5, /**< F5 */ - F6, /**< F6 */ - F7, /**< F7 */ - F8, /**< F8 */ - F9, /**< F9 */ - F10, /**< F10 */ - F11, /**< F11 */ - F12, /**< F12 */ - - Space, /**< Space */ - Tab, /**< Tab */ - Comma, /**< Comma */ - Period, /**< Period */ - Minus, /**< Minus */ + Enter, /**< Enter */ + Esc, /**< Escape */ + + Up, /**< Up arrow */ + Down, /**< Down arrow */ + Left, /**< Left arrow */ + Right, /**< Right arrow */ + Home, /**< Home */ + End, /**< End */ + PageUp, /**< Page up */ + PageDown, /**< Page down */ + Backspace, /**< Backspace */ + Insert, /**< Insert */ + Delete, /**< Delete */ + + F1, /**< F1 */ + F2, /**< F2 */ + F3, /**< F3 */ + F4, /**< F4 */ + F5, /**< F5 */ + F6, /**< F6 */ + F7, /**< F7 */ + F8, /**< F8 */ + F9, /**< F9 */ + F10, /**< F10 */ + F11, /**< F11 */ + F12, /**< F12 */ + + Space, /**< Space */ + Tab, /**< Tab */ + Comma, /**< Comma */ + Period, /**< Period */ + Minus, /**< Minus */ /* Note: This may only be represented as SHIFT + = */ - Plus, /**< Plus */ - Slash, /**< Slash */ + Plus, /**< Plus */ + Slash, /**< Slash */ /* Note: This may only be represented as SHIFT + 5 */ - Percent, /**< Percent */ - Smicolon, /**< Semicolon */ - Equal, /**< Equal */ - - Zero, /**< Zero */ - One, /**< One */ - Two, /**< Two */ - Three, /**< Three */ - Four, /**< Four */ - Five, /**< Five */ - Six, /**< Six */ - Seven, /**< Seven */ - Eight, /**< Eight */ - Nine, /**< Nine */ - - A, /**< Letter A */ - B, /**< Letter B */ - C, /**< Letter C */ - D, /**< Letter D */ - E, /**< Letter E */ - F, /**< Letter F */ - G, /**< Letter G */ - H, /**< Letter H */ - I, /**< Letter I */ - J, /**< Letter J */ - K, /**< Letter K */ - L, /**< Letter L */ - M, /**< Letter M */ - N, /**< Letter N */ - O, /**< Letter O */ - P, /**< Letter P */ - Q, /**< Letter Q */ - R, /**< Letter R */ - S, /**< Letter S */ - T, /**< Letter T */ - U, /**< Letter U */ - V, /**< Letter V */ - W, /**< Letter W */ - X, /**< Letter X */ - Y, /**< Letter Y */ - Z, /**< Letter Z */ - - CapsLock, /**< Caps lock */ - ScrollLock, /**< Scroll lock */ - NumLock, /**< Num lock */ - PrintScreen,/**< Print screen */ - Pause, /**< Pause */ + Percent, /**< Percent */ + Semicolon, /**< Semicolon */ + Equal, /**< Equal */ + + Zero, /**< Zero */ + One, /**< One */ + Two, /**< Two */ + Three, /**< Three */ + Four, /**< Four */ + Five, /**< Five */ + Six, /**< Six */ + Seven, /**< Seven */ + Eight, /**< Eight */ + Nine, /**< Nine */ + + A = 'A', /**< Letter A */ + B, /**< Letter B */ + C, /**< Letter C */ + D, /**< Letter D */ + E, /**< Letter E */ + F, /**< Letter F */ + G, /**< Letter G */ + H, /**< Letter H */ + I, /**< Letter I */ + J, /**< Letter J */ + K, /**< Letter K */ + L, /**< Letter L */ + M, /**< Letter M */ + N, /**< Letter N */ + O, /**< Letter O */ + P, /**< Letter P */ + Q, /**< Letter Q */ + R, /**< Letter R */ + S, /**< Letter S */ + T, /**< Letter T */ + U, /**< Letter U */ + V, /**< Letter V */ + W, /**< Letter W */ + X, /**< Letter X */ + Y, /**< Letter Y */ + Z, /**< Letter Z */ + + CapsLock, /**< Caps lock */ + ScrollLock, /**< Scroll lock */ + NumLock, /**< Num lock */ + PrintScreen, /**< Print screen */ + Pause, /**< Pause */ Menu, /**< Menu */ NumZero, /**< Numpad zero */ @@ -917,13 +917,13 @@ class EmscriptenApplication::KeyEvent: public EmscriptenApplication::InputEvent NumSeven, /**< Numpad seven */ NumEight, /**< Numpad eight */ NumNine, /**< Numpad nine */ - NumDecimal, /**< Numpad decimal */ - NumDivide, /**< Numpad divide */ - NumMultiply, /**< Numpad multiply */ - NumSubtract, /**< Numpad subtract */ - NumAdd, /**< Numpad add */ - NumEnter, /**< Numpad enter */ - NumEqual /**< Numpad equal */ + NumDecimal, /**< Numpad decimal */ + NumDivide, /**< Numpad divide */ + NumMultiply, /**< Numpad multiply */ + NumSubtract, /**< Numpad subtract */ + NumAdd, /**< Numpad add */ + NumEnter, /**< Numpad enter */ + NumEqual /**< Numpad equal */ }; @@ -942,7 +942,9 @@ class EmscriptenApplication::KeyEvent: public EmscriptenApplication::InputEvent const EmscriptenKeyboardEvent* _event; - /* Translate emscripten key code to Key enum */ + /* Translate emscripten key code (as defined by + https://www.w3.org/TR/uievents-code/#key-code-attribute-value) + to Key enum */ static Key toKey(const EM_UTF8* keyCode); };