aboutsummaryrefslogtreecommitdiff
path: root/emccpre.js
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>2013-04-05 15:49:27 +0000
committerSimon Tatham <anakin@pobox.com>2013-04-05 15:49:27 +0000
commit2360b77812e184902e44c7f4ea2f6fc79729e370 (patch)
treed2d36b7537da580109e3b4f522da4585f70f92f8 /emccpre.js
parent5dc559c8be1b8f6ed15f560433f25c952c874f93 (diff)
downloadpuzzles-2360b77812e184902e44c7f4ea2f6fc79729e370.zip
puzzles-2360b77812e184902e44c7f4ea2f6fc79729e370.tar.gz
puzzles-2360b77812e184902e44c7f4ea2f6fc79729e370.tar.bz2
puzzles-2360b77812e184902e44c7f4ea2f6fc79729e370.tar.xz
Rewrite the JS keyboard handling to cope with IE and Chrome.
Unlike Firefox, IE and Chrome don't generate keypress events at all if you suppress the default handling of keydowns. Therefore, we have to figure out everything from the keydown event, because if we unsuppress the default handling of any keydowns then we'll get annoyances like ^R going back to meaning reload-page rather than redo-move. [originally from svn r9810]
Diffstat (limited to 'emccpre.js')
-rw-r--r--emccpre.js25
1 files changed, 10 insertions, 15 deletions
diff --git a/emccpre.js b/emccpre.js
index 66e6354..701c7f7 100644
--- a/emccpre.js
+++ b/emccpre.js
@@ -157,26 +157,21 @@ function initPuzzle() {
}
};
- // Set up keyboard handlers. We expect ordinary keys (with a
- // charCode) to be handled by onkeypress, but function keys
- // (arrows etc) to be handled by onkeydown.
- //
- // We also call event.preventDefault() in both handlers. 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 can indulge their instinct to press ^R for redo,
- // for example, without accidentally reloading the page.
- key = Module.cwrap('key', 'void',
- ['number', 'number', 'number', 'number']);
+ // Set up keyboard handlers. We do all the actual keyboard
+ // handling in onkeydown; but we also call event.preventDefault()
+ // in both the keydown and keypress handlers. 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
+ // can indulge their instinct to press ^R for redo, for example,
+ // without accidentally reloading the page.
+ key = Module.cwrap('key', 'void', ['number', 'number', 'string',
+ 'string', 'number', 'number']);
onscreen_canvas.onkeydown = function(event) {
- key(event.keyCode, event.charCode,
+ key(event.keyCode, event.charCode, event.key, event.char,
event.shiftKey ? 1 : 0, event.ctrlKey ? 1 : 0);
event.preventDefault();
};
onscreen_canvas.onkeypress = function(event) {
- if (event.charCode != 0)
- key(event.keyCode, event.charCode,
- event.shiftKey ? 1 : 0, event.ctrlKey ? 1 : 0);
event.preventDefault();
};