Bug fix for the Keyboard Shortcuts
MAC OS SHORTCUTS: - Toggle Terminal: ⌘ + ` - Toggle Theme: ⌘ + ⌥ + ⇧ + D - Toggle Chat: ⌘ + ⌥ + J - Toggle Settings: ⌘ + ⌥ + S WINDOWS/LINUX SHORTCUTS: - Toggle Terminal: Ctrl + ` - Toggle Theme: Win + Alt + Shift + D - Toggle Chat: Ctrl + Alt + J - Toggle Settings: Ctrl + Alt + S
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import { useStore } from '@nanostores/react';
|
||||
import { useEffect } from 'react';
|
||||
import { shortcutsStore, type Shortcuts } from '~/lib/stores/settings';
|
||||
import { isMac } from '~/utils/os';
|
||||
|
||||
// List of keys that should not trigger shortcuts when typing in input/textarea
|
||||
const INPUT_ELEMENTS = ['input', 'textarea'];
|
||||
|
||||
class ShortcutEventEmitter {
|
||||
#emitter = new EventTarget();
|
||||
@@ -25,55 +29,54 @@ export function useShortcuts(): void {
|
||||
|
||||
useEffect(() => {
|
||||
const handleKeyDown = (event: KeyboardEvent): void => {
|
||||
// Debug logging
|
||||
console.log('Key pressed:', {
|
||||
key: event.key,
|
||||
code: event.code, // This gives us the physical key regardless of modifiers
|
||||
ctrlKey: event.ctrlKey,
|
||||
shiftKey: event.shiftKey,
|
||||
altKey: event.altKey,
|
||||
metaKey: event.metaKey,
|
||||
});
|
||||
|
||||
/*
|
||||
* Check for theme toggle shortcut first (Option + Command + Shift + D)
|
||||
* Use event.code to check for the physical D key regardless of the character produced
|
||||
*/
|
||||
// Don't trigger shortcuts when typing in input fields
|
||||
if (
|
||||
event.code === 'KeyD' &&
|
||||
event.metaKey && // Command (Mac) or Windows key
|
||||
event.altKey && // Option (Mac) or Alt (Windows)
|
||||
event.shiftKey &&
|
||||
!event.ctrlKey
|
||||
document.activeElement &&
|
||||
INPUT_ELEMENTS.includes(document.activeElement.tagName.toLowerCase()) &&
|
||||
!event.altKey && // Allow Alt combinations even in input fields
|
||||
!event.metaKey && // Allow Cmd/Win combinations even in input fields
|
||||
!event.ctrlKey // Allow Ctrl combinations even in input fields
|
||||
) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
shortcuts.toggleTheme.action();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle other shortcuts
|
||||
for (const name in shortcuts) {
|
||||
const shortcut = shortcuts[name as keyof Shortcuts];
|
||||
// Debug logging in development only
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
console.log('Key pressed:', {
|
||||
key: event.key,
|
||||
code: event.code,
|
||||
ctrlKey: event.ctrlKey,
|
||||
shiftKey: event.shiftKey,
|
||||
altKey: event.altKey,
|
||||
metaKey: event.metaKey,
|
||||
target: event.target,
|
||||
});
|
||||
}
|
||||
|
||||
if (name === 'toggleTheme') {
|
||||
continue;
|
||||
} // Skip theme toggle as it's handled above
|
||||
|
||||
// For other shortcuts, check both key and code
|
||||
// Handle shortcuts
|
||||
for (const [name, shortcut] of Object.entries(shortcuts)) {
|
||||
const keyMatches =
|
||||
shortcut.key.toLowerCase() === event.key.toLowerCase() || `Key${shortcut.key.toUpperCase()}` === event.code;
|
||||
|
||||
// Handle ctrlOrMetaKey based on OS
|
||||
const ctrlOrMetaKeyMatches = shortcut.ctrlOrMetaKey
|
||||
? (isMac && event.metaKey) || (!isMac && event.ctrlKey)
|
||||
: true;
|
||||
|
||||
const modifiersMatch =
|
||||
ctrlOrMetaKeyMatches &&
|
||||
(shortcut.ctrlKey === undefined || shortcut.ctrlKey === event.ctrlKey) &&
|
||||
(shortcut.metaKey === undefined || shortcut.metaKey === event.metaKey) &&
|
||||
(shortcut.shiftKey === undefined || shortcut.shiftKey === event.shiftKey) &&
|
||||
(shortcut.altKey === undefined || shortcut.altKey === event.altKey);
|
||||
|
||||
if (keyMatches && modifiersMatch) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
// Prevent default browser behavior if specified
|
||||
if (shortcut.isPreventDefault) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
|
||||
shortcutEventEmitter.dispatch(name as keyof Shortcuts);
|
||||
shortcut.action();
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user