From 7c0bc3b52173fe40d9f2a7d61a4281ed6ac6e1d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Rodr=C3=ADguez=20Montalvo?= Date: Fri, 13 Sep 2024 11:25:30 +0200 Subject: [PATCH] Handle shortcuts for single key shortcut and Ctrl or Meta combinations --- src/pods/toolbar/shortcut/shortcut.const.ts | 10 +++++----- src/pods/toolbar/shortcut/shortcut.hook.tsx | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/pods/toolbar/shortcut/shortcut.const.ts b/src/pods/toolbar/shortcut/shortcut.const.ts index fd25ef86..6158771e 100644 --- a/src/pods/toolbar/shortcut/shortcut.const.ts +++ b/src/pods/toolbar/shortcut/shortcut.const.ts @@ -8,31 +8,31 @@ export const SHORTCUTS: Shortcut = { delete: { description: 'Delete', id: 'delete-button-shortcut', - targetKey: ['Backspace'], + targetKey: ['backspace'], targetKeyLabel: 'Backspace', }, copy: { description: 'Copy', id: 'copy-button-shortcut', - targetKey: ['c'], + targetKey: ['Ctrl+c', 'Meta+c'], targetKeyLabel: 'Ctrl + C', }, paste: { description: 'Paste', id: 'paste-button-shortcut', - targetKey: ['v'], + targetKey: ['Ctrl+v', 'Meta+v'], targetKeyLabel: 'Ctrl + V', }, undo: { description: 'Undo', id: 'undo-button-shortcut', - targetKey: ['z'], + targetKey: ['Ctrl+z', 'Meta+z'], targetKeyLabel: 'Ctrl + Z', }, redo: { description: 'Redo', id: 'red-button-shortcut', - targetKey: ['y'], + targetKey: ['Ctrl+y', 'Meta+y'], targetKeyLabel: 'Ctrl + Y', }, }; diff --git a/src/pods/toolbar/shortcut/shortcut.hook.tsx b/src/pods/toolbar/shortcut/shortcut.hook.tsx index 49278cdb..ccf8ca2e 100644 --- a/src/pods/toolbar/shortcut/shortcut.hook.tsx +++ b/src/pods/toolbar/shortcut/shortcut.hook.tsx @@ -13,15 +13,15 @@ export const useShortcut = ({ targetKey, callback }: ShortcutHookProps) => { //const isAltKeyPressed = event.getModifierState('Alt'); //const isCtrlKeyPressed = event.getModifierState('Control'); const isCtrlOrCmdPressed = event.ctrlKey || event.metaKey; + const ctrlKey = isMacOS() ? 'Meta' : 'Ctrl'; + const pressedKey = event.key.toLowerCase(); if ( - (isWindowsOrLinux() && isCtrlOrCmdPressed) || - (isMacOS() && isCtrlOrCmdPressed) + targetKey.includes(pressedKey) || + (isCtrlOrCmdPressed && targetKey.includes(`${ctrlKey}+${pressedKey}`)) ) { - if (targetKey.includes(event.key)) { - event.preventDefault(); - callback(); - } + event.preventDefault(); + callback(); } };