src/video/emscripten/SDL_emscriptenevents.c
changeset 11034 fcb1f3161d2e
parent 11025 2215b66794ba
child 11067 f4d12a22446e
equal deleted inserted replaced
11033:3b8bfd13c61f 11034:fcb1f3161d2e
   488 
   488 
   489 static EM_BOOL
   489 static EM_BOOL
   490 Emscripten_HandleKey(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
   490 Emscripten_HandleKey(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
   491 {
   491 {
   492     Uint32 scancode;
   492     Uint32 scancode;
       
   493     SDL_bool prevent_default;
       
   494     SDL_bool is_nav_key;
   493 
   495 
   494     /* .keyCode is deprecated, but still the most reliable way to get keys */
   496     /* .keyCode is deprecated, but still the most reliable way to get keys */
   495     if (keyEvent->keyCode < SDL_arraysize(emscripten_scancode_table)) {
   497     if (keyEvent->keyCode < SDL_arraysize(emscripten_scancode_table)) {
   496         scancode = emscripten_scancode_table[keyEvent->keyCode];
   498         scancode = emscripten_scancode_table[keyEvent->keyCode];
   497 
   499 
   515             }
   517             }
   516             SDL_SendKeyboardKey(eventType == EMSCRIPTEN_EVENT_KEYDOWN ? SDL_PRESSED : SDL_RELEASED, scancode);
   518             SDL_SendKeyboardKey(eventType == EMSCRIPTEN_EVENT_KEYDOWN ? SDL_PRESSED : SDL_RELEASED, scancode);
   517         }
   519         }
   518     }
   520     }
   519 
   521 
   520     SDL_bool prevent_default = SDL_GetEventState(eventType == EMSCRIPTEN_EVENT_KEYDOWN ? SDL_KEYDOWN : SDL_KEYUP) == SDL_ENABLE;
   522     prevent_default = SDL_GetEventState(eventType == EMSCRIPTEN_EVENT_KEYDOWN ? SDL_KEYDOWN : SDL_KEYUP) == SDL_ENABLE;
   521 
   523 
   522     /* if TEXTINPUT events are enabled we can't prevent keydown or we won't get keypress
   524     /* if TEXTINPUT events are enabled we can't prevent keydown or we won't get keypress
   523      * we need to ALWAYS prevent backspace and tab otherwise chrome takes action and does bad navigation UX
   525      * we need to ALWAYS prevent backspace and tab otherwise chrome takes action and does bad navigation UX
   524      */
   526      */
   525     SDL_bool is_nav_key = keyEvent->keyCode == 8 /* backspace */ ||
   527     is_nav_key = keyEvent->keyCode == 8 /* backspace */ ||
   526                           keyEvent->keyCode == 9 /* tab */ ||
   528                  keyEvent->keyCode == 9 /* tab */ ||
   527                           keyEvent->keyCode == 37 /* left */ ||
   529                  keyEvent->keyCode == 37 /* left */ ||
   528                           keyEvent->keyCode == 38 /* up */ ||
   530                  keyEvent->keyCode == 38 /* up */ ||
   529                           keyEvent->keyCode == 39 /* right */ ||
   531                  keyEvent->keyCode == 39 /* right */ ||
   530                           keyEvent->keyCode == 40 /* down */;
   532                  keyEvent->keyCode == 40 /* down */;
   531 
   533 
   532     if (eventType == EMSCRIPTEN_EVENT_KEYDOWN && SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE && !is_nav_key)
   534     if (eventType == EMSCRIPTEN_EVENT_KEYDOWN && SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE && !is_nav_key)
   533         prevent_default = SDL_FALSE;
   535         prevent_default = SDL_FALSE;
   534 
   536 
   535     return prevent_default;
   537     return prevent_default;
   627 }
   629 }
   628 
   630 
   629 void
   631 void
   630 Emscripten_RegisterEventHandlers(SDL_WindowData *data)
   632 Emscripten_RegisterEventHandlers(SDL_WindowData *data)
   631 {
   633 {
       
   634     const char *keyElement;
       
   635 
   632     /* There is only one window and that window is the canvas */
   636     /* There is only one window and that window is the canvas */
   633     emscripten_set_mousemove_callback("#canvas", data, 0, Emscripten_HandleMouseMove);
   637     emscripten_set_mousemove_callback("#canvas", data, 0, Emscripten_HandleMouseMove);
   634 
   638 
   635     emscripten_set_mousedown_callback("#canvas", data, 0, Emscripten_HandleMouseButton);
   639     emscripten_set_mousedown_callback("#canvas", data, 0, Emscripten_HandleMouseButton);
   636     emscripten_set_mouseup_callback("#document", data, 0, Emscripten_HandleMouseButton);
   640     emscripten_set_mouseup_callback("#document", data, 0, Emscripten_HandleMouseButton);
   649     emscripten_set_touchcancel_callback("#canvas", data, 0, Emscripten_HandleTouch);
   653     emscripten_set_touchcancel_callback("#canvas", data, 0, Emscripten_HandleTouch);
   650 
   654 
   651     emscripten_set_pointerlockchange_callback(NULL, data, 0, Emscripten_HandlePointerLockChange);
   655     emscripten_set_pointerlockchange_callback(NULL, data, 0, Emscripten_HandlePointerLockChange);
   652 
   656 
   653     /* Keyboard events are awkward */
   657     /* Keyboard events are awkward */
   654     const char *keyElement = SDL_GetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT);
   658     keyElement = SDL_GetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT);
   655     if (!keyElement) keyElement = "#window";
   659     if (!keyElement) keyElement = "#window";
   656 
   660 
   657     emscripten_set_keydown_callback(keyElement, data, 0, Emscripten_HandleKey);
   661     emscripten_set_keydown_callback(keyElement, data, 0, Emscripten_HandleKey);
   658     emscripten_set_keyup_callback(keyElement, data, 0, Emscripten_HandleKey);
   662     emscripten_set_keyup_callback(keyElement, data, 0, Emscripten_HandleKey);
   659     emscripten_set_keypress_callback(keyElement, data, 0, Emscripten_HandleKeyPress);
   663     emscripten_set_keypress_callback(keyElement, data, 0, Emscripten_HandleKeyPress);
   666 }
   670 }
   667 
   671 
   668 void
   672 void
   669 Emscripten_UnregisterEventHandlers(SDL_WindowData *data)
   673 Emscripten_UnregisterEventHandlers(SDL_WindowData *data)
   670 {
   674 {
       
   675     const char *target;
       
   676 
   671     /* only works due to having one window */
   677     /* only works due to having one window */
   672     emscripten_set_mousemove_callback("#canvas", NULL, 0, NULL);
   678     emscripten_set_mousemove_callback("#canvas", NULL, 0, NULL);
   673 
   679 
   674     emscripten_set_mousedown_callback("#canvas", NULL, 0, NULL);
   680     emscripten_set_mousedown_callback("#canvas", NULL, 0, NULL);
   675     emscripten_set_mouseup_callback("#document", NULL, 0, NULL);
   681     emscripten_set_mouseup_callback("#document", NULL, 0, NULL);
   687     emscripten_set_touchmove_callback("#canvas", NULL, 0, NULL);
   693     emscripten_set_touchmove_callback("#canvas", NULL, 0, NULL);
   688     emscripten_set_touchcancel_callback("#canvas", NULL, 0, NULL);
   694     emscripten_set_touchcancel_callback("#canvas", NULL, 0, NULL);
   689 
   695 
   690     emscripten_set_pointerlockchange_callback(NULL, NULL, 0, NULL);
   696     emscripten_set_pointerlockchange_callback(NULL, NULL, 0, NULL);
   691 
   697 
   692     const char *target = SDL_GetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT);
   698     target = SDL_GetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT);
   693     if (!target) {
   699     if (!target) {
   694         target = "#window";
   700         target = "#window";
   695     }
   701     }
   696 
   702 
   697     emscripten_set_keydown_callback(target, NULL, 0, NULL);
   703     emscripten_set_keydown_callback(target, NULL, 0, NULL);