Java Diagonal Movement

Started by
1 comment, last by Hollandera 12 years, 10 months ago
Hello, I am trying to create an arrow key movement system that is familiar to all gamers, but Java is not cooperating.
In my program, I am using a KeyListener and keeping track of keystates with booleans, like the GameDev Wiki says to do.

After some testing, I found out that if I press and hold a diagonal like up-left and release it, everything works fine.
However, if I press and hold up-left, then release only left, Java will not fire a KeyEvent for left being released until up is also released!

I want to be able to detect the left being released, and I have no idea how to do so.
Advertisement
Any chance you could post some code?

I can only think that the component listening for KeyEvents is losing focus.

I have seen odd problems like this with old PS/2 keyboards (Hardware limitation), but this seems unlikely. :)
Structure you're Key event handling as follows.(Don't forget to use your keyreleased function) Handle all events in your gameUpdate() based on what keys are currently set as pressed.

public void keyPressed(KeyEvent k)
{
int keyCode = k.getKeyCode();

switch (keyCode)
{
case KeyEvent.VK_LEFT:
{
setLeftPressed(true);
setRightPressed(false);
break;
}
case KeyEvent.VK_RIGHT:
{
setRightPressed(true);
setLeftPressed(false);
break;
}
case KeyEvent.VK_UP:
{
setUpPressed(true);
setDownPressed(false);
}
//ect for all the keys you need.
}
}

This topic is closed to new replies.

Advertisement