NYAH
journeyman
Reged: 07/11/04
Posts: 58
Loc: NJ
|
|
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
|
rappermas
addict
Reged: 06/28/04
Posts: 537
Loc: Colorado
|
|
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?
|
hiwa
Carpal Tunnel
Reged: 06/21/03
Posts: 7704
Loc: Japan
|
|
> 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*
|
NYAH
journeyman
Reged: 07/11/04
Posts: 58
Loc: NJ
|
|
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
|
timotta
stranger
Reged: 09/05/08
Posts: 1
|
|
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
|