-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
keyboard_utils.js
133 lines (117 loc) · 4.53 KB
/
keyboard_utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
let mapKeyRegistry = {};
Utils.monitorChromeStorage("mapKeyRegistry", value => { return mapKeyRegistry = value; });
const KeyboardUtils = {
// This maps event.key key names to Vimium key names.
keyNames: {
"ArrowLeft": "left", "ArrowUp": "up", "ArrowRight": "right", "ArrowDown": "down", " ": "space"
},
init() {
if (navigator.userAgent.indexOf("Mac") !== -1) {
this.platform = "Mac";
} else if (navigator.userAgent.indexOf("Linux") !== -1) {
this.platform = "Linux";
} else {
this.platform = "Windows";
}
},
getKeyChar(event) {
let key;
if (!Settings.get("ignoreKeyboardLayout")) {
key = event.key;
} else if (!event.code) {
key = event.key != null ? event.key : ""; // Fall back to event.key (see #3099).
} else if (event.code.slice(0, 6) === "Numpad") {
// We cannot correctly emulate the numpad, so fall back to event.key; see #2626.
key = event.key;
} else {
// The logic here is from the vim-like-key-notation project (https://github.com/lydell/vim-like-key-notation).
key = event.code;
if (key.slice(0, 3) === "Key") { key = key.slice(3); }
// Translate some special keys to event.key-like strings and handle <Shift>.
if (this.enUsTranslations[key]) {
key = event.shiftKey ? this.enUsTranslations[key][1] : this.enUsTranslations[key][0];
} else if ((key.length === 1) && !event.shiftKey) {
key = key.toLowerCase();
}
}
// It appears that key is not always defined (see #2453).
if (!key) {
return "";
} else if (key in this.keyNames) {
return this.keyNames[key];
} else if (this.isModifier(event)) {
return ""; // Don't resolve modifier keys.
} else if (key.length === 1) {
return key;
} else {
return key.toLowerCase();
}
},
getKeyCharString(event) {
let keyChar = this.getKeyChar(event);
if (!keyChar)
return;
const modifiers = [];
if (event.shiftKey && (keyChar.length === 1)) { keyChar = keyChar.toUpperCase(); }
// These must be in alphabetical order (to match the sorted modifier order in Commands.normalizeKey).
if (event.altKey) { modifiers.push("a"); }
if (event.ctrlKey) { modifiers.push("c"); }
if (event.metaKey) { modifiers.push("m"); }
if (event.shiftKey && (keyChar.length > 1)) { modifiers.push("s"); }
keyChar = [...modifiers, keyChar].join("-");
if (1 < keyChar.length) { keyChar = `<${keyChar}>`; }
keyChar = mapKeyRegistry[keyChar] != null ? mapKeyRegistry[keyChar] : keyChar;
return keyChar;
},
isEscape: (function() {
let useVimLikeEscape = true;
Utils.monitorChromeStorage("useVimLikeEscape", value => useVimLikeEscape = value);
return function(event) {
// <c-[> is mapped to Escape in Vim by default.
// Escape with a keyCode 229 means that this event comes from IME, and should not be treated as a
// direct/normal Escape event. IME will handle the event, not vimium.
// See https://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html
return ((event.key === "Escape") && (event.keyCode !== 229)) ||
(useVimLikeEscape && (this.getKeyCharString(event) === "<c-[>"));
};
})(),
isBackspace(event) {
return ["Backspace", "Delete"].includes(event.key);
},
isPrintable(event) {
const s = this.getKeyCharString(event);
return s && s.length == 1;
},
isModifier(event) {
return ["Control", "Shift", "Alt", "OS", "AltGraph", "Meta"].includes(event.key);
},
enUsTranslations: {
"Backquote": ["`", "~"],
"Minus": ["-", "_"],
"Equal": ["=", "+"],
"Backslash": ["\\","|"],
"IntlBackslash": ["\\","|"],
"BracketLeft": ["[", "{"],
"BracketRight": ["]", "}"],
"Semicolon": [";", ":"],
"Quote": ["'", '"'],
"Comma": [",", "<"],
"Period": [".", ">"],
"Slash": ["/", "?"],
"Space": [" ", " "],
"Digit1": ["1", "!"],
"Digit2": ["2", "@"],
"Digit3": ["3", "#"],
"Digit4": ["4", "$"],
"Digit5": ["5", "%"],
"Digit6": ["6", "^"],
"Digit7": ["7", "&"],
"Digit8": ["8", "*"],
"Digit9": ["9", "("],
"Digit0": ["0", ")"]
}
};
KeyboardUtils.init();
root = typeof exports !== 'undefined' && exports !== null ? exports : (window.root != null ? window.root : (window.root = {}));
root.KeyboardUtils = KeyboardUtils;
if (typeof exports === 'undefined' || exports === null) { extend(window, root); }