Skip to content

Commit d5953ed

Browse files
committed
call keybardRemapper when no binding found in mousetrap
1 parent bfadec0 commit d5953ed

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

ui/lib/src/keyboardRemapper.ts

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,33 @@ export class KeyboardRemapper {
2323
}
2424
}
2525

26+
const alphabet: string[] = Array.from(
27+
{ length: 26 },
28+
(_, i) => String.fromCharCode(97 + i), // 'a' is char code 97
29+
);
30+
31+
const digits: string[] = Array.from(
32+
{ length: 10 },
33+
(_, i) => String.fromCharCode(48 + i), // '0' is char code 48
34+
);
35+
36+
const qwertyMapLowerCaseLetters: Mapping[] = alphabet.map(
37+
(letter: string) => <Mapping>{ key: letter, code: 'Key' + letter.toUpperCase(), shiftKey: false },
38+
);
39+
40+
const qwertyMapUpperCaseLetters: Mapping[] = alphabet.map(
41+
(letter: string) =>
42+
<Mapping>{ key: letter.toUpperCase(), code: 'Key' + letter.toUpperCase(), shiftKey: true },
43+
);
44+
45+
const qwertyMapDigits: Mapping[] = digits.map(
46+
(letter: string) => <Mapping>{ key: letter, code: 'Digit' + letter.toUpperCase(), shiftKey: false },
47+
);
48+
2649
const qwertyMappings: Mapping[] = [
27-
...qwertyMapUpperCaseLetters(),
28-
...qwertyMapLowerCaseLetters(),
29-
...qwertyMapDigits(),
50+
...qwertyMapUpperCaseLetters,
51+
...qwertyMapLowerCaseLetters,
52+
...qwertyMapDigits,
3053
{
3154
key: '?',
3255
code: 'Slash',
@@ -53,32 +76,3 @@ const qwertyMappings: Mapping[] = [
5376
shiftKey: true,
5477
},
5578
];
56-
57-
const alphabet: string[] = Array.from(
58-
{ length: 26 },
59-
(_, i) => String.fromCharCode(97 + i), // 'a' is char code 97
60-
);
61-
62-
function qwertyMapLowerCaseLetters(): Mapping[] {
63-
return alphabet.map(
64-
(letter: string) => <Mapping>{ key: letter, code: 'Key' + letter.toUpperCase(), shiftKey: false },
65-
);
66-
}
67-
68-
function qwertyMapUpperCaseLetters(): Mapping[] {
69-
return alphabet.map(
70-
(letter: string) =>
71-
<Mapping>{ key: letter.toUpperCase(), code: 'Key' + letter.toUpperCase(), shiftKey: true },
72-
);
73-
}
74-
75-
const digits: string[] = Array.from(
76-
{ length: 10 },
77-
(_, i) => String.fromCharCode(48 + i), // '0' is char code 48
78-
);
79-
80-
function qwertyMapDigits(): Mapping[] {
81-
return digits.map(
82-
(letter: string) => <Mapping>{ key: letter, code: 'Digit' + letter.toUpperCase(), shiftKey: false },
83-
);
84-
}

ui/site/src/mousetrap.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { KeyboardRemapper } from 'lib/keyboardRemapper';
19+
1820
type Action = 'keypress' | 'keydown' | 'keyup';
1921

2022
type Callback = (e: KeyboardEvent) => void;
@@ -32,6 +34,8 @@ interface Binding {
3234
action: Action;
3335
}
3436

37+
const qwertyRemapper = new KeyboardRemapper('QWERTY');
38+
3539
const MAP: Record<string, string> = {
3640
8: 'backspace',
3741
9: 'tab',
@@ -209,10 +213,11 @@ export default class Mousetrap {
209213
};
210214

211215
private getMatches = (e: KeyboardEvent): Binding[] => {
216+
const { mapped: mapped, key: mappedKey } = qwertyRemapper.map(e);
212217
const key = keyFromEvent(e);
213218
const action = e.type;
214219
const modifiers = action === 'keyup' && isModifier(key) ? [key] : eventModifiers(e);
215-
return (this.bindings[key] || []).filter(
220+
return (this.bindings[key] || (mapped && this.bindings[mappedKey]) || []).filter(
216221
binding =>
217222
action === binding.action &&
218223
// Chrome will not fire a keypress if meta or control is down,

0 commit comments

Comments
 (0)