0

I use ubuntu. The way ubuntu handles keyboard input is different from that of windows. In ubuntu, after a key pressed event is triggered, a key released event is triggered immediately after that, even if you do not release the key. These false key Released events are making it difficult for me to program games, especially programming the collision detection. No matter how perfect my code is, I am not able to solve the problem.

As a proof, if you just write a program which will output whether a key is pressed or release, you will see that the output in ubuntu will appear like this - keyPressed > keyReleased > keyPressed > keyReleased and so on even if you do not release the key. Whereas in windows - key Pressed > key Pressed > key Pressed> key Pressed and so on.

Do you know any way to deal with this?

Here is a simple code that will make my issue clear, if you run this on ubuntu:-

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Strange extends JPanel implements KeyListener{

    public Strange(){
        this.setPreferredSize(new Dimension(500, 500));

        addKeyListener(this);
        setFocusable(true);

        JFrame frame=new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(this);
        frame.pack();
        frame.setVisible(true);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_LEFT)
            System.out.println("left is pressed");
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_LEFT)
            System.out.println("left is released");
    }

    @Override
    public void keyTyped(KeyEvent e) {}

    public static void main(String[] args){
        new Strange();
    }
}
anonymous
  • 448
  • 1
  • 6
  • 25
  • which language are you using. Can you show some code. – adnan kamili May 20 '14 at 18:51
  • i use java. But the issue i have posted is a low level issue, not dependent on any programming language. – anonymous May 20 '14 at 19:00
  • @adnankamili okay i have posted some code to demonstrate my problem – anonymous May 20 '14 at 19:14
  • I actually use C++ and never faced this issue. It seems you are overriding the wrong handlers keypress is usually equivalent to keydown + keyup don't you have keyup and keydown events. – adnan kamili May 20 '14 at 19:19
  • @adnankamili you say in c++ you don't face this issue, how come? do you use ubuntu? – anonymous May 20 '14 at 19:23
  • yup I use Ubuntu and holds true for any Linux distro as kernel is same. You are most probably overriding wrong handlers please consider my previous comment. – adnan kamili May 20 '14 at 19:27
  • @adnankamili uh... i don't think so :D thanks for commenting. – anonymous May 20 '14 at 19:32
  • this is duplicate of http://stackoverflow.com/questions/1736828/how-to-stop-repeated-keypressed-keyreleased-events-in-swing or http://stackoverflow.com/questions/1457071/how-to-know-when-a-user-has-really-released-a-key-in-java BTW: my Linux (Arch) does not send keyReleased for VK_LEFT. It does however for "normal" keys. – close2 May 20 '14 at 21:06

1 Answers1

0

Try to use an update method with a game loop like this:

public class Strange extends JPanel implements KeyListener{

public Strange(){
    this.setPreferredSize(new Dimension(500, 500));

    JFrame frame=new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(this);
    frame.pack();
    frame.setVisible(true);

    while(true){
       update();

       try{
           Thread.sleep(10);
       }catch(Exception e){}
    }
}

public void update(){
    addKeyListener(this);
    setFocusable(true);
}

@Override
public void keyPressed(KeyEvent e) {
    if(e.getKeyCode()==KeyEvent.VK_LEFT)
        System.out.println("left is pressed");
}

@Override
public void keyReleased(KeyEvent e) {
    if(e.getKeyCode()==KeyEvent.VK_LEFT)
        System.out.println("left is released");
}

@Override
public void keyTyped(KeyEvent e) {}

public static void main(String[] args){
    new Strange();
}
}

I'm not on ubuntu, so i'm not sure that it works. But i hope.

Marcello Davi
  • 433
  • 4
  • 16