J
Jan Biel
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)
{}
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)
{}