aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Key binding problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Key binding problem" Watch "Key binding problem" New topic
Author

Key binding problem

Alexander Walker
Ranch Hand

Joined: Jun 04, 2010
Posts: 34
I have a couple of problems I have encountered while making Key bindings.

1. When I make a key binding to for example Ctrl+Alt+E and have an action performed to write a character to the JTextField I was typing in, the character I programatically added will be added as it should, but if there was originally a character associated to that key combination, it will also be entered, meaning two characters will be entered while only one should be entered. Is there anyway to get rid of that character that was originally associated to that key combination?

2. When I type on my countries keyboard locale for example "Alt Graph+E", it seems that it is the same as typing "Ctrl+Alt+E", however when I type "Alt Graph+E" with the US English locale, it seems that it is the same as typing "Alt+E". Why is that? is there anyway for it to always be "Ctrl+Alt" when typing "Alt Graph" regardless of the keyboard locale? Well, for it to always actually fire "Alt Graph" when typing "Alt Graph" would also work.

Thanks in advance for any help.


Heres example code.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
but if there was originally a character associated to that key combination, it will also be entered


I don't understand the scenario. When would a character be associated with Ctrl+Alt+E? When I use that combination I only see an "a" added to the text field.

Also, it probably doesn't make a difference in your demo, but you would normally extend TextAction. That way the Action can also be added to a menu item.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3438
    
  47

I don't understand the scenario. When would a character be associated with Ctrl+Alt+E? When I use that combination I only see an "a" added to the text field.

The "euro currency" character is generated by some localized keyboard drivers for Ctrl-Alt-E or AltGr-E.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3438
    
  47

Alexander,

the question is - what are you trying to achieve?

