Path: blob/master/src/pc/controller/controller_emscripten_keyboard.c
7861 views
#ifdef TARGET_WEB12#include <string.h>3#include <emscripten/html5.h>4#include "macros.h"5#include "controller_keyboard.h"67static const struct {8const char *code;9int scancode;10} keymap_browser[] = {11{"Escape", 0x01},12{"Digit1", 0x02 },13{"Digit2", 0x03 },14{"Digit3", 0x04 },15{"Digit4", 0x05 },16{"Digit5", 0x06 },17{"Digit6", 0x07 },18{"Digit7", 0x08 },19{"Digit8", 0x09 },20{"Digit9", 0x0a },21{"Digit0", 0x0b },22{"Minus", 0x0c },23{"Equal", 0x0d },24{"Backspace", 0x0e },25{"Tab", 0x0f },26{"KeyQ", 0x10 },27{"KeyW", 0x11 },28{"KeyE", 0x12 },29{"KeyR", 0x13 },30{"KeyT", 0x14 },31{"KeyY", 0x15 },32{"KeyU", 0x16 },33{"KeyI", 0x17 },34{"KeyO", 0x18 },35{"KeyP", 0x19 },36{"BracketLeft", 0x1a },37{"BracketRight", 0x1b },38{"Enter", 0x1c },39{"ControlLeft", 0x1d },40{"KeyA", 0x1e },41{"KeyS", 0x1f },42{"KeyD", 0x20 },43{"KeyF", 0x21 },44{"KeyG", 0x22 },45{"KeyH", 0x23 },46{"KeyJ", 0x24 },47{"KeyK", 0x25 },48{"KeyL", 0x26 },49{"Semicolon", 0x27 },50{"Quote", 0x28 },51{"Backquote", 0x29 },52{"ShiftLeft", 0x2a },53{"Backslash", 0x2b },54{"KeyZ", 0x2c },55{"KeyX", 0x2d },56{"KeyC", 0x2e },57{"KeyV", 0x2f },58{"KeyB", 0x30 },59{"KeyN", 0x31 },60{"KeyM", 0x32 },61{"Comma", 0x33 },62{"Period", 0x34 },63{"Slash", 0x35 },64{"ShiftRight", 0x36 },65{"NumpadMultiply", 0x37 },66{"AltLeft", 0x38 },67{"Space", 0x39 },68{"CapsLock", 0x3a },69{"F1", 0x3b },70{"F2", 0x3c },71{"F3", 0x3d },72{"F4", 0x3e },73{"F5", 0x3f },74{"F6", 0x40 },75{"F7", 0x41 },76{"F8", 0x42 },77{"F9", 0x43 },78{"F10", 0x44 },79{"NumLock", 0x45 },80{"ScrollLock", 0x46 },81{"Numpad7", 0x47 },82{"Numpad8", 0x48 },83{"Numpad9", 0x49 },84{"NumpadSubtract", 0x4a },85{"Numpad4", 0x4b },86{"Numpad5", 0x4c },87{"Numpad6", 0x4d },88{"NumpadAdd", 0x4e },89{"Numpad1", 0x4f },90{"Numpad2", 0x50 },91{"Numpad3", 0x51 },92{"Numpad0", 0x52 },93{"NumpadDecimal", 0x53 },94{"PrintScreen", 0x54 },95// 0x5596{"IntlBackslash", 0x56 },97{"F11", 0x57 },98{"F12", 0x58 },99{"IntlRo", 0x59 },100//{"Katakana", 0 },101//{"Hiragana", 0 },102{"NumpadEnter", 0x11c },103{"ControlRight", 0x11d },104{"NumpadDivide", 0x135 },105{"AltRight", 0x138 },106{"Home", 0x147 },107{"ArrowUp", 0x148 },108{"PageUp", 0x149 },109{"ArrowLeft", 0x14b },110{"ArrowRight", 0x14d },111{"End", 0x14f },112{"ArrowDown", 0x150 },113{"PageDown", 0x151 },114{"Insert", 0x152 },115{"Delete", 0x153 },116{"Pause", 0x21d },117{"MetaLeft", 0x15b },118{"MetaRight", 0x15c },119{"ContextMenu", 0x15d },120};121122static EM_BOOL controller_emscripten_keyboard_handler(int event_type, const EmscriptenKeyboardEvent *key_event, UNUSED void *user_data) {123for (size_t i = 0; i < sizeof(keymap_browser) / sizeof(keymap_browser[0]); i++) {124if (strcmp(key_event->code, keymap_browser[i].code) == 0) {125if (event_type == EMSCRIPTEN_EVENT_KEYDOWN) {126return keyboard_on_key_down(keymap_browser[i].scancode);127} else if (event_type == EMSCRIPTEN_EVENT_KEYUP) {128return keyboard_on_key_up(keymap_browser[i].scancode);129}130break;131}132}133return EM_FALSE;134}135136static EM_BOOL controller_emscripten_keyboard_blur_handler(UNUSED int event_type, UNUSED const EmscriptenFocusEvent *focus_event, UNUSED void *user_data) {137keyboard_on_all_keys_up();138return EM_TRUE;139}140141void controller_emscripten_keyboard_init(void) {142// Should be #window according to docs, but that crashes143const char *target = EMSCRIPTEN_EVENT_TARGET_WINDOW;144145emscripten_set_keydown_callback(target, NULL, EM_FALSE, controller_emscripten_keyboard_handler);146emscripten_set_keyup_callback(target, NULL, EM_FALSE, controller_emscripten_keyboard_handler);147emscripten_set_blur_callback(target, NULL, EM_FALSE, controller_emscripten_keyboard_blur_handler);148}149150#endif151152153