KeyListener and Alt Key - weird behaviour

L

lm

Hi,

Using Java 1.5.0_06-b05.
XP SP2

I attached a KeyListerner to a component to specifically monitor modifer key
events (Shift/Alt/Ctrl keys).

Using simple code I can pick up the keyPressed() and keyReleased() events
fine, but I noticed some weird behaviour with the Alt key.

When pressing the Alt key, it triggers a keyPressed event and then a
subsequent keyReleased event when I release the key (as expected). But when
I hit Alt a second consecutive time, it does not trigger a keyPressed event,
but it does trigger the subsequent keyReleased event when released. When I
press a third consecutiv time, it behaves correctly (triggers both pressed
and released events). In summary, every second consecutive pressing of Alt
fails to trigger a keyPressed event, but all keyReleased events are
triggered.

All other keys (Shift and Ctrl) seem to behave normally.

Is this normal behaviour for the Alt key or is it a potential bug of the JVM
or OS???

Thanks!
 
K

Knute Johnson

lm said:
Hi,

Using Java 1.5.0_06-b05.
XP SP2

I attached a KeyListerner to a component to specifically monitor modifer key
events (Shift/Alt/Ctrl keys).

Using simple code I can pick up the keyPressed() and keyReleased() events
fine, but I noticed some weird behaviour with the Alt key.

When pressing the Alt key, it triggers a keyPressed event and then a
subsequent keyReleased event when I release the key (as expected). But when
I hit Alt a second consecutive time, it does not trigger a keyPressed event,
but it does trigger the subsequent keyReleased event when released. When I
press a third consecutiv time, it behaves correctly (triggers both pressed
and released events). In summary, every second consecutive pressing of Alt
fails to trigger a keyPressed event, but all keyReleased events are
triggered.

All other keys (Shift and Ctrl) seem to behave normally.

Is this normal behaviour for the Alt key or is it a potential bug of the JVM
or OS???

Thanks!

Sure looks like a bug to me! I get exactly the same behavior. What OS
are you using? I tested it on XP SP2.

import java.awt.*;
import java.awt.event.*;

public class test2 {
public static void main(String[] args) {
Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
TextField tf = new TextField("text field");
tf.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
System.out.println("keypressed");
}
public void keyReleased(KeyEvent ke) {
System.out.println("keyreleased");
}
});
f.add(tf);
f.pack();
f.setVisible(true);
}
}
 
L

Luc The Perverse

lm said:
Is this normal behaviour for the Alt key or is it a potential bug of the
JVM or OS???

Yes this is normal and correct use of the alt key.

When you push alt by itself it takes control to the menu.

Try pushing alt, releasing it, and then pushing the space bar (in just about
any program).

I haven't tried but I imagine you can probably stop the alt key from doing
this if you need to consecutively catch it - but if you are using menus in
your program it is likely that this will annoy a user who wishes to use the
menu in this fashion.
 
K

Knute Johnson

Luc said:
Yes this is normal and correct use of the alt key.

When you push alt by itself it takes control to the menu.

Try pushing alt, releasing it, and then pushing the space bar (in just about
any program).

I haven't tried but I imagine you can probably stop the alt key from doing
this if you need to consecutively catch it - but if you are using menus in
your program it is likely that this will annoy a user who wishes to use the
menu in this fashion.

What does Alt then Spacebar do on your computer? On mine it just dings.
 
L

Luc The Perverse

Knute Johnson said:
What does Alt then Spacebar do on your computer? On mine it just dings.

On a windows machine it should bring up the window control menu. It should
do this unless your foreground window doesn't have a title bar, or the menu
has been disabled.
 
K

Knute Johnson

Luc said:
On a windows machine it should bring up the window control menu. It should
do this unless your foreground window doesn't have a title bar, or the menu
has been disabled.

On Thunderbird, the newsreader that I use, ALT then Spacebar causes a
ding. On Pegasus, my email client it brings up the window menu with
Restore, Minimize, and Close.

In either case I'm not sure why when no other window or component takes
the focus that it should consume the keypressed event. Where are they
going if not to some menu? If you press the Windows menu key it
consumes both keypressed and keyreleased until it is gone.

Interestingly enough F10 operates similarly to ALT.
 
L

Luc The Perverse

Knute Johnson said:
On Thunderbird, the newsreader that I use, ALT then Spacebar causes a
ding. On Pegasus, my email client it brings up the window menu with
Restore, Minimize, and Close.

In either case I'm not sure why when no other window or component takes
the focus that it should consume the keypressed event. Where are they
going if not to some menu? If you press the Windows menu key it consumes
both keypressed and keyreleased until it is gone.

Interestingly enough F10 operates similarly to ALT.

That is because F10 also brings up the menu ;)

It is not that Alt is not being pressed and released again, just that the
component that you are listening from is not getting the message because the
Alt key has given focus to the menu.
 
K

Knute Johnson

Luc said:
It is not that Alt is not being pressed and released again, just that the
component that you are listening from is not getting the message because the
Alt key has given focus to the menu.

I got that but what component or window is getting the focus if there
isn't a window or other component?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top