Key Presses only work randomly

This is such a strange problem…

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?

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

I guess you were ironic? :smiley:

Oh not at all im very sorry, I posted the question also in StackOverflow cause sometimes problems this comunity cannot solve they can. And after i found the solution myself i just posted than on stack overflow and then pasted it here.
Sorry i did not mean to look down on this comunity in any way.

Don’t get me wrong, I was kidding :smiley:

Yeah im not :), Just wanted to make sure you did not get me wrong :smiley: