allow binding keys without modifiers
This commit is contained in:
parent
ad9b656c14
commit
cc2c0900df
3 changed files with 55 additions and 41 deletions
|
|
@ -68,20 +68,20 @@ module.exports = {
|
||||||
},
|
},
|
||||||
|
|
||||||
"test: mac hotkeys": function() {
|
"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);
|
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);
|
assert.equal(command, undefined);
|
||||||
},
|
},
|
||||||
|
|
||||||
"test: win hotkeys": function() {
|
"test: win hotkeys": function() {
|
||||||
var cm = new CommandManager("win", [this.command]);
|
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);
|
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);
|
assert.equal(command, this.command);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -91,7 +91,7 @@ module.exports = {
|
||||||
this.cm.exec("gotoline");
|
this.cm.exec("gotoline");
|
||||||
assert.ok(!this.command.called);
|
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);
|
assert.equal(command, null);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -101,7 +101,7 @@ module.exports = {
|
||||||
this.cm.exec("gotoline");
|
this.cm.exec("gotoline");
|
||||||
assert.ok(!this.command.called);
|
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);
|
assert.equal(command, null);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -121,7 +121,7 @@ module.exports = {
|
||||||
assert.ok(command.called);
|
assert.ok(command.called);
|
||||||
assert.ok(!this.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() {
|
"test: adding commands and recording a macro": function() {
|
||||||
|
|
@ -156,33 +156,41 @@ module.exports = {
|
||||||
},
|
},
|
||||||
|
|
||||||
"test: bindkeys": function() {
|
"test: bindkeys": function() {
|
||||||
var called = "";
|
|
||||||
this.cm.addCommands({
|
|
||||||
cm1: function(editor, arg) {
|
|
||||||
called += "1" + (arg || "");
|
|
||||||
},
|
|
||||||
cm2: function(editor) {
|
|
||||||
called += "2";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
this.cm.bindKeys({
|
this.cm.bindKeys({
|
||||||
"Ctrl-L|Command-C": "cm1",
|
"Ctrl-L|Command-C": "cm1",
|
||||||
"Ctrl-R": "cm2"
|
"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");
|
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");
|
assert.equal(command, "cm2");
|
||||||
|
|
||||||
this.cm.bindKeys({
|
this.cm.bindKeys({
|
||||||
"Ctrl-R": null
|
"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);
|
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");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -129,33 +129,34 @@ function HashHandler(config, platform) {
|
||||||
var key = typeof binding == "string" ? binding: binding[this.platform];
|
var key = typeof binding == "string" ? binding: binding[this.platform];
|
||||||
this.bindKey(key, command);
|
this.bindKey(key, command);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// accepts keys in the form ctrl+Enter or ctrl-Enter
|
||||||
|
// keys without modifiers or shift only
|
||||||
this.parseKeys = function(keys) {
|
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 hashId = 0;
|
||||||
var parts = keys.toLowerCase().trim().split(/\s*\-\s*/);
|
for (var i = parts.length; i--;) {
|
||||||
|
var modifier = keyUtil.KEY_MODS[parts[i]];
|
||||||
for (var i = 0, l = parts.length; i < l; i++) {
|
if (modifier == null)
|
||||||
if (keyUtil.KEY_MODS[parts[i]])
|
throw "invalid modifier " + parts[i] + " in " + keys;
|
||||||
hashId = hashId | keyUtil.KEY_MODS[parts[i]];
|
hashId |= modifier;
|
||||||
else
|
|
||||||
key = parts[i] || "-"; //when empty, the splitSafe removed a '-'
|
|
||||||
}
|
}
|
||||||
|
return {key: key, hashId: hashId};
|
||||||
if (parts[0] == "text" && parts.length == 2) {
|
|
||||||
hashId = -1;
|
|
||||||
key = parts[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
key: key,
|
|
||||||
hashId: hashId
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.findKeyCommand = function findKeyCommand(hashId, keyString) {
|
this.findKeyCommand = function findKeyCommand(hashId, keyString) {
|
||||||
var ckbr = this.commmandKeyBinding;
|
var ckbr = this.commmandKeyBinding;
|
||||||
return ckbr[hashId] && ckbr[hashId][keyString.toLowerCase()];
|
return ckbr[hashId] && ckbr[hashId][keyString];
|
||||||
};
|
};
|
||||||
|
|
||||||
this.handleKeyboard = function(data, hashId, keyString, keyCode) {
|
this.handleKeyboard = function(data, hashId, keyString, keyCode) {
|
||||||
|
|
|
||||||
|
|
@ -108,7 +108,7 @@ var Keys = (function() {
|
||||||
|
|
||||||
// A reverse map of FUNCTION_KEYS
|
// A reverse map of FUNCTION_KEYS
|
||||||
for (var i in ret.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);
|
ret[name] = parseInt(i, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -118,6 +118,11 @@ var Keys = (function() {
|
||||||
oop.mixin(ret, ret.PRINTABLE_KEYS);
|
oop.mixin(ret, ret.PRINTABLE_KEYS);
|
||||||
oop.mixin(ret, ret.FUNCTION_KEYS);
|
oop.mixin(ret, ret.FUNCTION_KEYS);
|
||||||
|
|
||||||
|
// aliases
|
||||||
|
ret.enter = ret["return"];
|
||||||
|
ret.escape = ret.esc;
|
||||||
|
ret.del = ret["delete"];
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
})();
|
})();
|
||||||
oop.mixin(exports, Keys);
|
oop.mixin(exports, Keys);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue