简体   繁体   中英

Key listener and key bindings doesnt work every compile

Basically i have created a key listener connected to my JPanel.. And sometimes when i compile and run it it does work realy good and no problems what so ever.. But then sometimes when i compile it wont work. It doesn't recognise keypresses.

Here is the code i used for key listener in my JPanels update method:

if(keyListener.getMovingRight() == KeyEvent.VK_RIGHT){
            player.playerMoveRight();
        }
        if(keyListener.getMovingLeft() == KeyEvent.VK_LEFT){
            player.playerMoveLeft();
        }
        if(keyListener.getMovingUp() == KeyEvent.VK_UP){
            player.playerMoveUp();
        }
        if(keyListener.getMovingDown() == KeyEvent.VK_DOWN){
            player.playerMoveDown();
        }

And the above code knows if keys was pressed from this class:

public class KeyBoard implements KeyListener {

    private int playerMoveUp;
    private int playerMoveDown;
    private int playerMoveLeft;
    private int playerMoveRight;

    public KeyBoard(){
        playerMoveUp = 0;
        playerMoveDown = 0;
        playerMoveLeft = 0;
        playerMoveRight = 0;
    }

    @Override
    public void keyTyped(KeyEvent keyEvent) {
    }

    @Override
    public void keyPressed(KeyEvent keyEvent) {

        //Switch statement to get which keys were pressed
        switch(keyEvent.getKeyCode()){

            case KeyEvent.VK_UP:
                playerMoveUp = keyEvent.getKeyCode();
                break;

            case KeyEvent.VK_DOWN:
                playerMoveDown = keyEvent.getKeyCode();
                break;

            case KeyEvent.VK_LEFT:
                playerMoveLeft = keyEvent.getKeyCode();
                break;

            case KeyEvent.VK_RIGHT:
                playerMoveRight = keyEvent.getKeyCode();
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent keyEvent) {

        //Switch statement to get which keys were released
        switch(keyEvent.getKeyCode()){

            case KeyEvent.VK_UP:
                playerMoveUp = 0;
                break;

            case KeyEvent.VK_DOWN:
                playerMoveDown = 0;
                break;

            case KeyEvent.VK_LEFT:
                playerMoveLeft = 0;
                break;

            case KeyEvent.VK_RIGHT:
                playerMoveRight = 0;
                break;
        }
    }

    public int getMovingUp(){
        return playerMoveUp;
    }
    public int getMovingDown(){
        return playerMoveDown;
    }
    public int getMovingLeft(){
        return playerMoveLeft;
    }
    public int getMovingRight(){
        return playerMoveRight;
    }
}

And i ofcourse added the key listener to the JPanel by doing this:

panel.addKeyListener(KeyBoardClasshere);

Since it wasnt working regularly i tried something else called Key Bindigs since i heard that would increase my chances..

I added this in my JPanel class and commented out the key listener in the update method:

public void keyBindingsInitialize(ActionMap am, InputMap im){
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "Right");
        am.put("Right", RightBind);

        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "Left");
        am.put("Left", LeftBind);

        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "Up");
        am.put("Up", UpBind);

        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "Down");
        am.put("Down", DownBind);
    }
    
    Action RightBind = new AbstractAction(){
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            player.playerMoveRight();
        }
    };
    Action LeftBind = new AbstractAction(){
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            player.playerMoveLeft();
        }
    };
    Action UpBind = new AbstractAction(){
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            player.playerMoveUp();
        }
    };
    Action DownBind = new AbstractAction(){
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            player.playerMoveDown();
        }
    };

This also works but sometimes when i compile and run the project it just doesnt work and then it works if i terminate the project and then run it again.. Its soo strange in my oppinion.. Anyone have suggestions?

EDIT: I followed the answear on this question: KeyListener on JPanel randomly unresponsive

I do get response from clicking the button that apears and the spacebar but not the keys that is used in the update method.. And now after i added the button sometimes when i run the application all i get is a grey window and have to restart it a couple of times to make it normal again just as in the question i linked.

Try adding

setFocusable(true);

to your JPanel's constructor.

Solved it by adding frame.revalidate(); in my game loop. Thanks for all answears!

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