From 34f01e29644cd4edb2771f545633b56e8ceaf68c Mon Sep 17 00:00:00 2001 From: peterkogo Date: Thu, 7 Nov 2024 11:27:50 +0100 Subject: [PATCH] support key combinations which include + --- packages/react/src/hooks/useKeyPress.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/react/src/hooks/useKeyPress.ts b/packages/react/src/hooks/useKeyPress.ts index beca4000..8bce0564 100644 --- a/packages/react/src/hooks/useKeyPress.ts +++ b/packages/react/src/hooks/useKeyPress.ts @@ -45,7 +45,12 @@ export function useKeyPress( const [keyCodes, keysToWatch] = useMemo<[Array, Keys]>(() => { if (keyCode !== null) { const keyCodeArr = Array.isArray(keyCode) ? keyCode : [keyCode]; - const keys = keyCodeArr.filter((kc) => typeof kc === 'string').map((kc) => kc.split('+')); + const keys = keyCodeArr + .filter((kc) => typeof kc === 'string') + // we first replace all '+' with '\n' which we will use to split the keys on + // then we replace '\n\n' with '\n+', this way we can also support the combination 'key++' + // in the end we simply split on '\n' to get the key array + .map((kc) => kc.replace('+', '\n').replace('\n\n', '\n+').split('\n')); const keysFlat = keys.reduce((res: Keys, item) => res.concat(...item), []); return [keys, keysFlat];