Question

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) {

    }
   */
}
Was it helpful?

Solution

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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top