Permalink
Browse files

Update input to allow raw keycodes

Now keybindings can be set in the inputManager using the raw keycodes
instead of just the characters that they map to in javascript. This
can be used, for example, when a keyboard binds extra keys but
javascript decodes them as extended ascii chars.

The syntax is to escape the character code using '\' but since
javascript automatically escapes backslashes in strings in practice
the declaration will actually be something like 'C-\\195'

Its probably going to be hard to get these key codes to work across
platforms but it still is helpful especially if you are trying to
trigger a specific keyboard. It wouldn't be too tricky to write a
keyboard detection script that asked for user input and dynamically
set the various codes, though this would be shitty from a user perspective
  • Loading branch information...
1 parent 8b2735d commit 83fd96391191d7fc0551f31f03836f3715278998 @rhburrows committed Jun 23, 2011
Showing with 35 additions and 11 deletions.
  1. +13 −10 src/input.js
  2. +22 −1 test/inputTest.js
View
@@ -24,14 +24,6 @@
SPECIAL_KEY_STRINGS[111 + i] = '<F' + i + '>';
}
- function keyStringFromCode(charCode) {
- if (typeof SPECIAL_KEY_STRINGS[charCode] !== 'undefined') {
- return SPECIAL_KEY_STRINGS[charCode];
- } else {
- return String.fromCharCode(charCode);
- }
- }
-
InputManager.prototype = {
bindKey : function(command, f) {
var keys = command.split('-');
@@ -51,7 +43,9 @@
// There's probably a better way to do this
var keyString = keys[0];
- if (typeof SPECIAL_KEY_STRINGS[keyString] === "undefined") {
+ if (keyString.indexOf('\\') == 0) {
+ keyString = keyString.slice(1);
+ } else if (typeof SPECIAL_KEY_STRINGS[keyString] === "undefined") {
keyString = keyString.toUpperCase();
}
@@ -68,7 +62,13 @@
handler : function(editor) {
var bindings = this.bindings;
return function(e) {
- var keyString = keyStringFromCode(e.which);
+ var keyString;
+ if (typeof SPECIAL_KEY_STRINGS[e.which] !== 'undefined') {
+ keyString = SPECIAL_KEY_STRINGS[e.which];
+ } else {
+ keyString = String.fromCharCode(e.which);
+ }
+
var modifiers = EMPTY;
if (e.shiftKey) {
modifiers = modifiers | KBD_SHIFT;
@@ -83,6 +83,9 @@
if (typeof bindings[keyString + modifiers] !== "undefined") {
e.preventDefault();
return bindings[keyString + modifiers](editor, e);
+ } else if (typeof bindings["" + e.which + modifiers] !== "undefined") {
+ e.preventDefault();
+ return bindings["" + e.which + modifiers](editor, e);
} else {
e.preventDefault();
return true;
View
@@ -147,4 +147,25 @@ testKeyString('<F8>', 119, "F8");
testKeyString('<F9>', 120, "F9");
testKeyString('<F10>', 121, "F10");
testKeyString('<F11>', 122, "F11");
-testKeyString('<F12>', 123, "F12");
+testKeyString('<F12>', 123, "F12");
+
+test("bindKey with keycodes", function(){
+ var callCount = 0;
+ inputManager.bindKey('\\180', function(editor, ev){
+ callCount++;
+ });
+ var keyPress = jQuery.Event('keydown');
+ keyPress.which = 180;
+ inputHandler(keyPress);
+ equals(callCount, 1, "It treats escaped keycodes as the actual code");
+
+ var modCallCount = 0;
+ inputManager.bindKey('C-\\180', function(editor, ev){
+ modCallCount++;
+ });
+ var modKeyPress = jQuery.Event('keydown');
+ modKeyPress.which = 180;
+ modKeyPress.ctrlKey = true;
+ inputHandler(modKeyPress);
+ equals(modCallCount, 1, "It handles escaped codes with modifiers too");
+});

0 comments on commit 83fd963

Please sign in to comment.