• Post Reply Bookmark Topic Watch Topic
  • New Topic

KeyBindings using KeyStroke

 
Aahan Agrawal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


 
Darryl Burke
Bartender
Posts: 5152
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this is related to Swing, I've added it to the GUIs section.

Have you tried ENTER instead of Enter?
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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”.
 
Aahan Agrawal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"ENTER" as opposed to "Enter" resolved the issue.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!