Jump to content
  • Advertisement
Sign in to follow this  
RedUnderTheBed

Java keyboard handling

This topic is 4445 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

Hey, I've been building a game in java. It's a two player deathmatch shooter. So far I've just been trying to get all the clipping, shooting, etc, to work on its own. But now, after adding a second player, I've run into a problem with the keyboard handling. You can only press about two or three buttons at a time, and people will often need to strafe, turn and shoot in this game, I'm hoping to find some bettter key handling code or some other way around it. Thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement
Unfortunately, this is likely to be a problem with the hardware; a lot of modern keyboards won't let you press more than three keys at once. I have had several big business games that suffered from the same thing, so I don't think there is a software solution :(.

Share this post


Link to post
Share on other sites
Been quite a long time since I used Java2D (for games)... but the problem might be that you're doing too much work in the AWT-dispatch thread.

Try something like this:


boolean[] keysDown = new boolean[256]; // Might need a bigger array, to prevent NullPointerExceptions

public void keyPressed(KeyEvent e) {
keysDown[e.getKeyCode()] = true;
// Nothing else here!
}

public void keyReleased(KeyEvent e) {
keysDown[e.getKeyCode()] = false;
// Nothing else here!
}


Now in your game loop, you'd have something like this:

if (keysDown[KEY_STRAFE_LEFT]) {
player.strafeLeft();
}

Share this post


Link to post
Share on other sites
To the person above, thank you for the suggestion but that's already what I'm doing.

To the person above that, I've actually already built a similar game in a dos version of C++. There was a little bit of a problem with handling so many key presses but the game was still playable. Given that flucuation I'm not sure it would be a problem with the hardware, especially seeing as I've tried it on several different computers with the exact same results. I'm more inclined to think that the code that java uses to get the key presses wasn't made to handle so many. Anyway, I think in the end I'm just going to have to make it a network multiplayer game. Thanks for your help anyway.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!