After reading some tutorials I decided to switch from KeyListeners to KeyBindings. I think I have understood how they work, but I can't figure out why this little program detects a key release even if I don't release the key. EDIT: I am on a Linux machine.
//KeyBindingsTest.java
import javax.swing.*;
import java.awt.event.*;
public class KeyBindingsTest{
private JFrame w = new JFrame();
private JComponent j = new JPanel();
public KeyBindingsTest(){
w.setSize(400,400);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setVisible(true);
setKeyBindings();
w.add(j);
}
public static void main(String[] args){
new KeyBindingsTest();
}
private void setKeyBindings(){
InputMap im = j.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = j.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, false), "pressed");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, true), "released");
am.put("pressed", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Pressed");
}
});
am.put("released", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Released");
}
});
j.setFocusable(true);
j.requestFocusInWindow();
}
}
If I hold down the "1" key I expect the program to print continuously:
Pressed
Pressed
Pressed...
but it prints:
Pressed
Released
Pressed
Released...
I must be missing something, something very simple.
EDIT: now I've come up with a dirty dirty workaround, but I still hope someone will explain how to solve this in the appropriate way. Here it is:
// the beginning of the file should stay the same
private void setKeyBindings(){
InputMap im = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, false), "pressed");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, true), "released");
class MyAction extends AbstractAction{
private String s;
public MyAction(String s){
this.s = s;
}
@Override
public void actionPerformed(ActionEvent e) {
if(s.equals("pressed"))setKeyPressed();
else if(s.equals("released"))tryToSetKeyReleased();
}
}
am.put("pressed", new MyAction("pressed"));
am.put("released", new MyAction("released"));
setFocusable(true);
requestFocusInWindow();
}
public boolean pressed = false;
public void setKeyPressed(){System.out.println("key pressed");pressed =true;}
public void setKeyReleased(){System.out.println("key released");pressed = false;}
public void tryToSetKeyReleased(){
pressed = false;
(new Thread(
new Runnable(){
@Override
public void run() {
try{Thread.sleep(5);}
catch(Exception e){;}
if(pressed==false)setKeyReleased();
}
}
)).start();
}