Results 1 to 7 of 7
  1. #1
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default KeyListner won't work

    The code:
    Java Code:
    package --.-----.game;
    
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.ArrayList;
    import java.util.List;
    
    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);
                    System.out.println("This works!");
    		
    	}
    
    	public void keyReleased(KeyEvent e) {
    		toggleKey(e.getKeyCode(), false);
                    System.out.println("This works!");
    		
    	}
    
    	public void keyTyped(KeyEvent arg0) {
                       //not in use
    	}
    	
    	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);
    	}
    
    }
    I'm trying to figure out why this doesn't print "This works" if I press or release anything. It just ignores the keys that I press.

    Does this class look right?

  2. #2
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: KeyListner won't work

    Call this.setFocusable(true); and this.requestFocus(); right after you add the keyListener.

    EDIT: Whoops. Didn't really look at the structure. I have no idea why these work and I assume they do what they are named. Whenever my KeyListeners aren't working it's because I'm missing one or both of these.
    Last edited by Prime624; 04-05-2014 at 08:22 PM.

  3. #3
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: KeyListner won't work

    All right. Why, and what does it do?

    EDIT:
    both of those methods are unrecognized by eclipse.
    Last edited by AlexGraal; 04-05-2014 at 08:11 PM.

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

    Default Re: KeyListner won't work

    Those method calls would need to be for the Game object.
    The InputHandler class doesn't have those methods.

    What class does Game extend?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: KeyListner won't work

    Java Code:
    public class Game extends Canvas implements Runnable{
    EDIT:

    Ok, after adding those right after I add the keylistener, the keylistener now works.

    Why? I'd like to know what those even do?

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

    Default Re: KeyListner won't work

    Why does the code use AWT vs Swing classes?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: KeyListner won't work

    Well, I found a solid looking guide that I'm following and going through.

    If I had to guess, I'd assume that the game itself is actually being built and run through AWT, but Swing is being used just for the frame.

Similar Threads

  1. Replies: 2
    Last Post: 11-25-2012, 02:51 AM
  2. applet call dll work in Win2000 but not work in WinXP
    By manhcuongtin4 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2011, 01:45 PM
  3. Getting SWT app to work on a different OS
    By Ravioli in forum SWT / JFace
    Replies: 0
    Last Post: 02-20-2011, 09:35 AM
  4. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 02:58 AM

Posting Permissions

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