简体   繁体   中英

Key Input Doesn't Work Until Enter is Pressed

I'm making a Java Game, and whenever I start it up, none of the keys work, until I press enter. Here's the key input code:

if(KeyInput.currentKey(KeyInput.W)) player.setUp();
if(KeyInput.currentKey(KeyInput.A)) player.setLeft();
if(KeyInput.currentKey(KeyInput.S)) player.setDown();
if(KeyInput.currentKey(KeyInput.D)) player.setRight();
if(KeyInput.currentKey(KeyInput.UP)) player.setDirection(0);
if(KeyInput.currentKey(KeyInput.RIGHT)) player.setDirection(1);
if(KeyInput.currentKey(KeyInput.DOWN)) player.setDirection(2);
if(KeyInput.currentKey(KeyInput.LEFT)) player.setDirection(3);
if(KeyInput.currentKey(KeyInput.ENTER)) player.setPunching();

I have a class called KeyInput and the method currentKey just checks if there is a key being pressed down, and if there is, it checks if the key in the parameters is the current one. Here's that code:

public class KeyInput {

public static final int NUM_KEYS = 11;

public static boolean keyState[] = new boolean[NUM_KEYS];
public static boolean prevKeyState[] = new boolean[NUM_KEYS];

public static int W = 0;
public static int A = 1;
public static int S = 2;
public static int D = 3;
public static int ENTER = 4;
public static int UP = 5;
public static int LEFT = 6;
public static int DOWN = 7;
public static int RIGHT = 8;
public static int ESCAPE = 9;
public static int SHIFT = 10;

public static void setKey(int i, boolean b) {
    if(i == KeyEvent.VK_W) keyState[W] = b;
    else if(i == KeyEvent.VK_A) keyState[A] = b;
    else if(i == KeyEvent.VK_S) keyState[S] = b;
    else if(i == KeyEvent.VK_D) keyState[D] = b;
    else if(i == KeyEvent.VK_ENTER) keyState[ENTER] = b;
    else if(i == KeyEvent.VK_UP) keyState[UP] = b;
    else if(i == KeyEvent.VK_LEFT) keyState[LEFT] = b;
    else if(i == KeyEvent.VK_DOWN) keyState[DOWN] = b;
    else if(i == KeyEvent.VK_RIGHT) keyState[RIGHT] = b;
    else if(i == KeyEvent.VK_ESCAPE) keyState[ESCAPE] = b;
    else if(i == KeyEvent.VK_SHIFT) keyState[SHIFT] = b;
}

public static void update() {
    for(int i = 0; i < NUM_KEYS; i++) {
        prevKeyState[i] = keyState[i];
    }
}

public static boolean currentKey(int i) {
    return keyState[i];
}

}

That setKey method is called in the Main class:

public void keyTyped(KeyEvent key) {}
public void keyPressed(KeyEvent key) {
    KeyInput.setKey(key.getKeyCode(), true);
}
public void keyReleased(KeyEvent key) {
    KeyInput.setKey(key.getKeyCode(), false);
}

I don't know why this is happening, and I don't know if you guys will be able to help without knowing everything about the way my game has been programmed. Thanks in advance! :)

imho your code does not contain enough information to solve the problem.

Check the point/object you are initializing the KeyListener. maybe it is called the first time on a Enter-Key related action?

edit: you should add a keyListener to your panel. you cant handle Listener like normal classes, they are a special kind of classes called Interface, its like a extension for a normal class. Add the KeyListener Interface to your games panel and it will observe the panel for keyboard related actions.

you can find a game related example here: http://www.edu4java.com/en/game/game4.html

it should look something like this:

JPanel gamePanel = new JPanel();
gamePanel.addKeyListener(new KeyListener(){

    @Override
    public void keyPressed(KeyEvent arg0) {
        if(arg0.getExtendedKeyCode() == KeyEvent.VK_ENTER) player.setPunching();
        //and so on
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent arg0) {

    }

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM