allow binding keys without modifiers

This commit is contained in:
nightwing 2012-08-26 01:11:17 +04:00
commit cc2c0900df
3 changed files with 55 additions and 41 deletions

View file

@ -68,20 +68,20 @@ module.exports = {
},
"test: mac hotkeys": function() {
var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "L");
var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "l");
assert.equal(command, this.command);
var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "L");
var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "l");
assert.equal(command, undefined);
},
"test: win hotkeys": function() {
var cm = new CommandManager("win", [this.command]);
var command = cm.findKeyCommand(keys.KEY_MODS.command, "L");
var command = cm.findKeyCommand(keys.KEY_MODS.command, "l");
assert.equal(command, undefined);
var command = cm.findKeyCommand(keys.KEY_MODS.ctrl, "L");
var command = cm.findKeyCommand(keys.KEY_MODS.ctrl, "l");
assert.equal(command, this.command);
},
@ -91,7 +91,7 @@ module.exports = {
this.cm.exec("gotoline");
assert.ok(!this.command.called);
var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "L");
var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "l");
assert.equal(command, null);
},
@ -101,7 +101,7 @@ module.exports = {
this.cm.exec("gotoline");
assert.ok(!this.command.called);
var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "L");
var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "l");
assert.equal(command, null);
},
@ -121,7 +121,7 @@ module.exports = {
assert.ok(command.called);
assert.ok(!this.command.called);
assert.equal(this.cm.findKeyCommand(keys.KEY_MODS.command, "L"), command);
assert.equal(this.cm.findKeyCommand(keys.KEY_MODS.command, "l"), command);
},
"test: adding commands and recording a macro": function() {
@ -156,33 +156,41 @@ module.exports = {
},
"test: bindkeys": function() {
var called = "";
this.cm.addCommands({
cm1: function(editor, arg) {
called += "1" + (arg || "");
},
cm2: function(editor) {
called += "2";
}
});
this.cm.bindKeys({
"Ctrl-L|Command-C": "cm1",
"Ctrl-R": "cm2"
});
var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "C");
var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "c");
assert.equal(command, "cm1");
var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "R");
var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "r");
assert.equal(command, "cm2");
this.cm.bindKeys({
"Ctrl-R": null
});
var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "R");
var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "r");
assert.equal(command, null);
},
"test: binding keys without modifiers": function() {
this.cm.bindKeys({
"R": "cm1",
"Shift-r": "cm2",
"Return": "cm4",
"Enter": "cm3"
});
var command = this.cm.findKeyCommand(-1, "r");
assert.equal(command, "cm1");
var command = this.cm.findKeyCommand(-1, "R");
assert.equal(command, "cm2");
var command = this.cm.findKeyCommand(0, "return");
assert.equal(command, "cm3");
}
};

View file

@ -129,33 +129,34 @@ function HashHandler(config, platform) {
var key = typeof binding == "string" ? binding: binding[this.platform];
this.bindKey(key, command);
};
// accepts keys in the form ctrl+Enter or ctrl-Enter
// keys without modifiers or shift only
this.parseKeys = function(keys) {
var key;
var parts = keys.toLowerCase().split(/[\-\+]([\-\+])?/).filter(function(x){return x});
var key = parts.pop();
var keyCode = keyUtil[key];
if (keyUtil.FUNCTION_KEYS[keyCode])
key = keyUtil.FUNCTION_KEYS[keyCode].toLowerCase();
else if (!parts.length)
return {key: key, hashId: -1};
else if (parts.length == 1 && parts[0] == "shift")
return {key: key.toUpperCase(), hashId: -1};
var hashId = 0;
var parts = keys.toLowerCase().trim().split(/\s*\-\s*/);
for (var i = 0, l = parts.length; i < l; i++) {
if (keyUtil.KEY_MODS[parts[i]])
hashId = hashId | keyUtil.KEY_MODS[parts[i]];
else
key = parts[i] || "-"; //when empty, the splitSafe removed a '-'
for (var i = parts.length; i--;) {
var modifier = keyUtil.KEY_MODS[parts[i]];
if (modifier == null)
throw "invalid modifier " + parts[i] + " in " + keys;
hashId |= modifier;
}
if (parts[0] == "text" && parts.length == 2) {
hashId = -1;
key = parts[1];
}
return {
key: key,
hashId: hashId
};
return {key: key, hashId: hashId};
};
this.findKeyCommand = function findKeyCommand(hashId, keyString) {
var ckbr = this.commmandKeyBinding;
return ckbr[hashId] && ckbr[hashId][keyString.toLowerCase()];
return ckbr[hashId] && ckbr[hashId][keyString];
};
this.handleKeyboard = function(data, hashId, keyString, keyCode) {

View file

@ -108,7 +108,7 @@ var Keys = (function() {
// A reverse map of FUNCTION_KEYS
for (var i in ret.FUNCTION_KEYS) {
var name = ret.FUNCTION_KEYS[i].toUpperCase();
var name = ret.FUNCTION_KEYS[i].toLowerCase();
ret[name] = parseInt(i, 10);
}
@ -118,6 +118,11 @@ var Keys = (function() {
oop.mixin(ret, ret.PRINTABLE_KEYS);
oop.mixin(ret, ret.FUNCTION_KEYS);
// aliases
ret.enter = ret["return"];
ret.escape = ret.esc;
ret.del = ret["delete"];
return ret;
})();
oop.mixin(exports, Keys);