Archived

This topic is now archived and is closed to further replies.

Knudde

[java] KeyListener in JWindow under 1.4 doesn't work

Recommended Posts

Hi, the following simple program, displays output via the standard output when the mouse is moved or a key is pressed. Under 1.4 only output is display while moving the mouse, but for pressing the key it is missing. I already have wounded eyes by looking at the code, without detecting an error, maybe an bug in the jdk. What dou you think? ciao knudde import java.awt.event.*; import javax.swing.*; public class TestWindow extends JWindow implements KeyListener, MouseMotionListener { public TestWindow() { super(); setLocation(300, 300); setSize(400, 400); addKeyListener(this); addMouseMotionListener(this); setVisible(true); } // END constructor TestWindow public void keyPressed(KeyEvent keyEvent) { System.out.println("TestEngine keyPressed"); if (keyEvent.getKeyCode() == KeyEvent.VK_ESCAPE) System.exit(0); } // END keyPressed public void keyReleased(KeyEvent keyEvent) { } // END keyReleased public void keyTyped(KeyEvent keyEvent) { } // END keyTyped public void mouseDragged(MouseEvent mouseEvent) { } // END mouseDragged public void mouseMoved(MouseEvent mouseEvent) { System.out.println("TestEngine mouseMoved"); } // END mouseMoved public static void main(String args[]) { TestWindow testWindow = new TestWindow(); } // END main } // END class TestWindow

Share this post


Link to post
Share on other sites
try this:

getContentPane().addKeyListener(this);
//instead of addKeyListener(this);

setVisible(true);
requestFocus();//so that the JWindow can detect key events.

Share this post


Link to post
Share on other sites
There is an open bug related to KeyListener and the fix won''t be around until jre 1.5. Check out the bug parade.

The fanatic is incorruptible: if he kills for an idea, he can just as well get himself killed for one; in either case, tyrant or martyr, he is a monster.
--EM Cioran

Opere Citato

Share this post


Link to post
Share on other sites
Actually I just found a bug report that is almost identical to your bug. It isn''t a bug. JWindows can only get focus if they have a parent frame or dialog visible.

Link

The fanatic is incorruptible: if he kills for an idea, he can just as well get himself killed for one; in either case, tyrant or martyr, he is a monster.
--EM Cioran

Opere Citato

Share this post


Link to post
Share on other sites