Please join us at the new JavaWorld Q&A Forums. Your existing login will work there. The discussions here are now read-only.


JavaWorld >> Java Beginner

Pages: 1
NYAH
journeyman


Reged: 07/11/04
Posts: 58
Loc: NJ
2 Keys at Once
      #9643 - 07/24/04 02:57 PM

If you are in Microsoft Word and you hold down a key, lets say "h" and then you press another key, lets say "k", while still holding "h", the "h" will stop printing.
"hhhhhhhhhhhhhhhkkkkkkkkkkkkkkk" <--that is what you get
However, if you are playing a game, lets say CounterStrike. And you hold "W"(move forward) then also hold "D"(move right) you will move both forward and right at the same time.

If you use KeyListener in java, it will act like Microsoft Word. How do you make it act like CounterStrike where you can be holding two or more keys at one time and they will both register?
If you want an example, lets say I'm trying to make a two player pong applet where each player has two keys, one for up and the other for down, and you want the players to be able to hold the keys down to move. Thanks

-Anthony


Post Extras: Print Post   Remind Me!   Notify Moderator  
rappermas
addict


Reged: 06/28/04
Posts: 537
Loc: Colorado
Re: 2 Keys at Once [Re: NYAH]
      #9646 - 07/24/04 04:10 PM

What counterstrike does is listen for a key and then determines if another is down while that one is being pressed. What you would have to do is get a boolean value called up and set it to false. In the keyPressed method that you override, if the up key is pressed, set up to true. Then, when you type the next key, up was never released, but now there's information for the down key about what state the up key is in. In keyReleased, if the up key is released, set the variable to false. Do you understand this or do you need more help?

--------------------
If an orange is orange, then why isn't a lime a green and a lemon a yellow?


Post Extras: Print Post   Remind Me!   Notify Moderator  
hiwa
Carpal Tunnel


Reged: 06/21/03
Posts: 7704
Loc: Japan
Re: 2 Keys at Once [Re: NYAH]
      #9648 - 07/24/04 04:21 PM

> And you hold "W"(move forward) then also hold "D"(move right)
> you will move both forward and right at the same time.
Interesting problem. But it's not the problem of Java KeyListener per se because PC keyboard does auto-repeat by default. It is rather an issue of game parameters manipulation.

For example, in your keyPressed() method, you can set W_is_being_Pressed flag and/or D_is_being_Pressed flag and the game could proceed accordingly. You should reset the flags in the keyReleased() method.


--------------------
*stop cruelty* Annual number of euthanized cats&dogs: US 5M, JP 500K.*for our better karma*


Post Extras: Print Post   Remind Me!   Notify Moderator  
NYAH
journeyman


Reged: 07/11/04
Posts: 58
Loc: NJ
Re: 2 Keys at Once [Re: hiwa]
      #9675 - 07/25/04 03:37 PM

thx, I'm not actually doing anything with this but my friend was and I was just wondering, I'll try it out after I finish my current project.

-Anthony


Post Extras: Print Post   Remind Me!   Notify Moderator  
timotta
stranger


Reged: 09/05/08
Posts: 1
Re: 2 Keys at Once [Re: NYAH]
      #183432 - 09/05/08 08:48 AM

I develop a solution for this problem. An KeyListener adapter that have the keyDown and keyUp events. You can get it here: http://programandosemcafeina.blogspot.com/2007/06/download-da-timotta-api.html and you can see an example of use here: http://programandosemcafeina.blogspot.com/2008/09/keydown-e-keyup-para-applets-java.html

Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 3 anonymous users are browsing this forum.

Moderator:   

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is disabled
      UBBCode is enabled

Rating:
Topic views: 7538

Rate this topic

Jump to

Contact us JavaWorld

Powered by UBB.threads™ 6.5.5