do something once the key is held and repeat when its released and repressed

不羁岁月 提交于 2019-12-30 10:28:08

问题


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.


回答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");

                }

            }

        });

    }

}



回答2:


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

  • How to stop repeated keyPressed() / keyReleased() events in Swing
  • How to know when a user has really released a key in Java?


来源:https://stackoverflow.com/questions/11917377/do-something-once-the-key-is-held-and-repeat-when-its-released-and-repressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!