(frontend) support Shift and Alt key when building shortcuts

Add support for Shift and Alt modifiers when building shortcuts,
expanding the range of possible combinations and allowing more expressive
and flexible shortcut definitions.
This commit is contained in:
Ovgodd
2026-02-12 16:53:14 +01:00
committed by aleb_the_flash
parent a2c7becaf4
commit 9b033c55b2
3 changed files with 21 additions and 5 deletions

View File

@@ -1,4 +1,6 @@
export type Shortcut = { export type Shortcut = {
key: string key: string
ctrlKey?: boolean ctrlKey?: boolean
shiftKey?: boolean
altKey?: boolean
} }

View File

@@ -11,11 +11,13 @@ export const useKeyboardShortcuts = () => {
// This approach handles basic shortcuts but isn't comprehensive. // This approach handles basic shortcuts but isn't comprehensive.
// Issues might occur. First draft. // Issues might occur. First draft.
const onKeyDown = async (e: KeyboardEvent) => { const onKeyDown = async (e: KeyboardEvent) => {
const { key, metaKey, ctrlKey } = e const { key, metaKey, ctrlKey, shiftKey, altKey } = e
if (!key) return if (!key) return
const shortcutKey = formatShortcutKey({ const shortcutKey = formatShortcutKey({
key, key,
ctrlKey: ctrlKey || (isMacintosh() && metaKey), ctrlKey: ctrlKey || (isMacintosh() && metaKey),
shiftKey,
altKey,
}) })
const shortcut = shortcutsSnap.shortcuts.get(shortcutKey) const shortcut = shortcutsSnap.shortcuts.get(shortcutKey)
if (!shortcut) return if (!shortcut) return

View File

@@ -4,15 +4,27 @@ import { Shortcut } from '@/features/shortcuts/types'
export const CTRL = 'ctrl' export const CTRL = 'ctrl'
export const formatShortcutKey = (shortcut: Shortcut) => { export const formatShortcutKey = (shortcut: Shortcut) => {
if (shortcut.ctrlKey) return `${CTRL}+${shortcut.key.toUpperCase()}` const parts = []
return shortcut.key.toUpperCase() if (shortcut.ctrlKey) parts.push(CTRL)
if (shortcut.altKey) parts.push('alt')
if (shortcut.shiftKey) parts.push('shift')
parts.push(shortcut.key.toUpperCase())
return parts.join('+')
} }
export const appendShortcutLabel = (label: string, shortcut: Shortcut) => { export const appendShortcutLabel = (label: string, shortcut: Shortcut) => {
if (!shortcut.key) return if (!shortcut.key) return
let formattedKeyLabel = shortcut.key.toLowerCase() const parts: string[] = []
if (shortcut.ctrlKey) { if (shortcut.ctrlKey) {
formattedKeyLabel = `${isMacintosh() ? '⌘' : 'Ctrl'}+${formattedKeyLabel}` parts.push(isMacintosh() ? '⌘' : 'Ctrl')
} }
if (shortcut.altKey) {
parts.push(isMacintosh() ? '⌥' : 'Alt')
}
if (shortcut.shiftKey) {
parts.push('Shift')
}
parts.push(shortcut.key.toLowerCase())
const formattedKeyLabel = parts.join('+')
return `${label} (${formattedKeyLabel})` return `${label} (${formattedKeyLabel})`
} }