From 0f47311610edee978e23ea109cbbf7d55c346fdd Mon Sep 17 00:00:00 2001 From: eska Date: Tue, 19 Apr 2016 14:15:20 +0200 Subject: [PATCH] Add fallback to legacy KeyboardEvent in web export Fallback to KeyboardEvent property `charCode` is absence of both `key` and `char` for retrieval of unicode value. (cherry picked from commit 45b90f16ca080c601e6bf7038979eabb6b7eaa3e) --- platform/javascript/os_javascript.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index b60e5164b14..2e42e79996c 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -99,14 +99,30 @@ static InputEvent _setup_key_event(const EmscriptenKeyboardEvent *emscripten_eve return ev; } +static InputEvent deferred_key_event; + static EM_BOOL _keydown_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) { ERR_FAIL_COND_V(event_type!=EMSCRIPTEN_EVENT_KEYDOWN, false); InputEvent ev = _setup_key_event(key_event); ev.key.pressed = true; + if (ev.key.unicode==0 && keycode_has_unicode(ev.key.scancode)) { + // defer to keypress event for legacy unicode retrieval + deferred_key_event = ev; + return false; // do not suppress keypress event + } static_cast(user_data)->push_input(ev); - return ev.key.scancode!=KEY_UNKNOWN && ev.key.scancode!=0; + return true; +} + +static EM_BOOL _keypress_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) { + + ERR_FAIL_COND_V(event_type!=EMSCRIPTEN_EVENT_KEYPRESS, false); + + deferred_key_event.key.unicode = key_event->charCode; + static_cast(user_data)->push_input(deferred_key_event); + return true; } static EM_BOOL _keyup_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) { @@ -198,6 +214,10 @@ void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int if (result!=EMSCRIPTEN_RESULT_SUCCESS) { ERR_PRINTS( "Error while setting Emscripten keydown callback: Code " + itos(result) ); } + result = emscripten_set_keypress_callback(NULL, this, true, &_keypress_callback); + if (result!=EMSCRIPTEN_RESULT_SUCCESS) { + ERR_PRINTS( "Error while setting Emscripten keypress callback: Code " + itos(result) ); + } result = emscripten_set_keyup_callback(NULL, this, true, &_keyup_callback); if (result!=EMSCRIPTEN_RESULT_SUCCESS) { ERR_PRINTS( "Error while setting Emscripten keyup callback: Code " + itos(result) );