我想知道如何在键事件中按下所有键。例如,我想为 ctrl + f 编写一个侦听器来切换全屏。如何检查在一个事件中是否同时按下了 ctrl和f?
编辑 1:
我尝试打印KeyEvent.getModifiersExText(e.getModifiersEx())
并键入ctrl + f,但这只产生了?
.
老实说,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;
}
}
});
}
}
}
这样的事情应该这样做:
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) {
}
});
我发现了一些在这种情况下有效的很酷的方法:
e.isAltDown();
e.isAltGraphDown()
e.isControlDown()
e.isShiftDown()
如果按两个键,"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;
}
}
释放必须再次释放控制变量。