Spidermaninja
Spidermaninja

Reputation: 82

How to use keyListener in Java applet

I have absolutely no idea on how to use KeyListener in an applet (okay, I've got an okay idea). I know it has something to do with setting focus on the applet, but I have no clue as to how to do that. My program runs fine as an application, but it just won't take in keyboard input as an applet. If someone could give me some help, that would be awesome.

My code is:

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

Upvotes: 3

Views: 1733

Answers (1)

Roman C
Roman C

Reputation: 1

You was right, you need to set it focusable at the end of init method

setFocusable(true);

if you don't do it you at least have to click in the panel to get encountered keys.

Upvotes: 2

Related Questions