문제

I've made a game that is controlled by the arrow keys. Therefore I have a KeyListener listening for the arrow keys:

public void keyReleased(KeyEvent event)
{
    switch(event.getExtendedKeyCode())
    {
        case KeyEvent.VK_UP:
            gameManager.up();
            break;
        case KeyEvent.VK_DOWN:
            gameManager.down();
            break;
        case KeyEvent.VK_RIGHT:
            gameManager.right();
            break;
        case KeyEvent.VK_LEFT:
            gameManager.left();
            break;
    }
}

Now here is my problem:
When I maximize the window using the keyboard by pressing Windows key + Up arrow, these events still get fired. How can I detect that the Windows key has been pressed, while one of the arrow keys got pressed?

도움이 되었습니까?

해결책

Set an flag in your KeyListener, e.g.

boolean windowsPressed;

public void keyPressed(KeyEvent e) {
    if(event.getExtendedKeyCode() == VK.WINDOWS) windowsPressed = true;
}

public void keyReleased(KeyEvent event)
{
    switch(event.getExtendedKeyCode())
    {
        case KeyEvent.VK_UP:
            if(!windowsPressed) gameManager.up();
            break;
        case KeyEvent.VK_DOWN:
            gameManager.down();
            break;
        case KeyEvent.VK_RIGHT:
            gameManager.right();
            break;
        case KeyEvent.VK_LEFT:
            gameManager.left();
            break;
        case KeyEvent.VK_WINDOWS:
            windowsPressed = false;
            break;
    }
}

다른 팁

You can catch the window key in your keyreleased

case: KeyEvent.VK_WINDOWS:
//do nothing

so it wont register the up button when you pressed the window + up

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top