Skip to content

Commit

Permalink
Key Mapping
Browse files Browse the repository at this point in the history
Signed-off-by: Squareys <squareys@googlemail.com>
  • Loading branch information
Squareys committed Apr 10, 2019
1 parent c99fac6 commit 2d9d36d
Show file tree
Hide file tree
Showing 2 changed files with 326 additions and 91 deletions.
235 changes: 234 additions & 1 deletion src/Magnum/Platform/EmscriptenApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <emscripten/emscripten.h>

#include <Corrade/Utility/Debug.h>
#include <Corrade/Utility/String.h>

#include "Magnum/GL/Version.h"
#include "Magnum/Platform/GLContext.h"
Expand Down Expand Up @@ -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&) {}
Expand Down
Loading

0 comments on commit 2d9d36d

Please sign in to comment.