Sign in to follow this  
Flawe

[java] Key Listener Problem

Recommended Posts

Flawe    114
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 this post


Link to post
Share on other sites
5MinuteGaming    274
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.

[Edit] 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 this post


Link to post
Share on other sites
Flawe    114
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 this post


Link to post
Share on other sites
5MinuteGaming    274
Check my edited post I think that it answers your question.

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 this post


Link to post
Share on other sites
Flawe    114
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 this post


Link to post
Share on other sites
5MinuteGaming    274
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 this post


Link to post
Share on other sites
Flawe    114
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 this post


Link to post
Share on other sites
Kippesoep    892
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.

Share this post


Link to post
Share on other sites
Flawe    114
Ahh, thanx!

I changed to using the numpad now and it seems to work. I hope that nobody else gets a problem with the numpad now instead :P

Share this post


Link to post
Share on other sites
Kippesoep    892
Don't depend on that. Almost everybody will have a problem. My point is: it's very unreliable. What works on one keyboard may not work on another. If you're making a game (this is GameDev, after all), make your key bindings configurable, so the user may work around this if he encounters this problem.

Share this post


Link to post
Share on other sites
Flawe    114
Yeah, you're right. I was kinda tired when I wrote that. But I think 'll leave those things for when the game is finished =)
Thanx again

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this