From 1c640b3b8eea80cecb3ba5f41e1fb7c98280b22b Mon Sep 17 00:00:00 2001 From: cheungk Date: Thu, 3 Dec 2015 18:26:50 +0800 Subject: [PATCH] Do not prevent default handling for keydown event except for backspace and tab. --- AUTHORS | 1 + src/proxyClient.js | 16 +++++++- tests/keydown_preventdefault_proxy.cpp | 56 ++++++++++++++++++++++++++ tests/test_browser.py | 56 ++++++++++++++++++++++++++ 4 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 tests/keydown_preventdefault_proxy.cpp diff --git a/AUTHORS b/AUTHORS index 30369fb34a9a9..768d90cf79a6d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -220,3 +220,4 @@ a license to everyone to use it as detailed in LICENSE.) * Mark Achée * Piotr Paczkowski * Braden MacDonald +* Kevin Cheung (copyright owned by Autodesk, Inc.) diff --git a/src/proxyClient.js b/src/proxyClient.js index cbabdfef99d57..bb3d42f3be1f2 100644 --- a/src/proxyClient.js +++ b/src/proxyClient.js @@ -237,10 +237,24 @@ function cloneObject(event) { return ret; }; +// Only prevent default on backspace/tab because we don't want unexpected navigation. +// Do not prevent default on the rest as we need the keypress event. +function shouldPreventDefault(event) { + if (event.type === 'keydown' && event.keyCode !== 8 /* backspace */ && event.keyCode !== 9 /* tab */) { + return false; // keypress, back navigation + } else { + return true; // NO keypress, NO back navigation + } +}; + ['keydown', 'keyup', 'keypress', 'blur', 'visibilitychange'].forEach(function(event) { document.addEventListener(event, function(event) { worker.postMessage({ target: 'document', event: cloneObject(event) }); - event.preventDefault(); + + var preventDefault = shouldPreventDefault(event); + if (true === preventDefault) { + event.preventDefault(); + } }); }); diff --git a/tests/keydown_preventdefault_proxy.cpp b/tests/keydown_preventdefault_proxy.cpp new file mode 100644 index 0000000000000..4fe219470d80d --- /dev/null +++ b/tests/keydown_preventdefault_proxy.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +static int result = 1; + +// The event handler functions can return 1 to suppress the event and disable the default action. That calls event.preventDefault(); +// Returning 0 signals that the event was not consumed by the code, and will allow the event to pass on and bubble up normally. +extern "C" +{ + EM_BOOL keydown_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData) + { + if ((e->keyCode == 65) || (e->keyCode == 8)) + { + result *= 2; + } + else + { + REPORT_RESULT(); + emscripten_run_script("throw 'done'"); + } + return 0; + } +} + +extern "C" +{ + EM_BOOL keypress_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData) + { + result *= 3; + return 0; + } +} + +extern "C" +{ + EM_BOOL keyup_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData) + { + if ((e->keyCode == 65) || (e->keyCode == 8)) + { + result *= 5; + } + return 0; + } +} + +int main(int argc, char **argv) +{ + printf("main argc:%d\n", argc); + + emscripten_set_keydown_callback("#document", 0, 1, keydown_callback); + emscripten_set_keypress_callback("#document", 0, 1, keypress_callback); + emscripten_set_keyup_callback("#document", 0, 1, keyup_callback); + + return 0; +} + diff --git a/tests/test_browser.py b/tests/test_browser.py index 227103046abaa..bd15a2345bb2b 100644 --- a/tests/test_browser.py +++ b/tests/test_browser.py @@ -685,6 +685,62 @@ def post(): self.btest('sdl_key_proxy.c', '223092870', args=['--proxy-to-worker', '--pre-js', 'pre.js', '-s', '''EXPORTED_FUNCTIONS=['_main', '_one']''', '-s', 'NO_EXIT_RUNTIME=1'], manual_reference=True, post_build=post) + def test_keydown_preventdefault_proxy(self): + def post(): + html = open('test.html').read() + html = html.replace('', ''' + +''') + + open('test.html', 'w').write(html) + + self.btest('keydown_preventdefault_proxy.cpp', '300', args=['--proxy-to-worker', '-s', '''EXPORTED_FUNCTIONS=['_main']''', '-s', 'NO_EXIT_RUNTIME=1'], manual_reference=True, post_build=post) + def test_sdl_text(self): open(os.path.join(self.get_dir(), 'pre.js'), 'w').write(''' Module.postRun = function() {