weird key behavior

Discussion in 'Java' started by Jan Biel, Jun 29, 2004.

  1. Jan Biel

    Jan Biel Guest

    I have a problem and it's really driving me insane!

    I gave the code below, but first the problem description:

    It's quite simple:
    I have a JFrame with a KeyListener.
    Everytime someone hits the CTRL or ALT key the program is supposed to type
    either "CONTROL" or "ALT".

    But somehow the KeyListener does not always fire and I have no idea why.

    When the program starts I can hit CTRL as much as I want and get the desired
    result
    When I hit ALT once I cannot get any CTRL registered.
    When I hit ALT a second time, nothing happens
    But after that, CTRL hits register every time
    When I hit ALT again, no CTRL hits register
    and so on.

    It seems as if hitting alt would lock up the KeyEventListener until I hit
    ALT again.

    If I keep hitting ALT, the following happens:
    ALT -> returns "ALT"
    ALT -> nothing happens
    ALT -> returns "ALT"
    ALT -> nothing happens

    and so on. Seems to me like some "on-off" switch. I'm really confused :(

    =============================

    Here's the code:

    public class MyFrame extends JFrame implements KeyListener
    {
    public MyFrame()
    {
    this.setVisible(true);
    this.setSize(100, 100);
    this.addKeyListener(this);
    }

    public void keyPressed(KeyEvent arg0)
    {
    if (arg0.isControlDown())
    System.out.println("CONTROL");
    if (arg0.isAltDown())
    System.out.println("ALT");
    }

    public void keyReleased(KeyEvent arg0)
    {}

    public void keyTyped(KeyEvent arg0)
    {}
    Jan Biel, Jun 29, 2004
    #1
    1. Advertising

  2. > When the program starts I can hit CTRL as much as I want and get the
    desired
    > result
    > When I hit ALT once I cannot get any CTRL registered.
    > When I hit ALT a second time, nothing happens
    > But after that, CTRL hits register every time
    > When I hit ALT again, no CTRL hits register
    > and so on.


    Pressing Alt will move the focus to the menu, even if there is no menu.
    Pressing Alt again will return the focus to the application.
    I too would like to know if this behaviour can be turned off.

    Niels Dybdahl
    Niels Dybdahl, Jun 29, 2004
    #2
    1. Advertising

  3. Jan Biel

    Jan Biel Guest

    Niels Dybdahl wrote:
    >> When the program starts I can hit CTRL as much as I want and get the
    >> desired result
    >> When I hit ALT once I cannot get any CTRL registered.
    >> When I hit ALT a second time, nothing happens
    >> But after that, CTRL hits register every time
    >> When I hit ALT again, no CTRL hits register
    >> and so on.

    >
    > Pressing Alt will move the focus to the menu, even if there is no
    > menu. Pressing Alt again will return the focus to the application.
    > I too would like to know if this behaviour can be turned off.


    Yes this makes sense. I should have thought of that myself. Of course Swing
    uses some own KeyEvents automatically. :/

    Thanks for your input.
    I could use CTRL and SHIFT instead.

    Although it would be nice to learn how to turn off the focus change.

    Thanks!
    Janbiel
    Jan Biel, Jun 29, 2004
    #3
  4. Jan Biel

    Jan Biel Guest

    Niels Dybdahl wrote:

    > Pressing Alt will move the focus to the menu, even if there is no
    > menu. Pressing Alt again will return the focus to the application.
    > I too would like to know if this behaviour can be turned off.


    I found it on Google Groups. :)

    Just consume the KeyEvent before doing anything:

    public void keyPressed (KeyEvent e)
    {
    e.consume
    //now do what you want with the ALT key
    }

    HTH
    Janbiel
    Jan Biel, Jun 29, 2004
    #4
    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. dorayme
    Replies:
    1
    Views:
    618
    richard
    Jan 21, 2011
  2. richard
    Replies:
    0
    Views:
    579
    richard
    Jan 21, 2011
  3. richard
    Replies:
    0
    Views:
    614
    richard
    Jan 21, 2011
  4. Beauregard T. Shagnasty

    Re: A Weird Appearance for a Weird Site

    Beauregard T. Shagnasty, Jan 21, 2011, in forum: HTML
    Replies:
    1
    Views:
    437
    Captain Paralytic
    Jan 21, 2011
  5. M P
    Replies:
    1
    Views:
    466
Loading...

Share This Page