Results 1 to 4 of 4
- 12-17-2011, 06:56 PM #1
Detecting if the player presses q or e
Hello, I'm making a game in where the player switches through weapons with the q and e keys.
I have been able to tell if the player is pressing them in their capitalized state with this:
However I want to know if the player presses them capitalized or not.Java Code:public void keyTyped(KeyEvent e) { if(e.getKeyChar() == KeyEvent.VK_Q) { dim--; if(dim<0) dim=2; } if(e.getKeyChar() == KeyEvent.VK_E) { dim++; if(dim>2) dim=0; } }
Thanks! :D
- 12-17-2011, 07:00 PM #2
Re: Detecting if the player presses q or e
To see what keys are being pressed, add a println to the keyPressed method to print out the event that is passed to the listener. You will see that most of the keys on the keyboard will pass an event to the listener. Press the keys that you are interested in tracking and see what is printed.
For keeping track of when a key is released, you will also need to look at the keyReleased method.
- 12-17-2011, 07:11 PM #3
Re: Detecting if the player presses q or e
I changed my code to:
And moved it to keyPressed and it worked perfectly!Java Code:if(e.getKeyChar() == 'q') { dim--; if(dim<0) dim=2; } if(e.getKeyChar() == 'e') { dim++; if(dim>2) dim=0; }
I was able to figure it out by println'ing getKeyChar like you said.
I feel sort of silly considering how simple it was :[
Thank you!
- 12-18-2011, 04:38 PM #4
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
Similar Threads
-
How to ERROR if user presses Enter?
By net2chris in forum New To JavaReplies: 3Last Post: 10-03-2011, 05:30 PM -
Why isnt my program detecting any key presses?
By rajkobie in forum New To JavaReplies: 6Last Post: 05-19-2011, 05:45 PM -
Multiple keyboard presses
By abbeywell in forum Java GamingReplies: 6Last Post: 05-02-2011, 04:57 PM -
Responding to button presses
By Java Tip in forum javax.swingReplies: 0Last Post: 06-26-2008, 07:38 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks