Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By zweibieren

Thread: Detecting if the player presses q or e

  1. #1
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default 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:

    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;
    		}
    		
    	}
    However I want to know if the player presses them capitalized or not.
    Thanks! :D

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default 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.

  3. #3
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Re: Detecting if the player presses q or e

    I changed my code to:

    Java Code:
    if(e.getKeyChar() == 'q')
    		{
    			dim--;
    			if(dim<0)
    			dim=2;
    
    		}
    		
    		if(e.getKeyChar() == 'e')
    		{
    			dim++;
    			if(dim>2)
    			dim=0;
    		}
    And moved it to keyPressed and it worked perfectly!
    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!

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default Re: Detecting if the player presses q or e

    You might consider using a switch statement. It becomes more attractive as the number of options increases.
    Java Code:
           switch(e.getKeyChar()) {
           case 'q': 
                dim--;
                if(dim<0)
                       dim=2;
                break;
           case 'e': 
                dim++;
                if(dim>2)
                       dim=0;
                break;
           }
    DarrylBurke likes this.

Similar Threads

  1. How to ERROR if user presses Enter?
    By net2chris in forum New To Java
    Replies: 3
    Last Post: 10-03-2011, 05:30 PM
  2. Why isnt my program detecting any key presses?
    By rajkobie in forum New To Java
    Replies: 6
    Last Post: 05-19-2011, 05:45 PM
  3. Multiple keyboard presses
    By abbeywell in forum Java Gaming
    Replies: 6
    Last Post: 05-02-2011, 04:57 PM
  4. Responding to button presses
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 07:38 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •