# [java] Key Listener Problem

This topic is 4745 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I use the awt Key Listener methods in java in my new game. I use the regular keyPressed(KeyEvent e) method, but i have some strange errors. inside the keyPressed() method, i have a bunch of if statements which check to see which key was hit: if (e.getKeyCode() == KeyEvent.VK_LEFT){ ... } if (e.getKeyCode() == KeyEvent.VK_RIGHT){ ... } ...etc Now, the strange part is...when I push and hold Up-arrow and push and hold Space at the same time...when I then try to push left-arrow, nothing happens. There is no event. BUT, when I do the same thing, push and hols space and up-arrow, but instead push right-arrow, i get an event =/ Why is this happening? Thanx a lot

##### Share on other sites
Heres just a thought. Check your numlock while testing.

Otherwise you probably just missed a semicolon. Cause KeyListener should capture all the key events that your trying to catch.

 Also if you press two or more keys down the last key pressed will send events if a third key is pressed and then released while the other two are still held down no event will occur. You'll just have to work around it I'm afraid. You can use the keyReleased method in KeyListener to determine which keys are still being held down and which ones are already released.

##### Share on other sites
Actually, I use the Cursor key, not the num pad...but anyway..i don't think i've missed anything...here's the code

public void keyPressed(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_SPACE){
System.out.println("SPACE");
}
if (e.getKeyCode() == KeyEvent.VK_LEFT){
System.out.println("LEFT");
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
System.out.println("RIGHT");
}
if (e.getKeyCode() == KeyEvent.VK_UP){
System.out.println("UP");
}
}

##### Share on other sites

I tested your problem with almost the exact same handler however the output I got is that when I hold down the up-arrow and the spacebar whichever one was held down last continually sends events and then when I press and hold down the left arrow key I get left arrow events.

##### Share on other sites
ok..but how come it works when i press UP+Right+Space?
it only bugs when I press Up+Left+Space...that's the wierd thing...or Up+Space+Left...it's like Left and Space don't like each other :P

##### Share on other sites
Here is the code I used and it worked fine no glitches as long as mumlock was on and I kept all three keys down.

public void keyPressed(KeyEvent e) {        if( e.getKeyCode() == KeyEvent.VK_SPACE ) {			System.out.println("Spacebar is pressed!");		}    	if (e.getKeyCode() == KeyEvent.VK_LEFT){			System.out.println("Left arrow pressed!");		}		if (e.getKeyCode() == KeyEvent.VK_RIGHT){			System.out.println("Right arrow pressed!");		}		if( e.getKeyCode() == KeyEvent.VK_UP ) {			System.out.println( "Up arrow pressed!" );			}		if( e.getKeyCode() == KeyEvent.VK_DOWN ) {			System.out.println( "Down arrow pressed!");			}    }	public void keyReleased(KeyEvent e) {		if( e.getKeyCode() == KeyEvent.VK_SPACE ) {			System.out.println("Spacebar is released!");		}    	if (e.getKeyCode() == KeyEvent.VK_LEFT){			System.out.println("Left arrow released!");		}		if (e.getKeyCode() == KeyEvent.VK_RIGHT){			System.out.println("Right arrow released!");		}		if( e.getKeyCode() == KeyEvent.VK_UP ) {			System.out.println( "Up arrow released!" );			}		if( e.getKeyCode() == KeyEvent.VK_DOWN ) {			System.out.println( "Down arrow released!");			}	}

I added this listener to a frame using the addKeyListener method in the frames constructor.

##### Share on other sites
incredible...still the same problem...

##### Share on other sites
I can't seem to duplicate your problem so it must be something other than the code.

##### Share on other sites
Well, I made a new application which only sets up a frame and a key listener and uses your code. I get the exact same result. I guess there could be an interrupt between the keyboard and software or something. I don't think it's the code...

##### Share on other sites
There are many keyboards that don't support all key combinations. My keyboard doesn't support the combination W, Cursor-Up, Cursor-Left, not regestering Cursor-Left, but handling Cursor-Right just fine. That's a big problem in multiple-players-on-one-keyboard games.

Try another keyboard. Also, try using the separate cusor keys instead of the numeric cursor keys, or vice versa.

• 40
• 15
• 10
• 23
• 19