0

I have a problem with my problem. I am currently playing around with KeyListener in a Java applet, the problem is nothing happens when I type a key(No display). Here's the code :

package appl;

import java.applet.Applet;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Appl extends Applet implements KeyListener {

    @Override
    public void keyTyped(KeyEvent ke) {
        System.out.println("Pressed: " + ke.getKeyCode());
    }

    @Override
    public void keyPressed(KeyEvent ke) {
          System.out.println("Pressed: " + ke.getKeyChar());
    }

    @Override
    public void keyReleased(KeyEvent ke) {
          System.out.println("Pressed: " + ke.getKeyChar());
    }



   /* 
  public static void main(String[] args) {

    }
   */
}
4
  • 2
    Implementing a KeyListener doesn't mean your program is using it. You have to add it to your applet. Commented Apr 3, 2014 at 8:05
  • @BackSlash might as well make that an answer Commented Apr 3, 2014 at 8:06
  • Wait a moment, trying to understand it. I had a similar code that was working like that. Hmm..lemme try to check this out then I'll make you the guy who correctly answered me Commented Apr 3, 2014 at 8:08
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to Why CS teachers should stop teaching Java applets. 2) Why AWT rather than Swing? See my answer on Swing extras over AWT for many good reasons to abandon using AWT components. Commented Apr 4, 2014 at 5:07

1 Answer 1

1

Implementing a KeyListener doesn't mean your program is using it. You have to add it to your applet.

public class Appl extends Applet implements KeyListener {

    @Override
    public void keyTyped(KeyEvent ke) {
        System.out.println("Pressed: " + ke.getKeyCode());
    }

    @Override
    public void keyPressed(KeyEvent ke) {
          System.out.println("Pressed: " + ke.getKeyChar());
    }

    @Override
    public void keyReleased(KeyEvent ke) {
          System.out.println("Pressed: " + ke.getKeyChar());
    }



    public void init() {
        // YOUR CODE
        addKeyListener(this);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.