Forums Register Login
KeyBindings using KeyStroke
I wrote a keybinding into a program. The keybinding implementation that allowed me to use the ENTER key required me to use KeyStroke.getKeyStroke(int key, int modifier) as opposed to KeyStroke.getKeyStroke(String s). In both method calls, I used the appropriate key or modifier for ENTER, either KeyStroke.VK_ENTER or "Enter." Why does KeyStroke.getKeyStroke("Enter") not work?

(1 like)
Since this is related to Swing, I've added it to the GUIs section.

Have you tried ENTER instead of Enter?
(1 like)
Don't know. Have a look at the Java™ Tutorials and the documentation for KeyStroke#getKeyStroke(). It says it takes an int and I think you will find those ints are stored as constants in the KeyEvent class. It is usual for constants to be written in CAPITAL_LETTERS, so that may be why you can't get "Enter" to work.

I see there is an overloaded version of KeyStroke#getKeyStroke() which takes a String parameter. It says there is a particular syntax you have to follow, so you may need to alter the String you were passing in the first version of the method to match that syntax. I think that "ENTER" probably would match the required syntax if you will accept “pressed”.
"ENTER" as opposed to "Enter" resolved the issue.
Evacuate the building! Here, take this tiny ad with you:
Why should you try IntelliJ IDEA ?

This thread has been viewed 346 times.

All times above are in ranch (not your local) time.
The current ranch time is
Aug 16, 2018 14:33:13.