src/video/emscripten/SDL_emscriptenevents.c
changeset 11034 fcb1f3161d2e
parent 11025 2215b66794ba
child 11067 f4d12a22446e
     1.1 --- a/src/video/emscripten/SDL_emscriptenevents.c	Thu May 25 23:01:59 2017 +0200
     1.2 +++ b/src/video/emscripten/SDL_emscriptenevents.c	Fri May 26 22:45:40 2017 +0200
     1.3 @@ -490,6 +490,8 @@
     1.4  Emscripten_HandleKey(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
     1.5  {
     1.6      Uint32 scancode;
     1.7 +    SDL_bool prevent_default;
     1.8 +    SDL_bool is_nav_key;
     1.9  
    1.10      /* .keyCode is deprecated, but still the most reliable way to get keys */
    1.11      if (keyEvent->keyCode < SDL_arraysize(emscripten_scancode_table)) {
    1.12 @@ -517,17 +519,17 @@
    1.13          }
    1.14      }
    1.15  
    1.16 -    SDL_bool prevent_default = SDL_GetEventState(eventType == EMSCRIPTEN_EVENT_KEYDOWN ? SDL_KEYDOWN : SDL_KEYUP) == SDL_ENABLE;
    1.17 +    prevent_default = SDL_GetEventState(eventType == EMSCRIPTEN_EVENT_KEYDOWN ? SDL_KEYDOWN : SDL_KEYUP) == SDL_ENABLE;
    1.18  
    1.19      /* if TEXTINPUT events are enabled we can't prevent keydown or we won't get keypress
    1.20       * we need to ALWAYS prevent backspace and tab otherwise chrome takes action and does bad navigation UX
    1.21       */
    1.22 -    SDL_bool is_nav_key = keyEvent->keyCode == 8 /* backspace */ ||
    1.23 -                          keyEvent->keyCode == 9 /* tab */ ||
    1.24 -                          keyEvent->keyCode == 37 /* left */ ||
    1.25 -                          keyEvent->keyCode == 38 /* up */ ||
    1.26 -                          keyEvent->keyCode == 39 /* right */ ||
    1.27 -                          keyEvent->keyCode == 40 /* down */;
    1.28 +    is_nav_key = keyEvent->keyCode == 8 /* backspace */ ||
    1.29 +                 keyEvent->keyCode == 9 /* tab */ ||
    1.30 +                 keyEvent->keyCode == 37 /* left */ ||
    1.31 +                 keyEvent->keyCode == 38 /* up */ ||
    1.32 +                 keyEvent->keyCode == 39 /* right */ ||
    1.33 +                 keyEvent->keyCode == 40 /* down */;
    1.34  
    1.35      if (eventType == EMSCRIPTEN_EVENT_KEYDOWN && SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE && !is_nav_key)
    1.36          prevent_default = SDL_FALSE;
    1.37 @@ -629,6 +631,8 @@
    1.38  void
    1.39  Emscripten_RegisterEventHandlers(SDL_WindowData *data)
    1.40  {
    1.41 +    const char *keyElement;
    1.42 +
    1.43      /* There is only one window and that window is the canvas */
    1.44      emscripten_set_mousemove_callback("#canvas", data, 0, Emscripten_HandleMouseMove);
    1.45  
    1.46 @@ -651,7 +655,7 @@
    1.47      emscripten_set_pointerlockchange_callback(NULL, data, 0, Emscripten_HandlePointerLockChange);
    1.48  
    1.49      /* Keyboard events are awkward */
    1.50 -    const char *keyElement = SDL_GetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT);
    1.51 +    keyElement = SDL_GetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT);
    1.52      if (!keyElement) keyElement = "#window";
    1.53  
    1.54      emscripten_set_keydown_callback(keyElement, data, 0, Emscripten_HandleKey);
    1.55 @@ -668,6 +672,8 @@
    1.56  void
    1.57  Emscripten_UnregisterEventHandlers(SDL_WindowData *data)
    1.58  {
    1.59 +    const char *target;
    1.60 +
    1.61      /* only works due to having one window */
    1.62      emscripten_set_mousemove_callback("#canvas", NULL, 0, NULL);
    1.63  
    1.64 @@ -689,7 +695,7 @@
    1.65  
    1.66      emscripten_set_pointerlockchange_callback(NULL, NULL, 0, NULL);
    1.67  
    1.68 -    const char *target = SDL_GetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT);
    1.69 +    target = SDL_GetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT);
    1.70      if (!target) {
    1.71          target = "#window";
    1.72      }