Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #166: Fix over-zealous modifier flag discrepancy correction #185

Merged
merged 4 commits into from
May 30, 2019

Conversation

greena13
Copy link
Owner

@greena13 greena13 commented May 30, 2019

Context

There is a subroutine that is run on the keyup event that checks the current KeyBoardEvent's modifier key flags (e.g. metaKey) and compares it against the key state that react-hotkeys is maintaining. It then corrects for any discrepancies (missed key events, most likely due to a focus loss) in the react-hotkeys record of key state.

On the keyup event for a modifier key (e.g. Command/Meta), its own corresponding modifier key flag (e.g. metaKey) is false. This was causing registering the key as being in the keyup state twice, resulting in #166 whereby handlers bound to the keyup event of the Meta key were not being called.

This pull request

  • Adds additional logic to detect the exceptional case when it's a modifier key that is in the keyup state, and skips running the subroutine
  • Adds a test that records this behaviour and should prevent regressions in the future.

@@ -1111,17 +1111,28 @@ class AbstractKeyEventStrategy {
* Synchronises the key combination history to match the modifier key flag attributes
* on new key events
* @param {KeyboardEvent} event - Event to check the modifier flags for
* @param {String} key - Name of key that events relates to
* @param {KeyEventBitmapIndex} keyEventBitmapIndex - Index of event type
Copy link
Owner Author

@greena13 greena13 May 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Improve this parameter description

/**
* When a modifier key is being released (keyup), it sets its own modifier flag
* to false. (e.g. On the keyup event for Command, the metaKey attribute is false).
* If this the case, we want to handle it using the main algorithm.
Copy link
Owner Author

@greena13 greena13 May 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Expand this comment:

... and skip the reconciliation algorithm

@@ -301,7 +301,7 @@ class FocusOnlyKeyEventStrategy extends AbstractKeyEventStrategy {
return true;
}

const responseAction = this._howToHandleKeyDownEvent(event,
const responseAction = this._howToHandleKeyEvent(event,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐥Renamed this function to more accurately reflect its conceptual level of abstraction (it doesn't just deal with keydown events)

this.targetElement.focus();
});

it('then simulates the cmd keypress event and the non-modifier key\'s keypress event (https://github.com/greena13/react-hotkeys/issues/166)', function () {
Copy link
Owner Author

@greena13 greena13 May 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Fix this test description. (Copy and paste error.)

import FocusableElement from '../support/FocusableElement';

describe('Binding to keys with simulated keypress events:', function () {
context('when HotKeys has actions for a the keydown and keyup event', () => {
Copy link
Owner Author

@greena13 greena13 May 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Improve this context description - could be more specific.

@@ -28,26 +28,26 @@ export default class FocusableElement {
}

keyDown(key, options = {}) {
this.element.simulate('keyDown', {key});
this.element.simulate('keyDown', {key, ...options});
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐥 Allows passing additional KeyboardEvent attributes when writing tests.

@greena13 greena13 merged commit 1faba1f into master May 30, 2019
@greena13 greena13 deleted the bugfix_166 branch May 30, 2019 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant