question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update `triggerKeyEvent` to handle `code` and `key`.

See original GitHub issue

From https://github.com/emberjs/ember-test-helpers/pull/258#discussion_r155963086:

When I initially copied keyEvent, I continued to use keyCode as the main way to set the key, but it seems to be deprecated and the new alternatives are KeyboardEvent.code and KeyboardEvent.key, specially the last one.

Perhaps we might want to start using it, or maybe assume that integers mean keyCode and strings mean key.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gossicommented, Jan 17, 2019

Same here, code isn’t set. Here is the workaround:

await triggerKeyEvent('[data-test-code-input-1]', 'keydown', 'Digit4', {code: 'Digit4'});

Also will provide squiggly red lines if you are using TS 😦. May I suggest this API:

triggerKeyEvent(target: Target, eventType: KeyboardEventType, key: number | string, modifiers: KeyModifiers = DEFAULT_MODIFIERS);
triggerKeyEvent(target: Target, eventType: KeyboardEventType, key: number | string | Props);

where Props is the KeyboardEventInit. This way it is BC but will provide the flexibility we have to deal with when handling key events.

0reactions
istatesidecommented, Jan 7, 2019

Sorry to resurrect an old issue, but was the code property intentionally left out of the fix here? I’ve got a failing test because we’re using a keyboard shortcut that relies on the code property. I had skipped the test and added a comment linking to this issue, thinking that it would eventually be fixed. We can refactor to use something else, but is there a reason why code was not implemented?

From my understanding, key and code are both supported for different reasons- key gives you the string ‘value’ of a keypress, whereas code is useful for telling which physical button was pressed, regardless of keyboard layout or language.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to simulate key press events programmatically?
The answer is: There is no way to programmatically trigger input keys in the sandboxed browser environment under normal circumstances. Bottomline: I am...
Read more >
KeyboardEvent.key - Web APIs | MDN
The KeyboardEvent interface's key read-only property returns the value of the key pressed by the user, taking into consideration the state ...
Read more >
KeyEvent - Android Developers
Key code constant: L1 Button key. On a game controller, the L1 button should be either the button labeled L1 (or L) or...
Read more >
.keypress() | jQuery API Documentation
To trigger the event manually, apply .keypress() without an argument: ... Note that keydown and keyup provide a code indicating which key is...
Read more >
Keyboard: keydown and keyup - The Modern JavaScript Tutorial
So, event.code may match a wrong character for unexpected layout. Same letters in different layouts may map to different physical keys, leading ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found