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?
Post by:Darryl Burke
Since this is related to Swing, I've added it to the GUIs section.
Have you tried ENTER instead of Enter?
Post by:Campbell Ritchie
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”.