Java KeyBindings组合不起作用

我正在努力创造一种方法来确定组合。 在我甚至可以设置任何布尔值之前,我注意到某些组合键导致忽略其他键。

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作为推荐,以启用组合键。 但是,我面临的问题是, 一旦按下某个键并不是所有的键都被注册了 。 以我的示例,请尝试以下scheme:

启动示例并按住A键。 控制台将连续打印出“A has been pressed!”。 现在,在按住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); } } } } } 

Solutions Collecting From Web of "Java KeyBindings组合不起作用"