0

我想知道如何在按键事件中按下所有按键。例如,我想为ctrl+编写一个f可以切换全屏的监听器。我如何检查是否在一个事件中同时按下ctrl 和? f


编辑1:

我尝试打印KeyEvent.getModifiersExText(e.getModifiersEx())和输入ctrl+ f,但结果是这样?

4

4 回答 4

3

老实说,KeyListener它有很多限制并且使用起来很麻烦(恕我直言),相反,我会简单地利用键绑定API,它通常为您提供更大的灵活性和重用的潜力。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class KeyListenerTest {

    public static void main(String[] args) {
        new KeyListenerTest();
    }

    public KeyListenerTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel lbl;
        private boolean fullScreen = false;

        public TestPane() {
            lbl = new JLabel("Normal");
            setLayout(new GridBagLayout());
            add(lbl);

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

            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_DOWN_MASK), "FullScreen");
            am.put("FullScreen", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (fullScreen) {
                        lbl.setText("Normal");
                    } else {
                        lbl.setText("Full Screen");
                    }
                    fullScreen = !fullScreen;
                }
            });

        }

    }

}

只是为了让你不认为我完全有偏见,这里有一个例子使用KeyListener......

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class KeyListenerTest {

    public static void main(String[] args) {
        new KeyListenerTest();
    }

    public KeyListenerTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel lbl;
        private boolean fullScreen = false;

        public TestPane() {
            lbl = new JLabel("Normal");
            setLayout(new GridBagLayout());
            add(lbl);

            setFocusable(true);
            addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    requestFocusInWindow();
                }

            });

            addKeyListener(new KeyAdapter() {

                @Override
                public void keyPressed(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_F && e.isControlDown()) {
                        if (fullScreen) {
                            lbl.setText("Normal");
                        } else {
                            lbl.setText("Full Screen");
                        }
                        fullScreen = !fullScreen;
                    }
                }

            });
        }
    }
}
于 2013-11-06T22:17:04.437 回答
2

这样的事情应该这样做:

public MyListener extends KeyListener {
    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if ((e.getKeyCode() == KeyEvent.VK_F) && ((e.getModifiers() & KeyEvent.CTRL_DOWN_MASK) != 0)) {
            System.out.println("Keys ctrl+F pressed!");
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }
});
于 2013-11-06T22:05:01.217 回答
0

如果您按下两个键,则会调用两次“keyPressed(KeyEvent e)”。您可以将按下的键存储在布尔数组中并检查它们是否都被按下。

private boolean control_pressed = false;

public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_CONTROL) {
      control_pressed = true;
    }

    if (e.getKeyCode() == KeyEvent.VK_F && control_pressed) {
        /* do something */
    }
}

public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_CONTROL) {
      control_pressed = false;
    }
}

释放必须再次释放控制变量。

于 2013-11-06T22:06:17.017 回答
0

我发现了一些很酷的方法可以在这种情况下使用:

e.isAltDown();

e.isAltGraphDown()

e.isControlDown()

e.isShiftDown()

于 2013-11-06T22:07:44.903 回答