How to know when a user has really released a key in Java?

前端 未结 10 1319
孤城傲影
孤城傲影 2020-11-30 11:09

(Edited for clarity)

I want to detect when a user presses and releases a key in Java Swing, ignoring the keyboard auto repeat feature. I also would like a pure Java

10条回答
  •  萌比男神i
    2020-11-30 11:44

    You might want to use the action map of the component you are interested in. Here's an example that deals with a specific key (SPACE BAR) but I'm sure that if you read the documentation you may be able to modify it to handle generic key presses and releases.

    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.beans.PropertyChangeListener;
    
    import javax.swing.Action;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    
    public class Main {
        public static void main(String[] args) {
            JFrame f = new JFrame("Test");
            JPanel c = new JPanel();
    
            c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                    KeyStroke.getKeyStroke("SPACE"), "pressed");
            c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                    KeyStroke.getKeyStroke("released SPACE"), "released");
            c.getActionMap().put("pressed", new Action() {
                public void addPropertyChangeListener(
                        PropertyChangeListener listener) {
                }
    
                public Object getValue(String key) {
                    return null;
                }
    
                public boolean isEnabled() {
                    return true;
                }
    
                public void putValue(String key, Object value) {
                }
    
                public void removePropertyChangeListener(
                        PropertyChangeListener listener) {
                }
    
                public void setEnabled(boolean b) {
                }
    
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Pressed space at "+System.nanoTime());
                }
            });
            c.getActionMap().put("released", new Action() {
                public void addPropertyChangeListener(
                        PropertyChangeListener listener) {
                }
    
                public Object getValue(String key) {
                    return null;
                }
    
                public boolean isEnabled() {
                    return true;
                }
    
                public void putValue(String key, Object value) {
                }
    
                public void removePropertyChangeListener(
                        PropertyChangeListener listener) {
                }
    
                public void setEnabled(boolean b) {
                }
    
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Released space at "+System.nanoTime());
                }
            });
            c.setPreferredSize(new Dimension(200,200));
    
    
            f.getContentPane().add(c);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.pack();
            f.setVisible(true);
        }
    }
    

提交回复
热议问题