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:
addKeyDownListener("abc",...)?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 keyI would have expected the key parameter to be a a character and 13 would listen to enter
As the author of the PR which introduced these notifiers, I can say that: key is a string since it reflects the event.key property.
keyCode is deprecated since e.g. 13 is not ENTER is all keyboard layouts; neither code suits well, since it is tied to the hardware keyboard, e.g. with a Russian layout 协 will trigger event.code = 'Quote' but event.key = '协'.
Thus the choice to use key which represent the exact key the user intended; common constants to avoid typing keys manually are defined in Key.
I totally agree the JavaDocs should be more clear about this.
@heruan - what do you think about the following to allow easier discovery of "Key".
Dunno if this could be a way the Flow Team likes to handle stuff.
Wouldn鈥檛 this be just overhead? Instead of this:
addKeyPressListener(Key.ENTER, ...)addKeyPressListener("枚", ...)You鈥檇 have this:
addKeyPressListener(Key.ENTER, ...)addKeyPressListener(() -> "枚", ...)I don鈥檛 see any added value, am I missing something?
I really think it鈥檚 just a matter of JavaDocs here.
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 accepting Key enum values and one accepting strings. This would again help the IDE guide users to the predefined Key.XYZ values.
@knoobie and @Legioth suggestions in #4061, plus improved JavaDocs.
Most helpful comment
@knoobie and @Legioth suggestions in #4061, plus improved JavaDocs.