The "AltGr" key combinations is a standardized way to enter non-english characters on localized keyboards and is hard-wired into the keyboard driver. The safest approach to it is not to differentiate left Alt and right Alt keys, as on localized keyboards key combinations with right Alt may be completely unavailable to the user (cannot generate that key combination at all). For example in our keyboard layout some action in Microsoft Visio cannot be triggered by keyboard and we need to switch keyboard layout because of it, which is really, really annoying (don't remember which one, it was a colleague of mine who bumped into this situation).

However, inserting individual characters always goes around the input map - try it by adding the following line:Now pressing just the E key results into "ae" being entered to the field. The "euro currency" character is not special in this sense.

(By the way, by modifying the input map and action map received from the UI manager, you've probably modified the behaviour of all text fields in your application. It would probably have been better to modify maps obtained from the JTextField instance you're creating.)

If you really want to intercept key events on such a low level, you'll probably have to study JTextComponent (parent of JTextField) and especially its processInputMethodEvent method. You may have to subclass the JTextField and override some methods, or register some InputMethodListeners.

This is just what I've learned from a quick peek into the javadoc and JTextComponent source code, I haven't done something like this myself. You'd be digging pretty deep into Swing so expect it will take some time to get familiar with these mechanisms. I've comparable experience with JTable, which I was extending to handle merged cells.
Alexander Walker
Ranch Hand

Joined: Jun 04, 2010
Posts: 34
Martin Vajsar wrote:
the question is - what are you trying to achieve?

Basically I want to override some third-level characters (by third level characters I mean like you wrote, non-english characters, also explained in the AltGr wiki article). If you have a program where you have to enter a special character a lot of times, it will very fast become annoying if you on most of the keyboard localizations can't enter that special character, but instead have to copy-paste all of the time, hence why I wan't to overrirde a key combination. But because some keyboard localizations alreaddy have a special character associated to that key combination, I would need to cunsume it some way or else it will write two characters to the JTextField.

As for the key combinaitons I choose. On my keyboard localization when I press AltGr, then an action event with Ctrl-Alt seems to fired, however when I changed to US english keyboard localization an action event with Alt seems to be fired instead when I press AltGr. Hence I am confused as to what key combination I should use, that will feel natural for all keyboard localizations third-level characthers key combination.
If you have a recommendation as to what key combination I should use, please tell me.

Martin Vajsar wrote:
If you really want to intercept key events on such a low level, you'll probably have to study JTextComponent (parent of JTextField) and especially its processInputMethodEvent method. You may have to subclass the JTextField and override some methods, or register some InputMethodListeners.

This is just what I've learned from a quick peek into the javadoc and JTextComponent source code, I haven't done something like this myself. You'd be digging pretty deep into Swing so expect it will take some time to get familiar with these mechanisms. I've comparable experience with JTable, which I was extending to handle merged cells.

I will try looking into that, but it indeed sounds like it will be complicated and take time.

Thanks for the help.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3438
    
  47

In this situation I'd probably try to use a key combination that would not collide with keyboard drivers, eg. Alt-E instead of AltGr-E. People that have the special character on their keyboard layout and are therefore used to it will be able to continue to use their natural key combination, and people that do not have it probably aren't used to a specific combination anyway and will have to learn your combination anew. Although entering characters with Alt-key is somewhat unusual, it is still much better than Copy-Paste.

Even if you'd succeed in overriding AltGr, it might present problems for someone with locale you didn't consider, who may have a different character on key combination you chose and who will not be able to enter that character because of your customization. Alt-key combinations are generally reserved for application use, so you should not encounter any collision with keyboard drivers. I'd expect Alt-key and Ctrl-key to be generally safe to use.

If your target platform is not solely Windows, you'll have to consult specific guidelines of other platforms. What I'm saying here is generally valid for Windows, but it might be different eg. on Unix or Mac - I don't know them. I have also no idea whatsoever how japanese or chinese haracters are typed on the keyboard, not even on Windows.

After all, it might be good idea to allow the users to enter the combination they want to use, especially if you target more platforms or platforms you cannot directly test - might need some work to implement the GUI for this, but will certainly satisfy everyone.
Alexander Walker
Ranch Hand

Joined: Jun 04, 2010
Posts: 34
Martin Vajsar wrote:In this situation I'd probably try to use a key combination that would not collide with keyboard drivers, eg. Alt-E instead of AltGr-E. People that have the special character on their keyboard layout and are therefore used to it will be able to continue to use their natural key combination, and people that do not have it probably aren't used to a specific combination anyway and will have to learn your combination anew. Although entering characters with Alt-key is somewhat unusual, it is still much better than Copy-Paste.

Even if you'd succeed in overriding AltGr, it might present problems for someone with locale you didn't consider, who may have a different character on key combination you chose and who will not be able to enter that character because of your customization. Alt-key combinations are generally reserved for application use, so you should not encounter any collision with keyboard drivers. I'd expect Alt-key and Ctrl-key to be generally safe to use.

If your target platform is not solely Windows, you'll have to consult specific guidelines of other platforms. What I'm saying here is generally valid for Windows, but it might be different eg. on Unix or Mac - I don't know them. I have also no idea whatsoever how japanese or chinese haracters are typed on the keyboard, not even on Windows.

Thanks for the sugestion. This sounds like the best way to me, that way I want have to go through the same amount of trouble, and they will probably be more satisfied that way too.

Martin Vajsar wrote:
After all, it might be good idea to allow the users to enter the combination they want to use, especially if you target more platforms or platforms you cannot directly test - might need some work to implement the GUI for this, but will certainly satisfy everyone.

Did you mean that the users should be able to use what combinations they are used to, so it is better to use Alt-key or Crl+key? Or did you mean that the users should in the program be able to choose what combinations they want to have for the programs special keys?


Thanks for the help.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3438
    
  47

I didn't formulate my last paragraph well. I meant it as an enhancement to allow the users to change the key combination to enter the special character, ideally by pressing a key combination in a textbox or something similar. However you'd have to figure out how to record and later recognize a key event and bind the action to it. Probably even more work than the problem you've described first.
Alexander Walker
Ranch Hand

Joined: Jun 04, 2010
Posts: 34
Martin Vajsar wrote:I didn't formulate my last paragraph well. I meant it as an enhancement to allow the users to change the key combination to enter the special character, ideally by pressing a key combination in a textbox or something similar. However you'd have to figure out how to record and later recognize a key event and bind the action to it. Probably even more work than the problem you've described first.

Okey, now I understand. I too think it will be more work than my original problem, but I still might do it, since it is good to have it.

And again thanks for all the help and suggestions I have recieved.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Key binding problem
 
Similar Threads
Bind jbutton event to a keystroke
Two JPanels in a JFrame listening to keyboard events
Detecting Row Changes on a JTable
How to make a jButton respond to Enter key
JSpinner and KeyListener: not working