KeyListener and Alt Key - weird behaviour

Discussion in 'Java' started by lm, Jun 2, 2006.

  1. lm

    lm Guest

    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!
     
    lm, Jun 2, 2006
    #1
    1. Advertising

  2. lm wrote:
    > 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);
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Jun 2, 2006
    #2
    1. Advertising

  3. It works correctly on FC5 with Sun jdk1.5.0_06.

    --

    knute...
     
    Knute Johnson, Jun 2, 2006
    #3
  4. "lm" <> wrote in message
    news:447faea7$0$22164$...
    > 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.

    --
    LTP

    :)
     
    Luc The Perverse, Jun 2, 2006
    #4
  5. Luc The Perverse wrote:
    > "lm" <> wrote in message
    > news:447faea7$0$22164$...
    >> 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.
    >
    > --
    > LTP
    >
    > :)
    >
    >


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

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Jun 2, 2006
    #5
  6. "Knute Johnson" <> wrote in message
    news:XAZfg.15740$...
    > Luc The Perverse wrote:
    >> "lm" <> wrote in message
    >> news:447faea7$0$22164$...
    >>> 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.
    >>
    >> --
    >> LTP
    >>
    >> :)

    >
    > 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.
    --
    LTP

    :)
     
    Luc The Perverse, Jun 2, 2006
    #6
  7. Luc The Perverse wrote:
    > "Knute Johnson" <> wrote in message
    > news:XAZfg.15740$...
    >> Luc The Perverse wrote:
    >>> "lm" <> wrote in message
    >>> news:447faea7$0$22164$...
    >>>> 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.
    >>>
    >>> --
    >>> LTP
    >>>
    >>> :)

    >> 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.
    > --
    > LTP
    >
    > :)
    >
    >


    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.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Jun 3, 2006
    #7
  8. "Knute Johnson" <> wrote in message
    news:pE3gg.15741$...
    >>>> --
    >>>> LTP
    >>>>
    >>>> :)
    >>> 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.
    >> --
    >> LTP
    >>
    >> :)

    >
    > 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.

    --
    LTP

    :)
     
    Luc The Perverse, Jun 3, 2006
    #8
  9. Luc The Perverse wrote:
    >
    > 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?

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Jun 3, 2006
    #9
  10. lm

    Chris Uppal Guest

    Knute Johnson wrote:

    > what component or window is getting the focus if there
    > isn't a window or other component?


    Very Zen...

    ;-)

    -- chris
     
    Chris Uppal, Jun 3, 2006
    #10
  11. Chris Uppal wrote:
    > Knute Johnson wrote:
    >
    >> what component or window is getting the focus if there
    >> isn't a window or other component?

    >
    > Very Zen...
    >
    > ;-)
    >
    > -- chris
    >
    >
    >


    That's just too funny Chris!

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Jun 3, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Nicholson
    Replies:
    10
    Views:
    757
    Steven Cheng[MSFT]
    Aug 23, 2005
  2. apchar
    Replies:
    3
    Views:
    1,851
    Andrew Hobbs
    Feb 3, 2004
  3. Ike
    Replies:
    3
    Views:
    509
    Chris Smith
    Apr 2, 2006
  4. Kreedz
    Replies:
    8
    Views:
    414
    Graham Fawcett
    Sep 22, 2005
  5. phil89
    Replies:
    25
    Views:
    1,128
    John B. Matthews
    Jul 6, 2008
Loading...

Share This Page