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.

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();
         }
      }
    }

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?

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.

addKeyListener is only implemented for user interface components, so sooner or later you will need to use a JFrame or whatever. How is your pacman displayed?

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.