From 7f065f2aa3acced55dde49602a517177b5626fbe Mon Sep 17 00:00:00 2001 From: Olivier Tassinari Date: Wed, 9 Oct 2024 01:36:04 +0200 Subject: [PATCH] polish --- .../x-data-grid/src/utils/keyboardUtils.ts | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/packages/x-data-grid/src/utils/keyboardUtils.ts b/packages/x-data-grid/src/utils/keyboardUtils.ts index e428186e387fe..c8036facbe4f8 100644 --- a/packages/x-data-grid/src/utils/keyboardUtils.ts +++ b/packages/x-data-grid/src/utils/keyboardUtils.ts @@ -45,25 +45,24 @@ export const isHideMenuKey = (key: React.KeyboardEvent['key']) => key === 'Tab' // In theory, on macOS, ctrl + v doesn't trigger a paste, so the function should return false. // However, maybe it's overkill to fix, so let's be lazy. export function isPasteShortcut(event: React.KeyboardEvent) { - if ( + return ( (event.ctrlKey || event.metaKey) && - event.code === 'KeyV' && + // We can't use event.code === 'KeyV' as event.code assumes a QWERTY keyboard layout, + // for example, it would be another letter on a Dvorak physical keyboard. + // We can't use event.key === 'v' as event.key is not stable with key modifiers and keyboard layouts, + // for example, it would be ה on a Hebrew keyboard layout. + String.fromCharCode(event.keyCode) === 'V' && !event.shiftKey && !event.altKey - ) { - return true; - } - return false; + ); } -/** - * Checks if the keyboard event corresponds to the copy shortcut (CTRL+C or CMD+C) across different localization keyboards. - * - * @param {KeyboardEvent} event - The keyboard event to check. - * @returns {boolean} - Returns true if the event is a copy shortcut, otherwise false. - */ +// Checks if the keyboard event corresponds to the copy shortcut (CTRL+C or CMD+C) across different localization keyboards. export function isCopyShortcut(event: KeyboardEvent): boolean { return ( - (event.ctrlKey || event.metaKey) && event.code === 'KeyC' && !event.shiftKey && !event.altKey + (event.ctrlKey || event.metaKey) && + String.fromCharCode(event.keyCode) === 'C' && + !event.shiftKey && + !event.altKey ); }