summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreska <eska@eska.me>2016-04-19 14:15:20 +0200
committereska <eska@eska.me>2016-04-19 17:03:13 +0200
commit45b90f16ca080c601e6bf7038979eabb6b7eaa3e (patch)
treec740055e359458ed08f938b3e6cd8b5d5143f697
parent0e9eefb7d4d3d3b10558ade0fea5ea9656a7cace (diff)
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.
-rw-r--r--platform/javascript/os_javascript.cpp22
1 files changed, 21 insertions, 1 deletions
diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp
index b60e5164b1..2e42e79996 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<OS_JavaScript*>(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<OS_JavaScript*>(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) );