我正在努力创造一种识别关键组合的方法。在我甚至可以设置任何布尔值之前,我注意到某些键组合导致忽略其他键。
package question;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class Example extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
public Example() {
setBackground(Color.DARK_GRAY);
setPreferredSize(new Dimension(500,500));
setFocusable(true);
requestFocusInWindow();
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "action_a");
getActionMap().put("action_a", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("A has been pressed!");
}
});
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "action_s");
getActionMap().put("action_s", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("S has been pressed!");
}
});
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "action_d");
getActionMap().put("action_d", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("D has been pressed!"); // This code is reached, even when you are holding down the A key and S key
}
});getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_J, 0), "action_j");
getActionMap().put("action_j", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("J has been pressed!");
}
});
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_K, 0), "action_k");
getActionMap().put("action_k", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("K has been pressed!");
}
});
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_L, 0), "action_l");
getActionMap().put("action_l", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("L has been pressed!"); // This code is never reached when you hold down the J key and K key
}
});
JFrame frame;
frame = new JFrame("KeyBinding problem");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this, BorderLayout.CENTER);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
Example example = new Example();
}
}
我现在已经按照建议使用KeyBindings来启用组合键。但是,我遇到的问题是一次按下某些键时并非所有键都被注册。在我的示例中,请尝试以下方案:
启动示例并按住A键。控制台将不断打印出“A已被按下!”。现在,在按住A键的同时,另外添加S键并按住它,以及。控制台现在将停止打印“A已被按下!”并打印出“S已被按下!”反而是。现在按住A键,S键和D键。这将导致控制台不断打印出“D已被按下!”。
现在,重新启动所有内容,但不使用A,S和D键,而是使用J,K和L键。 这只适用于J和K,L键将被忽略。
- >顺便说一句,我正在使用 Windows 8.1(64位)。使用Aspire E1-772G笔记本电脑。
有人可以告诉我为什么会这样,以及我如何解决这个问题甚至解决这个问题?提前谢谢!
欢呼声。
*修改 我添加了另一个例子,它也直观地显示了问题。我不可能让所有标签立刻变成红色。 J + K + L的组合对我来说似乎有问题。 这与一次按下的键数量无关,因为它在我的主程序中用于6个键,以及。它只是J + K的组合,似乎停止为L键触发KeyEvents。 KeyListeners似乎也会出现这种情况,如下例所示。
package question;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.BitSet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Example extends JPanel implements KeyListener, Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
private BitSet bitSet;
private Thread thread;
private JLabel a_key, s_key, d_key, j_key, k_key, l_key;
public Example() {
setBackground(Color.DARK_GRAY);
setPreferredSize(new Dimension(700,300));
addKeyListener(this);
setFocusable(true);
requestFocusInWindow();
bitSet = new BitSet(256);
thread = new Thread(this);
a_key = new JLabel("A");
a_key.setBackground(Color.YELLOW);
a_key.setPreferredSize(new Dimension(50,50));
a_key.setOpaque(true);
s_key = new JLabel("S");
s_key.setBackground(Color.YELLOW);
s_key.setPreferredSize(new Dimension(50,50));
s_key.setOpaque(true);
d_key = new JLabel("D");
d_key.setBackground(Color.YELLOW);
d_key.setPreferredSize(new Dimension(50,50));
d_key.setOpaque(true);
j_key = new JLabel("J");
j_key.setBackground(Color.YELLOW);
j_key.setPreferredSize(new Dimension(50,50));
j_key.setOpaque(true);
k_key = new JLabel("K");
k_key.setBackground(Color.YELLOW);
k_key.setPreferredSize(new Dimension(50,50));
k_key.setOpaque(true);
l_key = new JLabel("L");
l_key.setBackground(Color.YELLOW);
l_key.setPreferredSize(new Dimension(50,50));
l_key.setOpaque(true);
add(a_key);
add(s_key);
add(d_key);
add(j_key);
add(k_key);
add(l_key);
JFrame frame;
frame = new JFrame("Key Event problem");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this, BorderLayout.CENTER);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
thread.start();
}
public static void main(String[] args) {
Example example = new Example();
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(bitSet.get(keyCode) == false) {
bitSet.set(keyCode);
}
}
@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
bitSet.clear(keyCode);
}
@Override
public void keyTyped(KeyEvent e) {
}
private boolean isKeyPressed(int keyCode) {
return bitSet.get(keyCode);
}
@Override
public void run() {
while(true) {
if(isKeyPressed(KeyEvent.VK_A)) {
a_key.setBackground(Color.red);
}
else {
if(a_key.getBackground() == Color.red) {
a_key.setBackground(Color.yellow);
}
}
if(isKeyPressed(KeyEvent.VK_S)) {
s_key.setBackground(Color.red);
}
else {
if(s_key.getBackground() == Color.red) {
s_key.setBackground(Color.yellow);
}
}
if(isKeyPressed(KeyEvent.VK_D)) {
d_key.setBackground(Color.red);
}
else {
if(d_key.getBackground() == Color.red) {
d_key.setBackground(Color.yellow);
}
}
if(isKeyPressed(KeyEvent.VK_J)) {
j_key.setBackground(Color.red);
}
else {
if(j_key.getBackground() == Color.red) {
j_key.setBackground(Color.yellow);
}
}
if(isKeyPressed(KeyEvent.VK_K)) {
k_key.setBackground(Color.red);
}
else {
if(k_key.getBackground() == Color.red) {
k_key.setBackground(Color.yellow);
}
}
if(isKeyPressed(KeyEvent.VK_L)) {
l_key.setBackground(Color.red);
}
else {
if(l_key.getBackground() == Color.red) {
l_key.setBackground(Color.yellow);
}
}
}
}
}
答案 0 :(得分:5)
这可能与密钥翻转有关:https://en.wikipedia.org/wiki/Rollover_(key) 某些键盘提供与WASD键的特殊连接,因为它们在游戏中经常使用。