aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorConrad Irwin2010-05-29 03:28:36 +0100
committerPhil Crosby2010-06-01 21:55:48 -0700
commit9d0061dca6101926da760e159d50151984ae24f8 (patch)
tree4405a8015e4526e68b98886c0c3f99fc16ec1d7d
parent3b412ce951643214f5e0f943e10ff9302fe001b9 (diff)
downloadvimium-9d0061dca6101926da760e159d50151984ae24f8.tar.bz2
Allow mapping to arrow keys and f-keys
Conflicts: commands.js
-rw-r--r--background_page.html10
-rw-r--r--commands.js9
-rw-r--r--lib/keyboardUtils.js19
-rw-r--r--vimiumFrontend.js2
4 files changed, 29 insertions, 11 deletions
diff --git a/background_page.html b/background_page.html
index e85dd7d6..9e43fecd 100644
--- a/background_page.html
+++ b/background_page.html
@@ -15,7 +15,7 @@
var validFirstKeys = {};
var singleKeyCommands = [];
- var hasModifierRegex = /^<[amc]-.>/;
+ var namedKeyRegex = /^(<[amc-].|(?:[amc]-)?[a-z]{2,5}>)(.*)$/;
var defaultSettings = {
scrollStepSize: 60,
@@ -407,15 +407,15 @@
}
function splitKeyIntoFirstAndSecond(key) {
- if (key.search(hasModifierRegex) == 0)
- return { first: key.slice(0, 5), second: key.slice(5) };
+ if (key.search(namedKeyRegex) == 0)
+ return { first: RegExp.$1, second: RegExp.$2 };
else
return { first: key[0], second: key.slice(1) };
}
function getActualKeyStrokeLength(key) {
- if (key.search(hasModifierRegex) == 0)
- return 1 + getActualKeyStrokeLength(key.slice(5));
+ if (key.search(namedKeyRegex) == 0)
+ return 1 + getActualKeyStrokeLength(RegExp.$2);
else
return key.length;
}
diff --git a/commands.js b/commands.js
index 08481280..8e4922f3 100644
--- a/commands.js
+++ b/commands.js
@@ -23,6 +23,11 @@ function mapKeyToCommand(key, command) {
function unmapKey(key) { delete keyToCommandRegistry[key]; }
+function normalizeKey(key) {
+ return key.replace(/<[amc]-/i, function(m){return m.toLowerCase();})
+ .replace(/<([acm]-)?([a-zA-Z0-9]+)>/, function(m, p, k){return "<" + (p ? p : "") + k.toLowerCase() + ">";});
+}
+
function parseCustomKeyMappings(customKeyMappings) {
lines = customKeyMappings.split("\n");
@@ -34,7 +39,7 @@ function parseCustomKeyMappings(customKeyMappings) {
if (lineCommand == "map") {
if (split_line.length != 3) { continue; }
- var key = split_line[1];
+ var key = normalizeKey(split_line[1]);
var vimiumCommand = split_line[2];
if (!availableCommands[vimiumCommand]) { continue }
@@ -45,7 +50,7 @@ function parseCustomKeyMappings(customKeyMappings) {
else if (lineCommand == "unmap") {
if (split_line.length != 2) { continue; }
- var key = split_line[1];
+ var key = normalizeKey(split_line[1]);
console.log("Unmapping", key);
unmapKey(key);
diff --git a/lib/keyboardUtils.js b/lib/keyboardUtils.js
index a47d273f..27ad5e42 100644
--- a/lib/keyboardUtils.js
+++ b/lib/keyboardUtils.js
@@ -1,4 +1,6 @@
-var keyCodes = { ESC: 27, backspace: 8, deleteKey: 46, enter: 13, space: 32 };
+var keyCodes = { ESC: 27, backspace: 8, deleteKey: 46, enter: 13, space: 32, f1: 112, f12: 123};
+var keyNames = { 37: "left", 38: "up", 39: "right", 40: "down" }
+
// This is a mapping of the incorrect keyIdentifiers generated by Webkit on Windows during keydown events to
// the correct identifiers, which are correctly generated on Mac. We require this mapping to properly handle
@@ -26,7 +28,18 @@ else
platform = "Windows";
function getKeyChar(event) {
- if (event.keyIdentifier.slice(0, 2) != "U+") { return ""; }
+ // Not a letter
+ if (event.keyIdentifier.slice(0, 2) != "U+") {
+ // Named key
+ if (keyNames[event.keyCode]) {
+ return keyNames[event.keyCode];
+ }
+ // F-key
+ if (event.keyCode >= keyCodes.f1 && event.keyCode <= keyCodes.f12) {
+ return "f" + (1 + event.keyCode - keyCodes.f1);
+ }
+ return "";
+ }
var keyIdentifier = event.keyIdentifier;
// On Windows, the keyIdentifiers for non-letter keys are incorrect. See
// https://bugs.webkit.org/show_bug.cgi?id=19906 for more details.
@@ -48,4 +61,4 @@ function isPrimaryModifierKey(event) {
function isEscape(event) {
return event.keyCode == keyCodes.ESC ||
(event.ctrlKey && getKeyChar(event) == '['); // c-[ is mapped to ESC in Vim by default.
-} \ No newline at end of file
+}
diff --git a/vimiumFrontend.js b/vimiumFrontend.js
index 2d30c2cf..4346d18f 100644
--- a/vimiumFrontend.js
+++ b/vimiumFrontend.js
@@ -263,7 +263,7 @@ function onKeydown(event) {
for (var i in modifiers)
keyChar = modifiers[i] + "-" + keyChar;
- if (modifiers.length > 0)
+ if (modifiers.length > 0 || keyChar.length > 1)
keyChar = "<" + keyChar + ">";
}
}