gpt4 book ai didi

java - KeyEvent 监听器中的多个键

转载 作者:行者123 更新时间:2023-12-01 17:19:23 27 4
gpt4 key购买 nike

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

<小时/>

编辑1:

我尝试打印 KeyEvent.getModifiersExText(e.getModifiersEx()) 并输入 ctrl + f,但这只是产生了

最佳答案

说实话,KeyListener 有很多限制,使用起来很麻烦(恕我直言),相反,我会简单地利用key bindings API,通常为您提供更大的灵 active 和重用潜力。

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;
}
}

});
}
}
}

关于java - KeyEvent 监听器中的多个键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19823633/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com