diff --git a/packages/devtools/client/app.vue b/packages/devtools/client/app.vue index c678062348..9a2d703144 100644 --- a/packages/devtools/client/app.vue +++ b/packages/devtools/client/app.vue @@ -8,6 +8,7 @@ import { setupFrameNav } from '~/composables/frame-nav' import { WS_DEBOUNCE_TIME } from '~/composables/rpc' import { registerCommands } from '~/composables/state-commands' import { i18n } from '~/i18n' +import { isDevtoolsShortcut } from '../src/runtime/shared/shortcut' import { wsConnectedOnce } from './composables/rpc' import { useSchemaInput } from './composables/state-schema' import { useDevToolsOptions } from './composables/storage-options' @@ -85,7 +86,7 @@ watchEffect(() => { }) useEventListener('keydown', (e) => { - if (e.code === 'KeyD' && e.altKey) { + if (isDevtoolsShortcut(e, false)) { client.value?.devtools.close() e.preventDefault() } diff --git a/packages/devtools/src/runtime/plugins/view/client.ts b/packages/devtools/src/runtime/plugins/view/client.ts index 04c65f4bcd..bc6c420bfe 100644 --- a/packages/devtools/src/runtime/plugins/view/client.ts +++ b/packages/devtools/src/runtime/plugins/view/client.ts @@ -16,6 +16,7 @@ import { computed, effect, isRef, markRaw, nextTick, reactive, ref, shallowReact import { useAppConfig } from '#imports' import { initTimelineMetrics } from '../../function-metrics-helpers' +import { isDevtoolsShortcut } from '../../shared/shortcut' // Host controls must update the visible Devframe viewer context, rather than // Vite's separate dock-registration context. Target the shared-frame anchor @@ -264,7 +265,7 @@ export async function setupDevToolsClient({ // Shortcut to toggle devtools (opens Vite DevTools panel with Nuxt DevTools entry) window.addEventListener('keydown', (e) => { - if (e.code === 'KeyD' && e.altKey && e.shiftKey) + if (isDevtoolsShortcut(e)) client.devtools.toggle() }) } diff --git a/packages/devtools/src/runtime/shared/shortcut.ts b/packages/devtools/src/runtime/shared/shortcut.ts new file mode 100644 index 0000000000..c1fbbc3b4e --- /dev/null +++ b/packages/devtools/src/runtime/shared/shortcut.ts @@ -0,0 +1,25 @@ +// `code` is the physical QWERTY position, and on macOS Option rewrites `key` +// (`d` -> `∂` / `Î`). `keyCode` (deprecated but universally supported) follows +// the active layout and ignores modifiers. + +export interface DevtoolsShortcutEvent { + altKey: boolean + shiftKey: boolean + ctrlKey?: boolean + metaKey?: boolean + key?: string + keyCode?: number +} + +const D_KEY_CODE = 68 + +export function isDevtoolsShortcut( + event: DevtoolsShortcutEvent, + requireShift = true, +): boolean { + return event.altKey + && (!requireShift || event.shiftKey) + && !event.ctrlKey + && !event.metaKey + && (event.key?.toLowerCase() === 'd' || event.keyCode === D_KEY_CODE) +} diff --git a/packages/devtools/test/devtools-shortcut.test.ts b/packages/devtools/test/devtools-shortcut.test.ts new file mode 100644 index 0000000000..79960c6cdc --- /dev/null +++ b/packages/devtools/test/devtools-shortcut.test.ts @@ -0,0 +1,47 @@ +import { describe, expect, it } from 'vitest' +import { isDevtoolsShortcut } from '../src/runtime/shared/shortcut' + +function createEvent(overrides: Partial[0]> = {}) { + return { + altKey: true, + shiftKey: true, + key: 'd', + keyCode: 68, + ...overrides, + } +} + +describe('isDevtoolsShortcut', () => { + it('matches Shift + Alt + D on a QWERTY layout', () => { + expect(isDevtoolsShortcut(createEvent())).toBe(true) + }) + + it('matches via keyCode when Option rewrites the key (macOS, Workman)', () => { + expect(isDevtoolsShortcut(createEvent({ key: 'Î' }))).toBe(true) + }) + + it('matches the produced key where Option does not rewrite it', () => { + expect(isDevtoolsShortcut(createEvent({ key: 'd', keyCode: 0 }))).toBe(true) + }) + + it('ignores keys that do not type d, like physical KeyD on Colemak', () => { + expect(isDevtoolsShortcut(createEvent({ key: 's', keyCode: 83 }))).toBe(false) + }) + + it('requires Alt', () => { + expect(isDevtoolsShortcut(createEvent({ altKey: false }))).toBe(false) + }) + + it('requires Shift when opening', () => { + expect(isDevtoolsShortcut(createEvent({ shiftKey: false }))).toBe(false) + }) + + it('allows Shift to be optional when closing', () => { + expect(isDevtoolsShortcut(createEvent({ shiftKey: false }), false)).toBe(true) + }) + + it('ignores Ctrl and Meta combinations', () => { + expect(isDevtoolsShortcut(createEvent({ ctrlKey: true }))).toBe(false) + expect(isDevtoolsShortcut(createEvent({ metaKey: true }))).toBe(false) + }) +})