weird key behavior

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

Niels Dybdahl

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
 
J

Jan Biel

Niels said:
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
 
J

Jan Biel

Niels said:
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
 

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

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top