Sign in to follow this  
kevin_06s

Java Diagonal Movement

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
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.
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this