diff --git a/emcc.c b/emcc.c index 62ea9f8..3c9850a 100644 --- a/emcc.c +++ b/emcc.c @@ -260,9 +260,10 @@ void mousemove(int x, int y, int buttons) } /* - * Keyboard handler called from JS. + * Keyboard handler called from JS. Returns true if the key was + * handled and hence the keydown event should be cancelled. */ -void key(int keycode, const char *key, const char *chr, int location, +bool key(int keycode, const char *key, const char *chr, int location, bool shift, bool ctrl) { /* Key location constants from JavaScript. */ @@ -366,7 +367,9 @@ void key(int keycode, const char *key, const char *chr, int location, midend_process_key(me, 0, 0, keyevent); update_undo_redo(); + return true; /* We've probably handled the event. */ } + return false; /* Event not handled, because we don't even recognise it. */ } /* diff --git a/emccpre.js b/emccpre.js index ee06019..37e5829 100644 --- a/emccpre.js +++ b/emccpre.js @@ -305,17 +305,17 @@ function initPuzzle() { }; // Set up keyboard handlers. We call event.preventDefault() - // in the keydown handler. This means that - // while the canvas itself has focus, _all_ keypresses go only to - // the puzzle - so users of this puzzle collection in other media + // in the keydown handler if it looks like we might have + // done something with the key. This means that users + // of this puzzle collection in other media // can indulge their instinct to press ^R for redo, for example, // without accidentally reloading the page. - key = Module.cwrap('key', 'void', ['number', 'string', 'string', - 'number', 'number', 'number']); + key = Module.cwrap('key', 'boolean', ['number', 'string', 'string', + 'number', 'number', 'number']); onscreen_canvas.onkeydown = function(event) { - key(event.keyCode, event.key, event.char, event.location, - event.shiftKey ? 1 : 0, event.ctrlKey ? 1 : 0); - event.preventDefault(); + if (key(event.keyCode, event.key, event.char, event.location, + event.shiftKey ? 1 : 0, event.ctrlKey ? 1 : 0)) + event.preventDefault(); }; // command() is a C function called to pass back events which