Skip to main content
gamedev.net
🔒 Locked

Java keyboard handling

Started by RedUnderTheBed Sep 18, 2006 at 7:13 PM 2 replies 1.4k views
Original Post
RedUnderTheBed
RedUnderTheBed
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.
Bob Janova
Bob Janova
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 :(.
amannis
amannis
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 NullPointerExceptionspublic 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();}
RedUnderTheBed
RedUnderTheBed
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.