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

feat(keyboard): keep key pressed for multiple keydown events #728

Merged
merged 13 commits into from
Oct 18, 2021

Conversation

patricklizon
Copy link
Contributor

@patricklizon patricklizon commented Sep 22, 2021

What:

Trigger multiple keydown events in a single key press

Why:

As requested in #618

How:

I followed @ph-fritsche suggestions #618 (comment)

  • added repeatModifier detection at getNextKeyDef
  • added repeatKey to keyboardState

Checklist:

  • Documentation
  • Tests
  • Ready to be merged

@codesandbox-ci
Copy link

codesandbox-ci bot commented Sep 22, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 2a46dc2:

Sandbox Source
userEvent-PR-template Configuration

Copy link
Member

@ph-fritsche ph-fritsche left a comment

Choose a reason for hiding this comment

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

First of all thanks a lot for taking the time to contribute. ❤️

The implementation does not properly solve the issue yet.

When a key is pressed in the browser, the browser dispatches a keydown event every n ms.
(Usually there is a primary delay before the key is considered "pressed" and then there is a secondary delay between each of the keydown events until the key is released or another one is either pressed or released - both usually according to some system setting.)

For each of the keydown events there might be the default behavior of a keypress and depending on the active element an input event.

Instead of dispatching each event multiple times the whole implementation needs to loop for fireKeyDownTimes.

src/keyboard/keyboardImplementation.ts Show resolved Hide resolved
src/keyboard/keyboardImplementation.ts Outdated Show resolved Hide resolved
@ph-fritsche ph-fritsche linked an issue Sep 27, 2021 that may be closed by this pull request
@patricklizon
Copy link
Contributor Author

@ph-fritsche Thank you for explaining how triggering keyboard events work in browser. That's great something I was not aware of. It's great learning opportunity for me, I'll definitely dig deeper into the topic and the source code of library 🙂

Copy link
Member

@ph-fritsche ph-fritsche left a comment

Choose a reason for hiding this comment

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

I started with a review but achieving this while keeping the code readable required quite some refactoring so I opened a PR.

src/__tests__/keyboard/keyboardImplementation.ts Outdated Show resolved Hide resolved
fix: only trigger one `keyup` for repeated `keydown`
Copy link
Member

@ph-fritsche ph-fritsche left a comment

Choose a reason for hiding this comment

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

I just noticed that we might leak state into another call if the last key def has a repeat modifier and keyboardState is reused on another call.

src/keyboard/keyboardImplementation.ts Outdated Show resolved Hide resolved
src/keyboard/keyboardImplementation.ts Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Oct 1, 2021

Codecov Report

Merging #728 (2a46dc2) into main (fcbdf21) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #728   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           51        51           
  Lines          994      1005   +11     
  Branches       413       408    -5     
=========================================
+ Hits           994      1005   +11     
Impacted Files Coverage Δ
src/keyboard/getNextKeyDef.ts 100.00% <100.00%> (ø)
src/keyboard/keyboardImplementation.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fcbdf21...2a46dc2. Read the comment docs.

Copy link
Member

@ph-fritsche ph-fritsche left a comment

Choose a reason for hiding this comment

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

Looks good to me now.

@ph-fritsche ph-fritsche changed the title feat: #618 Trigger multiple keydown events in a single key press feat(keyboard): keep key pressed for multiple keydown events Oct 1, 2021
@ph-fritsche
Copy link
Member

@ph-fritsche ph-fritsche merged commit 5d946d5 into testing-library:main Oct 18, 2021
@ph-fritsche
Copy link
Member

@all-contributors add @patricklizon code

@allcontributors
Copy link
Contributor

@ph-fritsche

I've put up a pull request to add @patricklizon! 🎉

@github-actions
Copy link

🎉 This PR is included in version 13.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Multiple keydown events in a single key press
2 participants