[java] Key listeners in Applets

Started by
1 comment, last by gdalston 23 years, 2 months ago
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
Advertisement
call requestFocus() either in the init method of your applet, or
when a mouse click occurs.
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
<< ...and here is one we developed earlier... >>

This topic is closed to new replies.

Advertisement