wood burning stoves*
The moose likes Java in General and the fly likes Java KeyListener - can't detect multiple pressed buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java KeyListener - can Watch "Java KeyListener - can New topic
Author

Java KeyListener - can't detect multiple pressed buttons

Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28
Hello JavaRanch, I hope my first topic got into the right section.
This is a short extract from my KeyListener method that is supposed to control a vechile in a game. The problem that I'm experiencing is when I press and hold down a few buttons, and then wants to press down another one, the keylistener doesn't detect that buttonpress!

For instance in this code, I press down and hold the Up and Left arrow keys on my keyboard and both moveUp and moveLeft is set to true. I want to keep hold down Up and Left because if I release them they will be set to false again in my keyReleased method. I now want to fire a shot from the vehicle while moving, but then when pressing Space while having the two arrow keys pressed down, case KeyEvent.VK_SPACE: is never executed.

After testing with other button combinations I noticed that the keylistener can handle for example Left Right Down simultaneously, but not Left Right Up. No matter what combination I try it can't handle more than three buttons at the same time this way.

After searching after solutions on this I found this problem discussed every here and there on the web. Apparently, some people on their computers are not having this problem. I have read some people saying that this might be a hardware issue that is out of the programmer's control and depends to computer to computer on how this method is going to perform.

But I have seen nobody mentioning any clear solutions or workarounds on this yet! Anyone got experience of this? I think I've heard about something called "helper listener" that's is used in java for handling multiple inputs, but I'm not sure. Can't find much when searching it.

Thanks in advance for input! //Kalle

PS: Here is a runnable program to test the issue I mentioned if you want to try it out. Try to press down and hold Up, Down and Space at the same time and see if you can get all values turned into true.



Code first, then think.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Welcome to the Ranch!

Believe it or not, this may have nothing to do with your program. In fact, your SSCCE works fine on my system. Instead, it may be caused by your keyboard. My previous keyboard had similar problems in games. I wanted to move diagonally, then press space: BEEP, and nothing. I had to release all buttons for it to respond again. I searched around and found out it's a limitation in the keyboard (Logitech Internet Navigator) - it couldn't handle more than 2 consecutive key presses (sad, I know...). In the end I replaced it with a so-called gaming keyboard (Logitech G11), and I haven't had the same problem ever since.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28
Oh I see. I'm using a rather unfancy keyboard, and I just tried pressing down multiple key buttons in other games to see if it was the same way there, and it was. I never noted it earlier though, must be because in most games you are using the mouse as well as it input source, then you often don't need more than 2-3 buttons pressed down at the same time.

So its getting a better keyboard or eating it up then?

PS: I noted when trying out, if I spread out the keys used for input on my keyboard, I'm able to press alot more keys at the same time. For instance, I can't press Up Down and Space as I mentioned earlier, but A S D UP Space is fine. Probably could be more then if you take keys that are on not the same circuit then.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Kalle Zander wrote:So its getting a better keyboard or eating it up then?

I'm afraid so. But with a little bit of money you can get a lot better keyboard. I love my G11; if I ever would replace it it would probably be another Logitech gaming keyboard (like the G110 or G19).

PS: I noted when trying out, if I spread out the keys used for input on my keyboard, I'm able to press alot more keys at the same time. For instance, I can't press Up Down and Space as I mentioned earlier, but A S D UP Space is fine. Probably could be more then if you take keys that are on not the same circuit then.

That's interesting. Seeing as I only had the problem with 2 arrow keys combined with space I never even tried if other keys had the same issue.
Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28
Rob Prime wrote:
Kalle Zander wrote:So its getting a better keyboard or eating it up then?

I'm afraid so. But with a little bit of money you can get a lot better keyboard. I love my G11; if I ever would replace it it would probably be another Logitech gaming keyboard (like the G110 or G19).

PS: I noted when trying out, if I spread out the keys used for input on my keyboard, I'm able to press alot more keys at the same time. For instance, I can't press Up Down and Space as I mentioned earlier, but A S D UP Space is fine. Probably could be more then if you take keys that are on not the same circuit then.

That's interesting. Seeing as I only had the problem with 2 arrow keys combined with space I never even tried if other keys had the same issue.


I see, but just so its clear that changing the input programming isn't helping, so you don't have to have your mind on that.

todo: buy a new keyboard

Thanks,

//Kalle
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java KeyListener - can't detect multiple pressed buttons
 
Similar Threads
Adding a KeyListener to a JLabel?
Multiple Key Press
Setting focus to a dialog button?
Problem with multiple keys pressed
how would I go about reading more than one key at a time?