Java KeyBindings 组合不起作用
Java KeyBindings Combination does not work
我正在努力创建一种识别组合键的方法。在我什至可以设置任何布尔值之前,我注意到某些组合键会导致忽略其他键。
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 has been pressed!"。现在,在按住 A 键的同时,另外添加 S 键并按住它。控制台现在将停止打印 "A has been pressed!" 并连续打印 "S has been pressed!" 。现在按住 A 键、S 键和 D 键。这将导致控制台不断打印出 "D has been pressed!".
现在,重新启动一切,但不要使用 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);
}
}
}
}
}
可能这与密钥翻转有关:https://en.wikipedia.org/wiki/Rollover_(key)
有些键盘与 WASD 键有特殊连接,因为它们在游戏中经常使用。
我正在努力创建一种识别组合键的方法。在我什至可以设置任何布尔值之前,我注意到某些组合键会导致忽略其他键。
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 has been pressed!"。现在,在按住 A 键的同时,另外添加 S 键并按住它。控制台现在将停止打印 "A has been pressed!" 并连续打印 "S has been pressed!" 。现在按住 A 键、S 键和 D 键。这将导致控制台不断打印出 "D has been pressed!".
现在,重新启动一切,但不要使用 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);
}
}
}
}
}
可能这与密钥翻转有关:https://en.wikipedia.org/wiki/Rollover_(key) 有些键盘与 WASD 键有特殊连接,因为它们在游戏中经常使用。