aspose file tools*
The moose likes Beginning Java and the fly likes keyevent not working in applet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "keyevent not working in applet?" Watch "keyevent not working in applet?" New topic
Author

keyevent not working in applet?

ajay smith
Greenhorn

Joined: Feb 02, 2013
Posts: 11
I've just started creating this portal game and I've encountered no problems except that KeyEvent isn't doing anything. Here's my code so far (in two classes):



and the second class:



Help please?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41574
    
  54
You need to add "keyaction" as KeyListener to the component that should listen to key events.


Ping & DNS - my free Android networking tools app
ajay smith
Greenhorn

Joined: Feb 02, 2013
Posts: 11
Where do I put that in/how do I do that? Kinda confused
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41574
    
  54
Something like "addKeyListener(new keyaction())". You would do that in the init method. The thinkerbox method you should delete entirely. And the keyaction class needs to override one (or more) of the 3 methods it defines, not create a new method. And it must not create a new instance of the portal class.

Start by reading http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html to get some understanding how key listeners work.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

AWT was superseded by Swing more than 10 years ago. I recommend you change to Swing, which means extending JApplet instead of Applet and gaining a basic understanding of the component hierarchy imposed by the RootPaneContainer interface. Then you can leverage Swing's Key Bindings which afford much more flexibility than a simple KeyListener.

db


luck, db
There are no new questions, but there may be new answers.
 
wood burning stoves
 
subject: keyevent not working in applet?