Jump to content
  • Advertisement
Sign in to follow this  
Stroker_Ace

[java] KeyListener - Multiple keyPressed events when key held.

This topic is 3719 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am intendiung to use the KeyListener interface in order to detect user input for my game. I have written a small test program and when I hold a key down I receive multiple keyPressed events which I was not expecting. When I release the key I get a single keyReleased event as expected. I realize I could store the state (up or down) of the keys I am interested in and only perform processing when this state changes however I imagine I would spend a lot of time processing superfluous keyPressed events when the user holds a key down. Is there any way to only receive one keyPressed event when the user holds a key down or am I stuck with checking the key state (up or down) each time I receive a keyPressed event? BTW - I am using Java 1.6 on Windows XP. Thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement
I noticed this back when I was a DOS programmer, writing an interrupt handler for the keyboard. This is just the way the hardware works. So yes, you're stuck storing the state.

Share this post


Link to post
Share on other sites
I usually just use a single KeyListener and create an InputManager with it that keeps two boolean arrays for the 256 keys that can be pressed. I wrote a blog post about how to create one. I am going to post my code directly in a moment.

It also handles mouse movement and mouse events. It's hardly all inclusive it needs to handle double clicks for mouse plus needs a bit of refining for the capturing the time a key has been down but its something to work from.

A link to my blog is in my profile signature below! It's the code snippets link.

Share this post


Link to post
Share on other sites
We've run into some similar issues. One of the problems is that games based on constant keyboard input will behave differently based on the user's system setting for the keyboard repeat rate.

In general, actions can't just fire every time you get keyboard input. Instead, you might need to fire your own events at the beginning of the game loop. Using Java's KeyListeners, you can gather information about what happens between frames, and then use your own listening architecture to handle the events.

This provides a more constant experience for the end users. Without it, one user might be able to move much faster than another in a simple situation such as (if (keyboard.x) player.x += 1).

Share this post


Link to post
Share on other sites
Quote:
Without it, one user might be able to move much faster than another in a simple situation such as (if (keyboard.x) player.x += 1).

Those types of situation are best addressed with timed movement. So it wouldn't be an issue if you are keeping track of the time between events being fired to a single KeyListener.

So you'd have

if(keyboard.y) player.x += speed * dt;

which is more accurate for moving objects in games.

Share this post


Link to post
Share on other sites
And it gets better: Only certain combinations of keys can be held at once (usually up to a maximum of three or four keys) and these combinations vary from keyboard to keyboard. So always make the keyboard controls user-definable.

I totally agree with 5MinuteGaming that movement should be timer based rather than moving on key events. The role of key pressed and released events is to start and stop movement respectively.

Share this post


Link to post
Share on other sites
I understand the application of the time delta for constant movement, but what about tile-based games where an object is moving between locations on a grid?

We're creating a visual programming environment where users can create casual games and multimedia applications. There are several ways for users to make objects move. One is by adding a value to the position, as would happen on a grid. How do you take into account the keyboard rate without correlating it to the game loop in this situation?

We've also noticed issues with multiple keys as well. The most basic one is when you're holding down the arrow key to move and then hit the spacebar to fire. You lose key press events for the arrow key. But, as mentioned, if you're just keeping track of which keys are up and down I think it should work, since supposedly you still get the keyReleased event (but you might not... what then?).

P.S. http://beta.sharendipity.com just went up if anyone wants to check it out.

Share this post


Link to post
Share on other sites
Quote:
I understand the application of the time delta for constant movement, but what about tile-based games where an object is moving between locations on a grid?

We're creating a visual programming environment where users can create casual games and multimedia applications. There are several ways for users to make objects move. One is by adding a value to the position, as would happen on a grid. How do you take into account the keyboard rate without correlating it to the game loop in this situation?


You should still keep track of keyboard states and trigger an event when the state changes. But in your case, your game shouldn't talk to this keyboard handler directly. Instead, add a handler that generates fake keyboard events, and uses a timer to do so, with your game logic getting events from that. Now you can guarantee the same delay and repeat rate across all computers.

Share this post


Link to post
Share on other sites
It's bit tricky to do that. you need track 'keys movement' by scanning keyPressed() and keyReleased() function.

I made a ready made game library, which already include keyboard events, mouse events, display configuration, image manipulation (rotation, scaling, making it to become transparent), and adding game font... it really save your time.

and the best of all, it's free!

Just try & download it here:
http://fly.engine.googlepages.com

sample + tutorial + documentation are provided.
Good luck...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!