3

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.

FThompson
  • 28,352
  • 13
  • 60
  • 93

2 Answers2

1

This works fine for me

InputMap im = getInputMap();
ActionMap am = getActionMap();

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, false), "Press");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, true), "Release");

am.put("Press", new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent e) {

        if (!pressed) {

            pressed = true;
            System.out.println("press");

        }

    }

});

am.put("Release", new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent e) {

        if (pressed) {

            pressed = false;
            System.out.println("release");

        }

    }

});

Just to make sure I did this

am.put("Release", new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent e) {

        pressed = false;
        System.out.println("release");

    }

});

And it still worked

Printed press when I pressed and held 1 and then printed release when I released the key. That was the only output I got

UPDATED with Full Code

public class TestPane extends JPanel {

    private boolean pressed;

    public TestPane() {

        InputMap im = getInputMap();
        ActionMap am = getActionMap();

        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, false), "Press");
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, true), "Release");

        am.put("Press", new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {

                if (!pressed) {

                    pressed = true;
                    System.out.println("press");

                }

            }

        });

        am.put("Release", new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {

                if (pressed) {

                    pressed = false;
                    System.out.println("release");

                }

            }

        });

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • what platform do you use ? i use ubuntu 12.04 and netbeans and your code doesn't work with me – Mahmoud Hosny Aug 11 '12 at 21:15
  • Ahh, I'm using Windows :P (with Netbeans) - I'll have a look and see if my Mac does the same thing – MadProgrammer Aug 11 '12 at 21:19
  • @MahmoudHosny Tried both examples under MacOS X Lion without issue. Sorry, looks like an issue with Ubuntu. I was testing with JDK1.6.0_24 under Windows and JDK1.6.0_33 under Mac. I even tried JDK1.7.0 under Windows and they all work fine – MadProgrammer Aug 11 '12 at 21:38
  • Absolutely, thought I might need to make it `focusable`, but it worked just fine - I added the full code just in case – MadProgrammer Aug 11 '12 at 22:03
1

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

Community
  • 1
  • 1
Bobulous
  • 12,967
  • 4
  • 37
  • 68