Skip to content

Commit e86fdfb

Browse files
committed
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 e86fdfb

File tree

1 file changed

+26
-40
lines changed

1 file changed

+26
-40
lines changed

site/src/pages/TerminalPage/TerminalPage.tsx

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -468,62 +468,48 @@ const useReloading = (isDisconnected: boolean) => {
468468
"notReloading",
469469
);
470470

471+
// Modifier keys should not trigger a reload.
472+
const ignoredKeys = [
473+
"Alt",
474+
"AltGraph",
475+
"CapsLock",
476+
"Control",
477+
"Fn",
478+
"FnLock",
479+
"Meta",
480+
"NumLock",
481+
"ScrollLock",
482+
"Shift",
483+
"Symbol",
484+
"SymbolLock",
485+
];
486+
471487
// Retry connection on key press when it is disconnected
472488
useEffect(() => {
473489
if (!isDisconnected || status === "reloading") {
474490
return;
475491
}
476492

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-
};
505-
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 reconnecting while modifiers are
495+
// held to cover cases where the terminal unexpectedly tries to
496+
// reconnect like 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)