Archived

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

gdalston

[java] Key listeners in Applets

Recommended Posts

gdalston    122
I''m trying to add a key listener to my applet. I have this code inside of init(): addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { System.out.println("Key pressed."); } }); This should work. It works fine in an application. I can get mouse listeners to work using this same technique. I think the problem is that the applet doesn''t have the "focus" of the keyboard (e.g. when I use appletviewer to run it the keyboard controls the buttons at the top of the appletviewer.) Is there anyway to get around this problem? Thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
call requestFocus() either in the init method of your applet, or
when a mouse click occurs.

Share this post


Link to post
Share on other sites
Neurotic    122
This is why often have a "please click on the applet before you play" comment next to most applet games.

The focus is not neccessarily on the applet, so it can''t register input events.

Mark

Share this post


Link to post
Share on other sites