wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes KeyListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "KeyListener" Watch "KeyListener" New topic
Author

KeyListener

Alexander Walker
Ranch Hand

Joined: Jun 04, 2010
Posts: 34
I found some things when using a KeyListener that seems strange to me.

1. There is a InputEvent.ALT_GRAPH_DOWN_MASK that can be used to check if the Alt Graph is down, however whenever I press the Alt Graph key, the keyPressed method is called twice, first with Ctrl as the modifier and then with Ctrl+Alt as modifiers. Seems to me like it simulates Ctrl+Alt being pressed. Why even have a InputEvent.ALT_GRAPH_DOWN_MASK if it's never used? Or is it meant to be used with somthing else? Or is there something else that is the reason?

2. When presseing the Alt key twice in a row, the second time the keyPressed event is never fired. On the third time the method will be called again.

3. When pressing the Alt key once and then pressing the Ctrl key, then the keyPressed event is never fired no matter how many times you press it, but if you then press ONCE on the Alt key and then press the Ctrl key it will fire.
Key sequence example: Alt, Ctrl, Ctrl, Ctrl, Alt, Ctrl, Ctrl. The key pressed event for presesing the Ctrl will only be fired the two last times Ctrl is pressed. This probablöy have sometinh to do with 2. Seems to be the same if you do it with Shift or Caps lock too.

4. If I press the Alt key once and then press a letter key, then the keyPressed is not fired, however it will be fired if you press the letter once more.
Key sequence example: Alt, B, B, B. The keyPressed event is only fired the last two times when pressing the letter key B. This probably have someting to do with 2.


That was those things that I found, I hope I made any sense. I'd like to know if other peaple are also having the same problem, or is it just someting I have?


Heres is my code that can be run to check these things.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
The Alt key is used to invoke the Windows System menu. Try "Alt" followed by the "Down arrow" key.

As a general rule you should be using Key Bindings and not listening for KeyEvents.
Alexander Walker
Ranch Hand

Joined: Jun 04, 2010
Posts: 34
Rob Camick wrote:The Alt key is used to invoke the Windows System menu. Try "Alt" followed by the "Down arrow" key.

Thanks for the good answer. That answer question 2-4.

Rob Camick wrote:As a general rule you should be using Key Bindings and not listening for KeyEvents.

What is the reason that one should be using Key Bindings instead of a KeyListener?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: KeyListener