-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathkeyboard_utils.js
154 lines (137 loc) · 4.99 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
let mapKeyRegistry = {};
Utils.monitorChromeSessionStorage("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",
"\n": "enter", // on a keypress event of Ctrl+Enter, tested on Chrome 92 and Windows 10
},
init() {
// TODO(philc): Remove this guard clause once Deno has a userAgent.
// https://github.com/denoland/deno/issues/14362
// As of 2022-04-30, Deno does not have userAgent defined on navigator.
if (navigator.userAgent == null) {
this.platform = "Unknown";
return;
}
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;
const canUseEventKey = !Settings.get("ignoreKeyboardLayout") &&
// On MacOS, when alt (option) is pressed, event.key is a symbol. E.g. the <a-c> key press
// yields ç. In such cases, use event.code instead to identify which key was pressed, so that
// the user can intuitively map <a-c> in their keymappings, rather than <a-ç>. See #3197.
!(this.platform == "Mac" && event.altKey);
if (canUseEventKey) {
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.monitorChromeSessionStorage("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();
globalThis.KeyboardUtils = KeyboardUtils;