Jump to content

Java KeyListener - can't detect multiple pressed buttons

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Cander

Cander

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts
public void keyPressed(KeyEvent e)
{
     switch (e.getKeyCode())
     {
         case KeyEvent.VK_UP:
            moveUp = true;
             break;
         case KeyEvent.VK_LEFT:
             moveLeft = true;
             break;
         case KeyEvent.VK_SPACE:
             fire();
             break;
         //etc...
     }
}
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 key 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! //Cander


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.

import javax.swing.*;
import java.awt.event.*;

public class Main extends JPanel implements KeyListener, Runnable {
    boolean isUpPressed, isDownPressed, isSpacePressed;
    static JFrame f;

    public static void main(String[] args) {
        f = new JFrame();
        f.setSize(600,300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(new Main());
        f.setVisible(true);
    }

    public Main() {
        setFocusable(true);
        addKeyListener(this);
        new Thread(this).start();
    }

    public void keyTyped(KeyEvent ke) {
    }

    public void keyPressed(KeyEvent ke) {
        switch(ke.getKeyCode()) {
            case KeyEvent.VK_UP: isUpPressed = true; break;
            case KeyEvent.VK_DOWN: isDownPressed = true; break;
            case KeyEvent.VK_SPACE: isSpacePressed = true; break;
        }
    }

    public void keyReleased(KeyEvent ke) {
        switch(ke.getKeyCode()) {
            case KeyEvent.VK_UP: isUpPressed = false; break;
            case KeyEvent.VK_DOWN: isDownPressed = false; break;
            case KeyEvent.VK_SPACE: isSpacePressed = false; break;
        }
}

    public void run() {
        while(true) {
            try {
                String s = "up pressed: " + isUpPressed + ", down pressed: " + isDownPressed +", spacePressed: " + isSpacePressed;
                f.setTitle(s);
                Thread.sleep(200);
            } catch(Exception exc) {
                exc.printStackTrace();
                break;
            }
        }
    }
}


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I didn't really test it, but i know that my computer starts beeping when I hold down 3 buttons for too long (~3secs).

#3
farrell2k

farrell2k

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts
Your problem is a hardware limitation. Use WASD instead. You may have better luck.

#4
Cander

Cander

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts
Yeah I eventually found out that it was a hardware issue. And yes, it worked better using other keys than the arrow keys on my keyboard. I heard if you have a more tech keyboard like a nice Logitech gaming keyboard you don't get this problem. Must be the cheap design of the circuits in this old keyboard I'm using causing this. :P




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users