Fix keyboard shortcuts capturing events when node is unfocused

Prevent LayerForge from intercepting Ctrl+C, Ctrl+V, and other keyboard
shortcuts when the canvas is not focused. This was causing unwanted
popups and interfering with other nodes in ComfyUI.

Changes:
- Remove document.body focus check from handlePasteEvent
- Add focus validation to handleKeyDown before processing shortcuts
- Modifier keys (Ctrl, Shift, Alt, Meta) are still tracked globally
- All other shortcuts only trigger when canvas is focused

Fixes issue where paste events were captured globally regardless of focus.
This commit is contained in:
diodiogod
2026-01-10 11:12:31 -03:00
parent 1f1d0aeb7d
commit dd5fc5470f
2 changed files with 28 additions and 8 deletions

View File

@@ -519,14 +519,24 @@ export class CanvasInteractions {
return targetHeight / oldHeight;
}
handleKeyDown(e) {
// Always track modifier keys regardless of focus
if (e.key === 'Control')
this.interaction.isCtrlPressed = true;
if (e.key === 'Meta')
this.interaction.isMetaPressed = true;
if (e.key === 'Shift')
this.interaction.isShiftPressed = true;
if (e.key === 'Alt') {
if (e.key === 'Alt')
this.interaction.isAltPressed = true;
// Check if canvas is focused before handling any shortcuts
const shouldHandle = this.canvas.isMouseOver ||
this.canvas.canvas.contains(document.activeElement) ||
document.activeElement === this.canvas.canvas;
if (!shouldHandle) {
return;
}
// Canvas-specific key handlers (only when focused)
if (e.key === 'Alt') {
e.preventDefault();
}
if (e.key.toLowerCase() === 's') {
@@ -1157,8 +1167,7 @@ export class CanvasInteractions {
async handlePasteEvent(e) {
const shouldHandle = this.canvas.isMouseOver ||
this.canvas.canvas.contains(document.activeElement) ||
document.activeElement === this.canvas.canvas ||
document.activeElement === document.body;
document.activeElement === this.canvas.canvas;
if (!shouldHandle) {
log.debug("Paste event ignored - not focused on canvas");
return;

View File

@@ -646,11 +646,23 @@ export class CanvasInteractions {
}
handleKeyDown(e: KeyboardEvent): void {
// Always track modifier keys regardless of focus
if (e.key === 'Control') this.interaction.isCtrlPressed = true;
if (e.key === 'Meta') this.interaction.isMetaPressed = true;
if (e.key === 'Shift') this.interaction.isShiftPressed = true;
if (e.key === 'Alt') this.interaction.isAltPressed = true;
// Check if canvas is focused before handling any shortcuts
const shouldHandle = this.canvas.isMouseOver ||
this.canvas.canvas.contains(document.activeElement) ||
document.activeElement === this.canvas.canvas;
if (!shouldHandle) {
return;
}
// Canvas-specific key handlers (only when focused)
if (e.key === 'Alt') {
this.interaction.isAltPressed = true;
e.preventDefault();
}
if (e.key.toLowerCase() === 's') {
@@ -664,7 +676,7 @@ export class CanvasInteractions {
this.canvas.shapeTool.activate();
return;
}
// Globalne skróty (Undo/Redo/Copy/Paste)
const mods = this.getModifierState(e);
if (mods.ctrl || mods.meta) {
@@ -1345,9 +1357,8 @@ export class CanvasInteractions {
const shouldHandle = this.canvas.isMouseOver ||
this.canvas.canvas.contains(document.activeElement) ||
document.activeElement === this.canvas.canvas ||
document.activeElement === document.body;
document.activeElement === this.canvas.canvas;
if (!shouldHandle) {
log.debug("Paste event ignored - not focused on canvas");
return;