Skip to content

Commit cb7c647

Browse files
committed
fix(browser): the Right Shift key was not being recognized on Windows system
1 parent d6bf64b commit cb7c647

2 files changed

Lines changed: 49 additions & 16 deletions

File tree

browser/src/components/keyboard/index.tsx

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface AltGrState {
1515
const ALTGR_THRESHOLD_MS = 10;
1616

1717
export const Keyboard = () => {
18+
const os = getOperatingSystem();
1819
const isKeyboardEnabled = useAtomValue(isKeyboardEnableAtom);
1920

2021
const keyboardRef = useRef(new KeyboardReport());
@@ -23,7 +24,7 @@ export const Keyboard = () => {
2324
const isComposing = useRef(false);
2425

2526
useEffect(() => {
26-
if (getOperatingSystem() === 'Windows' && !altGrState.current) {
27+
if (os === 'Windows' && !altGrState.current) {
2728
altGrState.current = { active: false, ctrlLeftTimestamp: 0 };
2829
}
2930

@@ -49,8 +50,8 @@ export const Keyboard = () => {
4950
event.preventDefault();
5051
event.stopPropagation();
5152

52-
const code = event.code;
53-
if (pressedKeys.current.has(code)) {
53+
const code = normalizeKeyCode(event, os);
54+
if (!code || pressedKeys.current.has(code)) {
5455
return;
5556
}
5657

@@ -81,7 +82,7 @@ export const Keyboard = () => {
8182
event.preventDefault();
8283
event.stopPropagation();
8384

84-
const code = event.code;
85+
const code = normalizeKeyCode(event, os);
8586

8687
// Handle AltGr state for Windows
8788
if (altGrState.current?.active) {
@@ -163,6 +164,33 @@ export const Keyboard = () => {
163164
};
164165
}, [isKeyboardEnabled]);
165166

167+
function normalizeKeyCode(event: KeyboardEvent, os?: string): string {
168+
if (event.code) {
169+
return event.code;
170+
}
171+
172+
// Fallback: use event.key + event.location to determine the key
173+
// event.location: 1 = left, 2 = right, 0 = standard (non-positional)
174+
if (event.key === 'Shift') {
175+
if (event.location === 0 && os === 'Windows') {
176+
return 'ShiftRight';
177+
}
178+
return event.location === 2 ? 'ShiftRight' : 'ShiftLeft';
179+
}
180+
181+
if (event.key === 'Control') {
182+
return event.location === 2 ? 'ControlRight' : 'ControlLeft';
183+
}
184+
if (event.key === 'Alt') {
185+
return event.location === 2 ? 'AltRight' : 'AltLeft';
186+
}
187+
if (event.key === 'Meta') {
188+
return event.location === 2 ? 'MetaRight' : 'MetaLeft';
189+
}
190+
191+
return event.code;
192+
}
193+
166194
// Keyboard handler
167195
async function handleKeyEvent(event: { type: 'keydown' | 'keyup'; code: string }): Promise<void> {
168196
const kb = keyboardRef.current;

browser/src/libs/browser/index.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1+
export type System = 'Windows' | 'macOS' | 'Linux' | 'Unknown';
2+
13
export interface BrowserInfo {
2-
os: string;
4+
os: System;
35
isChrome: boolean;
46
}
57

6-
export function getOperatingSystem(): string {
8+
export function getOperatingSystem(): System {
79
if (typeof window === 'undefined') {
810
return 'Unknown';
911
}
1012

11-
const platformSource =
13+
if ('userAgentData' in navigator) {
1214
// @ts-expect-error check userAgentData.platform
13-
'userAgentData' in navigator ? navigator.userAgentData.platform : navigator.platform;
14-
15-
if (!platformSource) {
16-
return 'Unknown';
15+
const platform = navigator.userAgentData?.platform?.toLowerCase();
16+
if (platform) {
17+
if (platform === 'windows') return 'Windows';
18+
if (platform === 'macos') return 'macOS';
19+
if (platform === 'linux' || platform === 'android') return 'Linux';
20+
}
1721
}
1822

19-
const platform = platformSource.toLowerCase();
23+
// Fallback to User Agent
24+
const userAgent = navigator.userAgent;
2025

21-
if (platform.startsWith('win')) return 'Windows';
22-
if (platform.startsWith('mac')) return 'macOS';
23-
if (platform.startsWith('linux')) return 'Linux';
26+
if (/Win/i.test(userAgent)) return 'Windows';
27+
if (/Mac|iPhone|iPod|iPad/i.test(userAgent)) return 'macOS';
28+
if (/Linux|Android/i.test(userAgent)) return 'Linux';
2429

25-
return platformSource;
30+
return 'Unknown';
2631
}
2732

2833
export function isChromeBrowser(): boolean {

0 commit comments

Comments
 (0)