From facf6adb302cbb9149178b0702af235a09e1e515 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Tue, 3 Jun 2025 23:06:14 +0800 Subject: [PATCH 1/2] fix: escape shortcut key overriding each other --- example/src/App.js | 12 +++++++++--- src/KeyCommand/index.js | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/example/src/App.js b/example/src/App.js index 9997980..bd46f33 100644 --- a/example/src/App.js +++ b/example/src/App.js @@ -41,6 +41,11 @@ export default function App() { return KeyCommand.addListener(KEY_COMMAND, () => handleSearchCommandPress('[CMD + F] pressed')); }, []); + React.useEffect(() => { + const KEY_COMMAND = {input: KeyCommand.constants.keyInputEscape, modifierFlags: KeyCommand.constants.keyModifierShift}; + return KeyCommand.addListener(KEY_COMMAND, () => handleSearchCommandPress('[Shift + Esc] pressed')); + }, []); + React.useEffect(() => { const KEY_COMMAND = {input: KeyCommand.constants.keyInputEscape}; return KeyCommand.addListener(KEY_COMMAND, () => handleSearchCommandPress('[Esc] pressed')); @@ -78,9 +83,10 @@ export default function App() { 1. [CMD + F] 2. [G] 3. [Esc] - 4. [CMD + SHIFT + K] - 5. [ENTER] - 6. [DOWN / UP ARROW] + 4. [Shift + Esc] + 5. [CMD + SHIFT + K] + 6. [ENTER] + 7. [DOWN / UP ARROW] diff --git a/src/KeyCommand/index.js b/src/KeyCommand/index.js index 6589a57..ad17172 100644 --- a/src/KeyCommand/index.js +++ b/src/KeyCommand/index.js @@ -114,7 +114,7 @@ function getRegisteredCommandIndex(json) { // This does a strict check first to see if there is a match // https://github.com/Expensify/App/issues/18480 const strictIndex = _.findIndex(commands, item => ( - (item.input === json.input && matchesModifierFlags(item)) + ((item.input === json.input || (matchesEscape(item))) && matchesModifierFlags(item)) || (matchesEnter(item) && matchesModifierFlags(item)) )); From fce1ac3507cdc5639f04f5ce0d0d4fba13c27db1 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 4 Jun 2025 00:11:04 +0800 Subject: [PATCH 2/2] chore: rewrite so it's easier to read --- src/KeyCommand/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/KeyCommand/index.js b/src/KeyCommand/index.js index ad17172..41362a8 100644 --- a/src/KeyCommand/index.js +++ b/src/KeyCommand/index.js @@ -114,8 +114,9 @@ function getRegisteredCommandIndex(json) { // This does a strict check first to see if there is a match // https://github.com/Expensify/App/issues/18480 const strictIndex = _.findIndex(commands, item => ( - ((item.input === json.input || (matchesEscape(item))) && matchesModifierFlags(item)) + (item.input === json.input && matchesModifierFlags(item)) || (matchesEnter(item) && matchesModifierFlags(item)) + || (matchesEscape(item) && matchesModifierFlags(item)) )); if (strictIndex < 0) {