diff --git a/src/platform/keybindings/keybindingService.resolution.test.ts b/src/platform/keybindings/keybindingService.resolution.test.ts
new file mode 100644
index 00000000000..409a3d1479b
--- /dev/null
+++ b/src/platform/keybindings/keybindingService.resolution.test.ts
@@ -0,0 +1,143 @@
+import { effectScope, markRaw } from 'vue'
+import type { EffectScope } from 'vue'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { KeybindingImpl } from '@/platform/keybindings/keybinding'
+import { useKeybindingService } from '@/platform/keybindings/keybindingService'
+import { useKeybindingStore } from '@/platform/keybindings/keybindingStore'
+import { useSettingStore } from '@/platform/settings/settingStore'
+import { useCommandStore } from '@/stores/commandStore'
+import { useDialogStore } from '@/stores/dialogStore'
+
+const COMMAND_ID = 'Test.Shortcut'
+
+function pressKey(target: EventTarget, init: KeyboardEventInit = {}) {
+ target.dispatchEvent(
+ new KeyboardEvent('keydown', {
+ key: 'k',
+ bubbles: true,
+ cancelable: true,
+ ...init
+ })
+ )
+}
+
+function bindTestShortcut(targetElementId?: string) {
+ useKeybindingStore().addUserKeybinding(
+ new KeybindingImpl({
+ commandId: COMMAND_ID,
+ combo: { key: 'k' },
+ targetElementId
+ })
+ )
+}
+
+function openDialog() {
+ useDialogStore().dialogStack.push({
+ key: 'test-dialog',
+ visible: true,
+ component: markRaw({ template: '
' }),
+ contentProps: {},
+ dialogComponentProps: {},
+ priority: 0
+ })
+}
+
+describe('keybindingService - keydown resolution', () => {
+ let scope: EffectScope
+ const removals: Array<() => void> = []
+
+ beforeEach(() => {
+ vi.mocked(useSettingStore().get).mockImplementation(() => [])
+ vi.mocked(useCommandStore().execute).mockResolvedValue(undefined)
+ useDialogStore().dialogStack.length = 0
+ useCommandStore().registerCommand({ id: COMMAND_ID, function: vi.fn() })
+
+ scope = effectScope()
+ scope.run(() => useKeybindingService().listenForKeydowns())
+ })
+
+ afterEach(() => {
+ scope.stop()
+ removals.splice(0).forEach((remove) => remove())
+ document.body.replaceChildren()
+ })
+
+ it('runs a bound shortcut through the window listeners', () => {
+ bindTestShortcut()
+
+ pressKey(document.body)
+
+ expect(useCommandStore().execute).toHaveBeenCalledOnce()
+ expect(useCommandStore().execute).toHaveBeenCalledWith(COMMAND_ID)
+ })
+
+ it.for([
+ {
+ name: 'a plaintext-only editor',
+ build: (editor: HTMLElement) => {
+ editor.contentEditable = 'plaintext-only'
+ return editor
+ }
+ },
+ {
+ name: 'a focusable element nested in an editor',
+ build: (editor: HTMLElement) => {
+ editor.contentEditable = 'true'
+ const chip = document.createElement('span')
+ chip.tabIndex = 0
+ editor.append(chip)
+ return chip
+ }
+ }
+ ])('leaves a key typed in $name to the editor', ({ build }) => {
+ bindTestShortcut()
+ const editor = document.createElement('div')
+ document.body.append(editor)
+
+ pressKey(build(editor))
+
+ expect(useCommandStore().execute).not.toHaveBeenCalled()
+ })
+
+ it('leaves keys to an input method while it is composing', () => {
+ bindTestShortcut()
+
+ pressKey(document.body, { isComposing: true })
+
+ expect(useCommandStore().execute).not.toHaveBeenCalled()
+ })
+
+ it('does not run a workspace shortcut for a key that also closed a dialog', () => {
+ bindTestShortcut()
+ openDialog()
+ const closeDialog = () => {
+ useDialogStore().dialogStack.length = 0
+ }
+ document.addEventListener('keydown', closeDialog)
+ removals.push(() => document.removeEventListener('keydown', closeDialog))
+
+ pressKey(document.body)
+
+ expect(useCommandStore().execute).not.toHaveBeenCalled()
+ })
+
+ it('runs a canvas-targeted shortcut from the canvas listener', () => {
+ bindTestShortcut('graph-canvas-container')
+ const container = document.createElement('div')
+ container.id = 'graph-canvas-container'
+ const canvas = document.createElement('canvas')
+ container.append(canvas)
+ document.body.append(container)
+ const { executeCanvasKeybinding } = useKeybindingService()
+ const onCanvasKeydown = (event: KeyboardEvent) => {
+ executeCanvasKeybinding(event)
+ }
+ canvas.addEventListener('keydown', onCanvasKeydown, true)
+
+ pressKey(canvas)
+
+ expect(useCommandStore().execute).toHaveBeenCalledOnce()
+ expect(useCommandStore().execute).toHaveBeenCalledWith(COMMAND_ID)
+ })
+})
diff --git a/src/platform/keybindings/keybindingService.ts b/src/platform/keybindings/keybindingService.ts
index eb90bd1a3fb..dcc8eb551d2 100644
--- a/src/platform/keybindings/keybindingService.ts
+++ b/src/platform/keybindings/keybindingService.ts
@@ -1,3 +1,5 @@
+import { useEventListener } from '@vueuse/core'
+
import { isCloud } from '@/platform/distribution/types'
import { useSettingStore } from '@/platform/settings/settingStore'
import { useCommandStore } from '@/stores/commandStore'
@@ -10,6 +12,89 @@ import { KeyComboImpl } from './keyCombo'
import { KeybindingImpl } from './keybinding'
import { useKeybindingStore } from './keybindingStore'
+type KeydownResolution =
+ | { kind: 'unclaimed' }
+ | { kind: 'blockedByModal'; suppressBrowserDefault: boolean }
+ | { kind: 'legacyEscape' }
+ | { kind: 'claimed'; keybinding: KeybindingImpl }
+
+const UNCLAIMED: KeydownResolution = { kind: 'unclaimed' }
+const LEGACY_ESCAPE: KeydownResolution = { kind: 'legacyEscape' }
+
+const CANVAS_CONTAINER_ID = 'graph-canvas-container'
+
+const RUN_COMMAND_IDS = new Set([
+ 'Comfy.QueuePrompt',
+ 'Comfy.QueuePromptFront',
+ 'Comfy.QueueSelectedOutputNodes'
+])
+
+const resolutions = new WeakMap()
+
+function isTextEditingTarget(target: EventTarget | undefined): boolean {
+ if (!(target instanceof Element)) return false
+ if (target instanceof HTMLElement && target.isContentEditable) return true
+ return (
+ target.tagName === 'TEXTAREA' ||
+ target.tagName === 'INPUT' ||
+ (target.tagName === 'SPAN' && target.classList.contains('property_value'))
+ )
+}
+
+function isEscapeOwnedByMenuOrDialog(
+ event: KeyboardEvent,
+ target: EventTarget | undefined
+): boolean {
+ return (
+ event.key === 'Escape' &&
+ target instanceof Element &&
+ target.closest('[role="menu"], [role="menubar"], [role="dialog"]') !== null
+ )
+}
+
+function isLeftToTarget(
+ event: KeyboardEvent,
+ keyCombo: KeyComboImpl,
+ target: EventTarget | undefined
+): boolean {
+ return (
+ keyCombo.isModifier ||
+ event.isComposing ||
+ isEscapeOwnedByMenuOrDialog(event, target) ||
+ (keyCombo.isReservedByTextInput && isTextEditingTarget(target))
+ )
+}
+
+function isWithinTargetElement(
+ keybinding: KeybindingImpl,
+ target: EventTarget | undefined
+): boolean {
+ const targetElementId =
+ keybinding.targetElementId === 'graph-canvas'
+ ? CANVAS_CONTAINER_ID
+ : keybinding.targetElementId
+ if (!targetElementId) return true
+ const container = document.getElementById(targetElementId)
+ return target instanceof Node && container?.contains(target) === true
+}
+
+function isUnmodifiedEscape(event: KeyboardEvent): boolean {
+ return (
+ event.key === 'Escape' && !event.ctrlKey && !event.altKey && !event.metaKey
+ )
+}
+
+function closeLegacyModals() {
+ for (const modal of document.querySelectorAll('.comfy-modal')) {
+ const display = window.getComputedStyle(modal).getPropertyValue('display')
+ if (display !== 'none') {
+ modal.style.display = 'none'
+ break
+ }
+ }
+ for (const dialog of document.querySelectorAll('dialog')) dialog.close()
+}
+
export function useKeybindingService() {
const keybindingStore = useKeybindingStore()
const commandStore = useCommandStore()
@@ -23,115 +108,91 @@ export function useKeybindingService() {
: undefined
}
+ function resolve(event: KeyboardEvent): KeydownResolution {
+ const keyCombo = KeyComboImpl.fromEvent(event)
+ const target = event.composedPath()[0]
+ if (isLeftToTarget(event, keyCombo, target)) return UNCLAIMED
+
+ const keybinding = getExecutableKeybinding(keyCombo)
+ if (!keybinding)
+ return isUnmodifiedEscape(event) ? LEGACY_ESCAPE : UNCLAIMED
+ if (!isWithinTargetElement(keybinding, target)) return UNCLAIMED
+ if (isModalOpen(dialogStore.dialogStack.length)) {
+ return { kind: 'blockedByModal', suppressBrowserDefault: keyCombo.ctrl }
+ }
+ return { kind: 'claimed', keybinding }
+ }
+
+ function resolutionOf(event: KeyboardEvent): KeydownResolution {
+ const resolved = resolutions.get(event)
+ if (resolved) return resolved
+ const resolution = resolve(event)
+ resolutions.set(event, resolution)
+ return resolution
+ }
+
+ function resolveKeydown(event: KeyboardEvent) {
+ resolutions.set(event, resolve(event))
+ }
+
function executeCanvasKeybinding(event: KeyboardEvent): boolean {
if (event.type !== 'keydown' || event.repeat) return false
- if (isModalOpen(dialogStore.dialogStack.length)) return false
- const keybinding = getExecutableKeybinding(KeyComboImpl.fromEvent(event))
- if (keybinding?.targetElementId !== 'graph-canvas-container') return false
+ const resolution = resolutionOf(event)
+ if (
+ resolution.kind !== 'claimed' ||
+ resolution.keybinding.targetElementId !== CANVAS_CONTAINER_ID
+ ) {
+ return false
+ }
- void commandStore.execute(keybinding.commandId)
+ void commandStore.execute(resolution.keybinding.commandId)
event.preventDefault()
event.stopImmediatePropagation()
return true
}
- async function keybindHandler(event: KeyboardEvent) {
- const keyCombo = KeyComboImpl.fromEvent(event)
- if (keyCombo.isModifier) {
- return
- }
-
- const target = event.composedPath()[0] as HTMLElement
- // Let the active menu own Escape without also triggering the global shortcut.
- // `target` is usually the focused element, but when nothing has focus some
- // browsers (e.g. Safari) target the event at `document` instead of
- // `document.body`, which has no `closest` method.
- if (
- event.key === 'Escape' &&
- target instanceof Element &&
- target.closest('[role="menu"], [role="menubar"], [role="dialog"]')
- ) {
+ async function executeKeybinding(keybinding: KeybindingImpl) {
+ if (RUN_COMMAND_IDS.has(keybinding.commandId)) {
+ await commandStore.execute(keybinding.commandId, {
+ metadata: { trigger_source: 'keybinding' }
+ })
return
}
+ await commandStore.execute(keybinding.commandId)
+ }
- if (
- keyCombo.isReservedByTextInput &&
- (target.tagName === 'TEXTAREA' ||
- target.tagName === 'INPUT' ||
- target.contentEditable === 'true' ||
- (target.tagName === 'SPAN' &&
- target.classList.contains('property_value')))
- ) {
+ async function commitAtWindow(
+ keybinding: KeybindingImpl,
+ event: KeyboardEvent
+ ) {
+ if (event.key === 'Escape' && consultEscapeOverride(event)) {
+ if (!event.defaultPrevented) event.preventDefault()
return
}
+ event.preventDefault()
+ await executeKeybinding(keybinding)
+ }
- const keybinding = getExecutableKeybinding(keyCombo)
- if (keybinding) {
- const targetElementId =
- keybinding.targetElementId === 'graph-canvas'
- ? 'graph-canvas-container'
- : keybinding.targetElementId
- if (targetElementId) {
- const container = document.getElementById(targetElementId)
- if (!container?.contains(target)) {
- return
- }
- }
- if (isModalOpen(dialogStore.dialogStack.length)) {
- // Bare keys still have to reach inputs inside the dialog.
- if (keyCombo.ctrl) {
- event.preventDefault()
- }
+ async function keybindHandler(event: KeyboardEvent) {
+ const resolution = resolutionOf(event)
+ switch (resolution.kind) {
+ case 'unclaimed':
return
- }
-
- // A registered override (e.g. the agent composer owning Escape while a
- // turn is running) wins over the default keybinding, but only once an
- // open menu or dialog has already had first refusal above - those are
- // more specific to the moment than "some feature elsewhere is running".
- if (event.key === 'Escape' && consultEscapeOverride(event)) {
- if (!event.defaultPrevented) event.preventDefault()
+ case 'blockedByModal':
+ if (resolution.suppressBrowserDefault) event.preventDefault()
return
- }
-
- event.preventDefault()
- const runCommandIds = new Set([
- 'Comfy.QueuePrompt',
- 'Comfy.QueuePromptFront',
- 'Comfy.QueueSelectedOutputNodes'
- ])
- if (runCommandIds.has(keybinding.commandId)) {
- await commandStore.execute(keybinding.commandId, {
- metadata: {
- trigger_source: 'keybinding'
- }
- })
- } else {
- await commandStore.execute(keybinding.commandId)
- }
- return
- }
-
- if (event.ctrlKey || event.altKey || event.metaKey) {
- return
+ case 'legacyEscape':
+ closeLegacyModals()
+ return
+ case 'claimed':
+ await commitAtWindow(resolution.keybinding, event)
}
+ }
- if (event.key === 'Escape') {
- const modals = document.querySelectorAll('.comfy-modal')
- for (const modal of modals) {
- const modalDisplay = window
- .getComputedStyle(modal)
- .getPropertyValue('display')
-
- if (modalDisplay !== 'none') {
- modal.style.display = 'none'
- break
- }
- }
-
- for (const d of document.querySelectorAll('dialog')) d.close()
- }
+ function listenForKeydowns() {
+ useEventListener(window, 'keydown', resolveKeydown, { capture: true })
+ useEventListener(window, 'keydown', keybindHandler)
}
function registerCoreKeybindings() {
@@ -175,6 +236,7 @@ export function useKeybindingService() {
}
return {
+ listenForKeydowns,
executeCanvasKeybinding,
keybindHandler,
registerCoreKeybindings,
diff --git a/src/views/GraphView.test.ts b/src/views/GraphView.test.ts
index 73586d64fad..0ce58c17ea2 100644
--- a/src/views/GraphView.test.ts
+++ b/src/views/GraphView.test.ts
@@ -158,7 +158,7 @@ vi.mock(import('@/services/autoQueueService'), () => ({
vi.mock(import('@/platform/keybindings/keybindingService'), () => ({
useKeybindingService: () => ({
registerCoreKeybindings: vi.fn(),
- keybindHandler: vi.fn()
+ listenForKeydowns: vi.fn()
})
}))
diff --git a/src/views/GraphView.vue b/src/views/GraphView.vue
index 8cd68292d01..e315e052112 100644
--- a/src/views/GraphView.vue
+++ b/src/views/GraphView.vue
@@ -335,7 +335,7 @@ onBeforeUnmount(() => {
executionStore.unbindExecutionEvents()
})
-useEventListener(window, 'keydown', useKeybindingService().keybindHandler)
+useKeybindingService().listenForKeydowns()
const { wrapWithErrorHandling, wrapWithErrorHandlingAsync } = useErrorHandling()