• Post Reply Bookmark Topic Watch Topic
  • New Topic

Make it possible to handle 2 keypressed  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey!

I am making a game and I got stuck and would really need some help.
The game works that you can move left and right and shoot stuffs.
At the moment I cant move and shoot at the same time, only one of them at a time. And thats something I want to be able to do at the same time.
Is there any solution? I am using KeyEvent.

Sincerely,
 
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe there are only some specific cases where a keyboard will transmit knowledge of two (or more) keys being press, and that is the shift key, the alt/cmd key, and the control (ctrl) key. These can be used in conjunction with other keys but as far as I know there's no way to tell if a shift key, for instance, is pressed by itself.
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

I've done something like this myself, and what you need to do is store each pressed key. Don't use keyPressed but keyDown and keyUp. In keyDown add the key to the list of keys, and in keyUp remove it again.
 
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought keyDown and keyUp were deprecated decades ago. There is no guarantee that they will exist in future.
I have always done this by setting a boolean on keyPressed, and unsetting it on keyreleased. Note that you can get multiple keyPressed events (on the same key) without getting any intervening keyReleased events (when you hold a key down, you will continue to get keyPressed events at a platform-specific rate).
 
Sheriff
Posts: 22845
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:I thought keyDown and keyUp were deprecated decades ago.


Yes, "decades ago" is right:

The API docs wrote:As of JDK version 1.1, replaced by processKeyEvent(KeyEvent).


Like Fred says, inspect the KeyEvent to see if it's KEY_PRESSED or KEY_RELEASED... and as usual the API docs have a lot more helpful hints about how to deal with keyboard events.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:I thought keyDown and keyUp were deprecated decades ago. There is no guarantee that they will exist in future.
I have always done this by setting a boolean on keyPressed, and unsetting it on keyreleased. Note that you can get multiple keyPressed events (on the same key) without getting any intervening keyReleased events (when you hold a key down, you will continue to get keyPressed events at a platform-specific rate).

I actually meant those (the KeyListener methods), I didn't realise they were named differently.

About key pressed, are you sure that triggers multiple events? I thought that keyPressed was only called when you start pressing the key. I'd create a little test but I can't be bothered enough right now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!