Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: KeyListener is not working

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post KeyListener is not working

    I'm programming a game, but the keyListener doesn't work. Here is the source code:

    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    public class InputHandler implements KeyListener{
     
    	public InputHandler(Game game) {
    		game.addKeyListener(this);
    	}
     
    	public class Key {
     
    		private boolean pressed = false;
     
    		public boolean isPressed() {
    			return pressed;
    		}
     
    		public void toggle(boolean isPressed) {
    			pressed = isPressed;
     
    		}
    	}
     
     
    	public Key up = new Key();
    	public Key down = new Key();
    	public Key left = new Key();
    	public Key right = new Key();
     
    	public void keyPressed(KeyEvent e) {
    		toggleKey(e.getKeyCode(),true);
    	}
     
    	public void keyReleased(KeyEvent e) {
    		toggleKey(e.getKeyCode(),false);
    	}
     
    	public void keyTyped(KeyEvent e) {
     
    	}
     
    	public void toggleKey(int keyCode, boolean isPressed) {
    		if(keyCode == KeyEvent.VK_W){
    			up.toggle(isPressed);
    			}
    		if(keyCode == KeyEvent.VK_S){
    			down.toggle(isPressed);
    			}
    		if(keyCode == KeyEvent.VK_A){
    			left.toggle(isPressed);
    			}
    		if(keyCode == KeyEvent.VK_D){
    			right.toggle(isPressed);
    			}
    	}
     
     
    }

    and in class Game:
    ...
    public InputHandler input = new InputHandler(this);
    ...
    //in a method, that gets called every gametick:
    if(input.up.isPressed()==true){
    	System.out.println("up"); //This doesn't work!
    }

    My Question is: Why will it not say "up", if I press "w" ? Thanks in advance for every aswer
    Last edited by Micha S.; June 1st, 2014 at 05:38 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: KeyListener is not working

    Are the listener methods called? The component needs the focus to receive key events.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Micha S. (June 1st, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: KeyListener is not working

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link and Norm's instructions.

  5. #4
    Junior Member
    Join Date
    May 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: KeyListener is not working

    They don't get called, I just noticed that. ^^
    How do I set the focus? I have already tried "frame.requestFocus();" in the constructor of Game.. but actually, I have no Idea what that should do
    I never did something like that

    --- Update ---

    Okay, I tried it again and somehow it works and I have no idea, why it's working now ^^ (added frame.requestFocus(); to the Game class) Thank you, Norm, for helping and I hope my code is posted appropriately now

Similar Threads

  1. KeyListener not working for JLIST Component.
    By nageshvk in forum AWT / Java Swing
    Replies: 1
    Last Post: May 5th, 2014, 05:58 AM
  2. [SOLVED] keyListener keyPressed not working
    By hogsgonewild in forum What's Wrong With My Code?
    Replies: 19
    Last Post: October 13th, 2013, 02:34 PM
  3. KeyListener isn't working while jFrame not active
    By aslanali555 in forum AWT / Java Swing
    Replies: 3
    Last Post: August 22nd, 2013, 01:16 PM
  4. keyListener not working
    By soradogoof in forum Java Applets
    Replies: 2
    Last Post: August 14th, 2012, 05:33 PM
  5. My KeyListener is not Working!!
    By DarrenReeder in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2010, 05:18 PM

Tags for this Thread