mabe02 / lanterna

Java library for creating text-based GUIs
GNU Lesser General Public License v3.0
2.27k stars 242 forks source link

KeyType.SpaceBar missing? #455

Closed AObuchow closed 4 years ago

AObuchow commented 4 years ago

I wanted to detect spacebar input in my component, but couldn't find KeyType.SpaceBar. For the moment, I've resorted to the following:

if (keyStroke.getKeyType().ordinal() == 0) {
            System.out.println("Spacebar pressed");
        }

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.

avl42 commented 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() == ' ' .

AObuchow commented 4 years ago

Okay awesome, thank you @avl42! :) You just prevented future bugs from creeping into my application :)

mabe02 commented 4 years ago

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.