I need to check when the user presses on a key and releases it. If a key is held in, I will do something once.
I used the keystroke to get the button I want to relate an action with. I used also a semaphore to overcome this problem but it looks crazy
My code: `
InputMap inputMap = jPanel1.getInputMap();
KeyStroke keyPressed = KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, false);
KeyStroke keyReleased = KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, true);
//------------------------------------------
jPanel1.getActionMap().put("_1000Press", _1000Press);
inputMap.put(keyPressed, "_1000Press");
jPanel1.getActionMap().put("_1000Release", _1000Release);
inputMap.put(keyReleased, "_1000Release");
public Action _1000Press = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if(!one){
// do some thing
one = true;
System.out.println(one);
}
}
};
public Action _1000Release = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
one = false;
System.out.println(one);
}
};
`
When I press "1" key and hold it, it prints: false true false true false true
Thanks in advance.
I don't believe you can achieve what you need in Linux, because the operating system continually issues press/release signals to the JVM.
See
This works fine for me
Just to make sure I did this
And it still worked
Printed
press
when I pressed and held 1 and then printedrelease
when I released the key. That was the only output I gotUPDATED with Full Code