• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to map a Keyboard Entry in Java Swing to another Character or String?  RSS feed

 
Sandy Jain
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I want to trap a key or combination of keys (say ‘b’, Shift-b, Alt-b, and Shift-Alt-b) that is input into the keyboard for a Java Swing application. I want to do this for each of the keys on the keyboard. Once the key is trapped, I want to replace it with another character or string of my choice, and display the substitute character or string in a JTextPane object, at the same location in the JTextPane where the original character was typed..

The idea is to map the keys of a normal QWERTY keyboard to the characters of another script and language, like Hindi.

I am able to trap the keys of the QWERTY keyboard just fine, using the following kind of code:



However for the InputMap, I am only able to substitute the KeyStroke for another “Action” like DefaultEditorKit.forwardAction, not another character or String. I don’t necessarily have to use an InputMap - any method that allows me to map a keyboard entry to another character or String would be fine.
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create your own custom Action to do the translation:

 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!