| | |
Problem with KeyListener
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2009
Posts: 3
Reputation:
Solved Threads: 0
Hello all. I have a problem that seems like it should be relatively simple to fix but I have been scouring the internet and can't find an answer =(
I am trying to add a keylistener to this code, but it doesn't recognize the method addKeyListener(this); I am working with kareltherobot so that's where all of the methods like pickBeeper() and facingNorth() etc. are from. Any help would be appreciated.
I am trying to add a keylistener to this code, but it doesn't recognize the method addKeyListener(this); I am working with kareltherobot so that's where all of the methods like pickBeeper() and facingNorth() etc. are from. Any help would be appreciated.
Java Syntax (Toggle Plain Text)
import java.awt.event.*; import kareltherobot.*; public class PacMan extends Robot implements KeyListener, Directions { public PacMan(int street, int avenue, Direction direction, int beepers) { super(street, avenue, direction, beepers); addKeyListener(this); //this is the part that doesn't work } /**Method definitions for the KeyListener interface (there aren't any errors in this part) **/ public void keyTyped(KeyEvent evt) { char key = evt.getKeyChar(); if (key == 'w') { while(!facingNorth()) turnLeft(); if(frontIsClear()) move(); while(nextToABeeper()) pickBeeper(); } if (key == 'a') { while(!facingWest()) turnLeft(); while(nextToABeeper()) pickBeeper(); } if (key == 's') { while(!facingSouth()) turnLeft(); move(); while(nextToABeeper()) pickBeeper(); } if (key == 'd') { while(!facingEast()) turnLeft(); move(); while(nextToABeeper()) pickBeeper(); } } public void keyPressed(KeyEvent evt) { char key = evt.getKeyChar(); if (key == 'w') { while(!facingNorth()) turnLeft(); if(frontIsClear()) move(); while(nextToABeeper()) pickBeeper(); } if (key == 'a') { while(!facingWest()) turnLeft(); while(nextToABeeper()) pickBeeper(); } if (key == 's') { while(!facingSouth()) turnLeft(); move(); while(nextToABeeper()) pickBeeper(); } if (key == 'd') { while(!facingEast()) turnLeft(); move(); while(nextToABeeper()) pickBeeper(); } } public void keyReleased(KeyEvent evt) { char key = evt.getKeyChar(); if (key == 'w') { while(!facingNorth()) turnLeft(); if(frontIsClear()) move(); while(nextToABeeper()) pickBeeper(); } if (key == 'a') { while(!facingWest()) turnLeft(); while(nextToABeeper()) pickBeeper(); } if (key == 's') { while(!facingSouth()) turnLeft(); move(); while(nextToABeeper()) pickBeeper(); } if (key == 'd') { while(!facingEast()) turnLeft(); move(); while(nextToABeeper()) pickBeeper(); } } }
0
#2 32 Days Ago
Hi Ballen92 and welcome to DaniWeb 
What's the error message you get? If I had to guess on the information you've given us, I would say that the method addKeyListener is not defined for your class. What class does Robot extend?

What's the error message you get? If I had to guess on the information you've given us, I would say that the method addKeyListener is not defined for your class. What class does Robot extend?
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. •
•
Join Date: Nov 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#3 31 Days Ago
The error I get is:
PacMan.java:10: cannot find symbol
symbol : method addKeyListener(PacMan)
location: class PacMan
addKeyListener(this);
As for the Robot class, it does not extend anything, but it is part of a package called karel. I think you are right in that the method addKeyListener is not defined in my class, and it is definitely not defined in the Robot class, but I do not know how or where to define the method. I notice that many people who used KeyListeners often extend classes like JFrame, JPanel, or JApplet. Unfortunately in this case, I am restricted to extend the class Robot.
PacMan.java:10: cannot find symbol
symbol : method addKeyListener(PacMan)
location: class PacMan
addKeyListener(this);
As for the Robot class, it does not extend anything, but it is part of a package called karel. I think you are right in that the method addKeyListener is not defined in my class, and it is definitely not defined in the Robot class, but I do not know how or where to define the method. I notice that many people who used KeyListeners often extend classes like JFrame, JPanel, or JApplet. Unfortunately in this case, I am restricted to extend the class Robot.
•
•
Join Date: Nov 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#5 31 Days Ago
Well unfortunately this is where I am severely limited in using this kareltherobot package. It is a beginners guide for java programming and basically it creates the user interface for me, I don't deal with the actual graphics coding. I suppose I could tinker with the karel package that is given to me and see if I can add a keylistener somewhere in there. So, just to clarify, I can only add a keylistener to something that is graphically represented, like in the user interface? Also, if I extend JFrame or one of those classes, then should the compiler recognize the method addKeyListener?
Thanks for all the help!
Thanks for all the help!
![]() |
Similar Threads
- Problem with FormView.FindControl (ASP.NET)
- Image upload problem (PHP)
- [ask] Need help about KeyListener in jTextArea (Java)
- Problem with Pong (Java)
- extends JFrame implements KeyListener (Java)
- Swing: default button problem (Java)
- need help with my java calculator (Java)
- I lack focus... (Java)
Other Threads in the Java Forum
- Previous Thread: Http Get Request----Need help
- Next Thread: Method problems
| Thread Tools | Search this Thread |
android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database db defaultmethod development dice dragging draw ebook eclipse error event exception formatingtextintooltipjava fractal game givemetehcodez graphics gui hql html ide image infinite input integer invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie numbers openjavafx oracle parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads time tree windows





