Skip to content

Commit

Permalink
Merge pull request #32 from samh-nl/fix/issue-23395
Browse files Browse the repository at this point in the history
fix: prevent callback from being called twice
  • Loading branch information
johnmlee101 authored Oct 2, 2023
2 parents 96847bb + 89e5cce commit f54d3c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,14 @@ private boolean matchesInput(ReadableMap command) {
* - {input: '123'}
*/
for (ReadableMap registeredCommand : commandsArray) {
boolean hasModifier = registeredCommand.hasKey("modifierFlags");
int registeredCommandModifierFlags = registeredCommand.hasKey("modifierFlags") ? registeredCommand.getInt("modifierFlags") : 0;
int commandModifierFlags = command.hasKey("modifierFlags") ? command.getInt("modifierFlags") : 0;

boolean matchInput = registeredCommand.getString("input").equals(command.getString("input"));
if (!hasModifier && matchInput) {
return true;
}
boolean matchModifierFlags = registeredCommandModifierFlags == commandModifierFlags;

if (hasModifier) {
boolean matchModifier = registeredCommand.getInt("modifierFlags") == command.getInt("modifierFlags");
if (matchInput && matchModifier) {
return true;
}
if (matchInput && matchModifierFlags) {
return true;
}
}

Expand All @@ -106,29 +103,31 @@ else if (keyEvent.isAltPressed()) {
else if (keyEvent.isShiftPressed()) {
modifierFlags = KeyEvent.META_SHIFT_MASK;
}
else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_ESCAPE) {
modifierFlags = KeyEvent.KEYCODE_ESCAPE;

int inputKeyCode = 0;
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_ESCAPE) {
inputKeyCode = KeyEvent.KEYCODE_ESCAPE;
}
else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
modifierFlags = KeyEvent.KEYCODE_DPAD_UP;
inputKeyCode = KeyEvent.KEYCODE_DPAD_UP;
}
else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
modifierFlags = KeyEvent.KEYCODE_DPAD_DOWN;
inputKeyCode = KeyEvent.KEYCODE_DPAD_DOWN;
}
else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
modifierFlags = KeyEvent.KEYCODE_DPAD_LEFT;
inputKeyCode = KeyEvent.KEYCODE_DPAD_LEFT;
}
else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
modifierFlags = KeyEvent.KEYCODE_DPAD_RIGHT;
inputKeyCode = KeyEvent.KEYCODE_DPAD_RIGHT;
}
else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
modifierFlags = KeyEvent.KEYCODE_ENTER;
inputKeyCode = KeyEvent.KEYCODE_ENTER;
}

/**
* Handle an event where modifier (e.g. ESC) is pressed without input key (e.g. F)
* Handle an event where an input (e.g. ESC) is pressed that has no display label
*/
String input = String.valueOf(modifierFlags);
String input = String.valueOf(inputKeyCode);
String displayLabel = String.valueOf(keyEvent.getDisplayLabel())
.replaceAll("[^A-Za-z0-9]", "")
.toLowerCase();
Expand Down
6 changes: 1 addition & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ function addListener(keyCommand, callback) {
const isInputMatched = (response.input.toLowerCase()) === (validatedKeyCommand.input.toLowerCase());
const isCommandMatched = (response.modifierFlags || 0) === (validatedKeyCommand.modifierFlags || 0);

if (!validatedKeyCommand.modifierFlags && isInputMatched) {
callback(response, event);
}

if (validatedKeyCommand.modifierFlags && isInputMatched && isCommandMatched) {
if (isInputMatched && isCommandMatched) {
callback(response, event);
}
};
Expand Down

0 comments on commit f54d3c9

Please sign in to comment.