Unclear how to use addKeyDown/Up/PressListener
See original GitHub issueKeyNotifier.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 useesc
,Esc
,escape
orEscape
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:
- Created 5 years ago
- Comments:5 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@knoobie and @Legioth suggestions in #4061, plus improved JavaDocs.
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:addKeyPressListener(Key.ENTER, ...)
addKeyPressListener(Key.of("ö"), ...)
Another alternative would be to have two separate overloads of
addKeyPressListener
(and all other similar methods), one acceptingKey
enum values and one accepting strings. This would again help the IDE guide users to the predefinedKey.XYZ
values.