Closed AObuchow closed 4 years ago
The KeyType for the spacebar is Character, and the character is ' '.
Your check for keyStroke.getKeyType().ordinal() == 0
is just equivalent to keyStroke.getKeyType() == KeyType.Character
(though the latter is the preferred style to use for Java enums) and it will be true not only for Spacebar, but for most characters(letter, digit, ...) you may type, unless you also add && keyStroke.character() == ' '
.
Okay awesome, thank you @avl42! :) You just prevented future bugs from creeping into my application :)
See, I'm not consistent here. Because I added a Tab value, but not SpaceBar. Probably because ReverseTab doesn't have a good character to it and so better to match them. Enter is also a value, since line endings are different on different platforms (although even on Windows I'm pretty sure the Enter key only generates \n). So, maybe it would have been easier usability-wise to add a SpaceBar value too, but I feel it's too late now.
I wanted to detect spacebar input in my component, but couldn't find
KeyType.SpaceBar
. For the moment, I've resorted to the following:Is there a better way to detect spacebar input? Could a
KeyType.SpaceBar
be added? I could try submitting a PR for this when I have a moment.