Skip to content

Commit

Permalink
Do not prevent default handling for keydown event except for backspac…
Browse files Browse the repository at this point in the history
…e and tab.
  • Loading branch information
awtcode committed Dec 3, 2015
1 parent 5b0fb86 commit 1c640b3
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,4 @@ a license to everyone to use it as detailed in LICENSE.)
* Mark Achée <mark@achee.com>
* Piotr Paczkowski <kontakt@trzeci.eu>
* Braden MacDonald <braden@bradenmacdonald.com>
* Kevin Cheung <kevin.cheung@autodesk.com> (copyright owned by Autodesk, Inc.)
16 changes: 15 additions & 1 deletion src/proxyClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
});

Expand Down
56 changes: 56 additions & 0 deletions tests/keydown_preventdefault_proxy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <stdio.h>
#include <emscripten.h>
#include <html5.h>
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;
}

56 changes: 56 additions & 0 deletions tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('</body>', '''
<script>
function keydown(c) {
var event = document.createEvent("KeyboardEvent");
event.initKeyEvent("keydown", true, true, window,
0, 0, 0, 0,
c, c);
return document.dispatchEvent(event);
}
function keypress(c) {
var event = document.createEvent("KeyboardEvent");
event.initKeyEvent("keypress", true, true, window,
0, 0, 0, 0,
c, c);
return document.dispatchEvent(event);
}
function keyup(c) {
var event = document.createEvent("KeyboardEvent");
event.initKeyEvent("keyup", true, true, window,
0, 0, 0, 0,
c, c);
return document.dispatchEvent(event);
}
function sendKey(c) {
// Simulate the sending of the keypress event when the
// prior keydown event is not prevent defaulted.
if (keydown(c) === false) {
console.log('keydown prevent defaulted, NOT sending keypress!!!');
} else {
keypress(c);
}
keyup(c);
}
// Send 'a'. Simulate the sending of the keypress event when the
// prior keydown event is not prevent defaulted.
sendKey(65);
// Send backspace. Keypress should not be sent over as default handling of
// the Keydown event should be prevented.
sendKey(8);
keydown(100);keyup(100); // trigger the end
</script>
</body>''')

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() {
Expand Down

0 comments on commit 1c640b3

Please sign in to comment.