Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9cab143

Browse files
committedSep 15, 2023
Use modifier properties on keydown event
Instead of manually tracking modifiers with keydown/keyup, as this can miss keys like in the case of ctrl+tab, leaving ctrl in a permanent "on" state. The other modifiers do not appear to be used in conjunction with other keys, except possibly Fn? So this should be more or less the same functionality.
1 parent 88db8cc commit 9cab143

File tree

1 file changed

+25
-39
lines changed

1 file changed

+25
-39
lines changed
 

‎site/src/pages/TerminalPage/TerminalPage.tsx

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -474,56 +474,42 @@ const useReloading = (isDisconnected: boolean) => {
474474
return;
475475
}
476476

477-
// Keep track of modifier keys since we want to avoid reconnecting while
478-
// modifiers are held. This covers cases where the terminal unexpectedly
479-
// tries to reconnect like when pressing ctrl+w, ctrl+r, and so on. This
480-
// will not work if you pressed a modifier before the disconnect and are
481-
// still holding it; if we need to account for that we will need to listen
482-
// for modifier keys while connected as well.
483-
const modifierKeyState: Record<string, boolean> = {
484-
Alt: false,
485-
AltGraph: false,
486-
CapsLock: false,
487-
Control: false,
488-
Fn: false,
489-
FnLock: false,
490-
Meta: false,
491-
NumLock: false,
492-
ScrollLock: false,
493-
Shift: false,
494-
Symbol: false,
495-
SymbolLock: false,
496-
};
497-
498-
const isModifier = (event: KeyboardEvent): boolean => {
499-
return event.key in modifierKeyState;
500-
};
501-
502-
const isModified = (): boolean => {
503-
return Object.values(modifierKeyState).includes(true);
504-
};
477+
// Modifier keys should not trigger a reload.
478+
const ignoredKeys = [
479+
"Alt",
480+
"AltGraph",
481+
"CapsLock",
482+
"Control",
483+
"Fn",
484+
"FnLock",
485+
"Meta",
486+
"NumLock",
487+
"ScrollLock",
488+
"Shift",
489+
"Symbol",
490+
"SymbolLock",
491+
];
505492

506493
const keyDownHandler = (event: KeyboardEvent) => {
507-
if (isModifier(event)) {
508-
modifierKeyState[event.key] = true;
509-
} else if (!isModified()) {
494+
// In addition to ignored keys, avoid reloading while modifiers are held
495+
// to cover cases where the terminal unexpectedly tries to reconnect like
496+
// when pressing ctrl+w, ctrl+r, and so on.
497+
if (
498+
!ignoredKeys.includes(event.key) &&
499+
!event.altKey &&
500+
!event.ctrlKey &&
501+
!event.metaKey &&
502+
!event.shiftKey
503+
) {
510504
setStatus("reloading");
511505
window.location.reload();
512506
}
513507
};
514508

515-
const keyUpHandler = (event: KeyboardEvent) => {
516-
if (isModifier(event)) {
517-
modifierKeyState[event.key] = false;
518-
}
519-
};
520-
521509
document.addEventListener("keydown", keyDownHandler, true);
522-
document.addEventListener("keyup", keyUpHandler, true);
523510

524511
return () => {
525512
document.removeEventListener("keydown", keyDownHandler, true);
526-
document.removeEventListener("keyup", keyUpHandler, true);
527513
};
528514
}, [status, isDisconnected]);
529515

0 commit comments

Comments
 (0)
Failed to load comments.