即使我没有释放密钥,也会检测到KeyStroke版本

时间:2013-11-06 11:30:41

标签: java swing key-bindings keystrokes

在阅读了一些教程后,我决定从KeyListeners切换到KeyBindings。我想我已经理解它们是如何工作的,但我无法弄清楚为什么这个小程序检测到一个密钥释放,即使我没有释放密钥。编辑:我在Linux机器上。

    //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();      
    }
}

如果我按住“1”键,我希望程序能够连续打印:

Pressed 
Pressed 
Pressed... 

但它打印出来:

Pressed
Released
Pressed
Released...

我必须遗漏一些东西,非常简单。

编辑:现在我想出了一个肮脏的解决方法,但我仍然希望有人能解释如何以适当的方式解决这个问题。这是:

            // 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();
    }

1 个答案:

答案 0 :(得分:2)

这与操作系统有关。例如,如果您连续按下记事本中的字符,它将连续打印该字符。因此,它不断发送按下释放信号。