Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for removing keybindings

  • Loading branch information...
commit 4c553f577e6426be2be073ba43f007775cc5f652 1 parent ac98afa
@rhburrows authored
Showing with 80 additions and 22 deletions.
  1. +39 −22 src/input.js
  2. +41 −0 test/inputTest.js
View
61 src/input.js
@@ -26,31 +26,41 @@
SPECIAL_KEY_STRINGS[111 + i] = '<F' + i + '>';
}
- InputManager.prototype = {
- bindKey : function(command, f) {
- var keys = command.split('-');
- var modifiers = EMPTY;
- while (keys.length > 1) {
- var mod = keys.shift();
- if (mod == 'S') {
- modifiers = modifiers | KBD_SHIFT;
- }
- if (mod == 'C') {
- modifiers = modifiers | KBD_CONTROL;
- }
- if (mod == 'A') {
- modifiers = modifiers | KBD_ALT;
- }
+ function keyStringFromCommand(command) {
+ var keys = command.split('-');
+ var modifiers = EMPTY;
+ while (keys.length > 1) {
+ var mod = keys.shift();
+ if (mod == 'S') {
+ modifiers = modifiers | KBD_SHIFT;
}
-
- // There's probably a better way to do this
- var keyString = keys[0];
- if (typeof SPECIAL_KEY_STRINGS[keyString] === "undefined" &&
- keyString.indexOf('\\') != 0) {
- keyString = keyString.toUpperCase();
+ if (mod == 'C') {
+ modifiers = modifiers | KBD_CONTROL;
}
+ if (mod == 'A') {
+ modifiers = modifiers | KBD_ALT;
+ }
+ }
- this.bindings[keyString + modifiers] = f;
+ // There's probably a better way to do this
+ var keyString = keys[0];
+ if (typeof SPECIAL_KEY_STRINGS[keyString] === "undefined" &&
+ keyString.indexOf('\\') != 0) {
+ keyString = keyString.toUpperCase();
+ }
+
+ return keyString + modifiers;
+ }
+
+ InputManager.prototype = {
+ bindKey : function(command, f) {
+ var keyString = keyStringFromCommand(command);
+ this.bindings[keyString] = f;
+ },
+
+ unbindKey : function(command) {
+ var keyString = keyStringFromCommand(command);
+ delete this.bindings[keyString];
},
bindKeys : function(bindings) {
@@ -60,6 +70,13 @@
});
},
+ unbindKeys : function(commands) {
+ var self = this;
+ $.each(commands, function(_index, command) {
+ self.unbindKey(command);
+ });
+ },
+
handler : function(editor) {
var bindings = this.bindings;
var characterReader = this.characterReader;
View
41 test/inputTest.js
@@ -168,4 +168,45 @@ test("bindKey with keycodes", function(){
modKeyPress.ctrlKey = true;
inputHandler(modKeyPress);
equals(modCallCount, 1, "It handles escaped codes with modifiers too");
+});
+
+test("unbindKey", function(){
+ var callCount = 0;
+ inputManager.bindKey('C-s', function(editor, ev){
+ callCount++;
+ });
+ var keyPress = jQuery.Event('keydown');
+ keyPress.which = 83;
+ keyPress.ctrlKey = true;
+ inputHandler(keyPress);
+ equals(callCount, 1, "Make sure its bound before testing unbinding");
+
+ inputManager.unbindKey('C-s');
+ inputHandler(keyPress);
+ equals(callCount, 1, "It removes the binding");
+});
+
+test("unbindKeys", function(){
+ var callCountOne = 0,
+ callCountTwo = 0;
+ inputManager.bindKeys({
+ 'C-s' : function(){ callCountOne++; },
+ 'A-s' : function(){ callCountTwo++; }
+ });
+ var keyPressOne = jQuery.Event('keydown'),
+ keyPressTwo = jQuery.Event('keydown');
+ keyPressOne.which = 83;
+ keyPressTwo.which = 83;
+ keyPressOne.ctrlKey = true;
+ keyPressTwo.altKey = true;
+ inputHandler(keyPressOne);
+ inputHandler(keyPressTwo);
+ equals(callCountOne, 1, "Make sure its bound before testing unbinding");
+ equals(callCountTwo, 1, "Make sure its bound before testing unbinding");
+
+ inputManager.unbindKeys(['C-s', 'A-s']);
+ inputHandler(keyPressOne);
+ inputHandler(keyPressTwo);
+ equals(callCountOne, 1, "It removes the binding");
+ equals(callCountTwo, 1, "It handles every binding in the list");
});
Please sign in to comment.
Something went wrong with that request. Please try again.