How to use keyListener in Java applet

I have absolutely no idea how to use KeyListener

in an applet (okay, I have a good idea). I know it has to do with setting focus to the applet, but I don't know how. My program works fine as an app, but it just won't enter the keyboard as an applet. If anyone can help me that would be awesome.

My code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Window extends JApplet {

    static Ship ship;
    static ColorPanel panel;
    static boolean up=false, down=false, left=false, right=false;

            public static class PanelListener implements KeyListener{

        public void keyPressed(KeyEvent e){
            switch(e.getKeyCode()) {
                    case 38: ship.setMoving(true); down = true;break;
                    case 40: ship.setMoving(true); up = true;break;
                    case 37: ship.setMoving(true); left = true;break;
                    case 39: ship.setMoving(true); right = true;break;
                    case 32: ship.setFiring(true); break;
                    case 49: ship.setSwitching(true); ship.switchOrdinance(49); break;
                    case 50: ship.setSwitching(true); ship.switchOrdinance(50); break;
                    case 51: ship.setSwitching(true); ship.switchOrdinance(51); break;
                    case 52: ship.setSwitching(true); ship.switchOrdinance(52); break;
                    case 83: if(ship.shieldState()){ship.shieldOff();}else{ship.shieldOn();}
            }
            ship.setDirection(up,down,left,right);
            ship.setVelocity(3);
        }
        public void keyReleased(KeyEvent e){
            switch(e.getKeyCode()) {
                case 38: ship.setMoving(false); down = false; break;
                case 40: ship.setMoving(false); up = false; break;
                case 37: ship.setMoving(false); left = false; break;
                case 39: ship.setMoving(false); right = false; break;
                case 32: ship.setFiring(false); break;
                case 49: ship.setSwitching(false); break;
                case 50: ship.setSwitching(false); break;
                case 51: ship.setSwitching(false); break;
                case 52: ship.setSwitching(false); break;
                default: ship.setMoving(false); ship.setFiring(false); ship.setSwitching(false); left=false; right=false; up=false; down=false;
            }
            ship.setDirection(up,down,left,right);
            if(!up || !down || !right || !left){
                ship.setVelocity(0);
            }
        }
        public void keyTyped(KeyEvent e){
        }
    }

        public void init(){
            Container pane;
            ship=new Ship();
            panel = new ColorPanel(Color.black, 1340, 640);
            panel.sendShip(ship);
            pane=getContentPane();
            pane.add(panel);
            addKeyListener(new PanelListener());
        }
}

      

+3


source to share


1 answer


You were right, you need to set it as focus at the end of the method init

setFocusable(true);

      



If you don't, you at least need to click on the panel to get the keys found.

+2


source







All Articles