weird key behavior

From: Jan Biel (jan.biel_at_tu-clausthal.de)
Date: 06/29/04


Date: Tue, 29 Jun 2004 16:29:21 +0200

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)
{}



Relevant Pages

  • Re: Alt E no longer opens Edit menu
    ... It's probably too late now, but if you had hit the alt-e once more, then enter, ... simonc wrote: ... I reset the standard toolbar and now Alt E works ... Hit alt-e again and edit won't be depressed. ...
    (microsoft.public.excel.misc)
  • Re: G1000 Check out
    ... > The ALT button toggles between alt hold and Vertical speed modes but not ... IIRC, the first time you hit the ALT button, this ... engage the altitude hold. ... Hit the ALT again and it toggles back to VS ...
    (rec.aviation.piloting)
  • Re: G1000 Check out
    ... However altitude navigation is confusing. ... The ALT button toggles between alt hold and Vertical speed modes but not all the time. ... IIRC, the first time you hit the ALT button, this engages the vertical speed hold. ...
    (rec.aviation.piloting)
  • Re: allow design changes
    ... Open a form in design view. ... Hold down Alt, and hit Enter. ... > I am a pretty novice Access user, and have been searching help and the ...
    (microsoft.public.access.forms)
  • Re: Special Paste Dialogue Box
    ... The ALT + F11 VB suggestion fixed the problem. ... perhaps you need to reset that menu. ... Hit ALT + F11 to go to the VBEditor. ...
    (microsoft.public.excel)