我在 Java 中遇到了更多关键问题。Key 事件本身就可以正常工作,SPACE并且当按下其他键时......
但是,如果我同时按下UP键和LEFT键,则SPACE事件不会触发。但是,SPACE当单独按下或按下或与其他键一起按下时,它会LEFT触发UP。
这是我的代码:
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_UP)
{
upkeyisdown = true;
}
if(e.getKeyCode() == KeyEvent.VK_DOWN)
{
downkeyisdown = true;
}
if(e.getKeyCode() == KeyEvent.VK_LEFT)
{
leftkeyisdown = true;
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT)
{
rightkeyisdown = true;
}
if(e.getKeyCode() == KeyEvent.VK_SPACE)
{
spacekeyisdown = true;
}
}
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_UP)
{
upkeyisdown = false;
}
if(e.getKeyCode() == KeyEvent.VK_DOWN)
{
downkeyisdown = false;
}
if(e.getKeyCode() == KeyEvent.VK_LEFT)
{
leftkeyisdown = false;
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT)
{
rightkeyisdown = false;
}
if(e.getKeyCode() == KeyEvent.VK_SPACE)
{
spacekeyisdown = false;
}
}