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.

Unclear how to use addKeyDown/Up/PressListener

See original GitHub issue

KeyNotifier.addKeyDownListener(String key, ComponentEventListener<KeyDownEvent> listener, KeyModifier... modifiers) javadoc says

Adds a {@code keydown} listener to this component, conditional on key and modifiers.
key: the key to match

What remains unclear is:

  • Why is the key a String? What happens with addKeyDownListener("abc",...)?
  • How do I listen to enter? Debugging shows that I should use “Enter” as they key to match, which was quite unexpected. Especially as “enter” does not work. There is seemingly no way to know upfront if I should use esc, Esc, escape or Escape to match the escape key

I would have expected the key parameter to be a a character and 13 would listen to enter

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
heruancommented, May 3, 2018

@knoobie and @Legioth suggestions in #4061, plus improved JavaDocs.

2reactions
Legiothcommented, May 3, 2018

The benefit would be discoverability, i.e. that your IDE will offer better auto complete suggestions.

As an alternative to an enum, I would suggest an interface with constants and a static of(String) factory method for other cases. The code would thus look like this instead:

  1. addKeyPressListener(Key.ENTER, ...)
  2. addKeyPressListener(Key.of("ö"), ...)

Another alternative would be to have two separate overloads of addKeyPressListener (and all other similar methods), one accepting Key enum values and one accepting strings. This would again help the IDE guide users to the predefined Key.XYZ values.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to prevent keydown event listener from listening while ...
addEventListener("keydown", checkKeyPressed, false); function checkKeyPressed(e){ var elem = e.target; var type = elem.getAttribute("type"); if(type!=
Read more >
How to disable a keypress event? - Qualtrics Community
I am programming a survey which relies on a question which uses a keypress event, and have encountered a problem where custom code...
Read more >
Event Listeners in React Components - Pluralsight
You can create an event listener in a React app by using the window.addEventListener ; The code snippet above shows you how to...
Read more >
Keydown is the only keyboard event we need - Mutually Human
In my experience, keydown is the only keyboard event worth using, keypress can be ignored entirely, and keyup is well suited for getting...
Read more >
Element: keydown event - Web APIs | MDN
The keydown event is fired when a key is pressed. ... Use the event name in methods like addEventListener() , or set an...
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