KeyEvent keyPressed键组合被阻止

时间:2015-02-06 00:15:51

标签: java blocking keyevent blocked

我一直在努力解决以下问题已有几个小时了,无法找到解决方案。我目前正致力于音乐/节奏游戏,用户必须在合适的时间按某些键才能获得积分。

现在,由于它应该与弹钢琴类似,因此必须有键组合。游戏中目前有7个键(A,S,D,SPACE,J,K和L),除了K + L + ANY之外,它们的每个组合都能正常工作。

起初,我认为可能无法同时按下2个以上的按键,但A,S,D,SPACE,J和K同时没有问题,但是当按下L时,它根本没有响应(没有KeyEvent被激活)。

这似乎是许多其他关键组合的问题。我只发现Y,X,D,T,Z,O和M(欧洲键盘)是一个可以一次按下的工作7键组合。然而,对于玩家而言,这并不是一个舒适的组合。

以下是我的代码中的相关部分:

package question;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class Example extends Canvas implements KeyListener {

/**
 * 
 */
private static final long serialVersionUID = 1L;

public Example() {
    JFrame frame;       
    frame = new JFrame("KeyEvent problem");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(this, BorderLayout.CENTER);
    frame.pack();       
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    setFocusable(true);
    addKeyListener(this);
    requestFocusInWindow(); 
}

public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();
    if(keyCode == KeyEvent.VK_A) {
        //lines.get(0).setActive(true);
    }
    else if(keyCode == KeyEvent.VK_S) {
        //lines.get(1).setActive(true);
    }
    else if(keyCode == KeyEvent.VK_D) {
    //  lines.get(2).setActive(true);
    }
    else if(keyCode == KeyEvent.VK_SPACE) {
        //lines.get(3).setActive(true);
    }
    else if(keyCode == KeyEvent.VK_J) {
        //lines.get(4).setActive(true);
    }
    else if(keyCode == KeyEvent.VK_K) {
        //lines.get(5).setActive(true);
    }
    else if(keyCode == KeyEvent.VK_L) {
        //lines.get(6).setActive(true);
    }

    System.out.println("keycode: " + keyCode + " keyChar: " + e.getKeyChar());
}

public void keyReleased(KeyEvent e) {
    int keyCode = e.getKeyCode();
    if(keyCode == KeyEvent.VK_A) {
        //lines.get(0).setActive(false);
    }
    else if(keyCode == KeyEvent.VK_S) {
        //lines.get(1).setActive(false);
    }
    else if(keyCode == KeyEvent.VK_D) {
        //lines.get(2).setActive(false);
    }
    else if(keyCode == KeyEvent.VK_SPACE) {
        //lines.get(3).setActive(false);
    }
    else if(keyCode == KeyEvent.VK_J) {
        //lines.get(4).setActive(false);
    }
    else if(keyCode == KeyEvent.VK_K) {
        //lines.get(5).setActive(false);
    }
    else if(keyCode == KeyEvent.VK_L) {
        //lines.get(6).setActive(false);
    }       
}

public void keyTyped(KeyEvent arg0) {}

public static void main(String[] args) {
    Example example = new Example();
}

}

其中lines.get(index).setActive(boolean b)只是为游戏中键的某些图形表示设置了一个标志。但是,按下按钮时,您也可以在控制台中看到此信息。他们的keyCode和KeyChar是垃圾邮件,当按住每个键时,这个工作正常,最近按下的键在控制台中显示。但这不适用于J + K + L.

当按住A然后按A + S然后按A + S + D时,此示例的控制台将显示什么:

    keycode:65 keyChar:a

    keycode:65 keyChar:a

    keycode:65 keyChar:a

    keycode:83 keyChar:s

    keycode:83 keyChar:s

    keycode:83 keyChar:s

    keycode:68 keyChar:d

    keycode:68 keyChar:d

    keycode:68 keyChar:d

然后J然后J + K然后J + K + L导致以下结果:

    keycode:74 keyChar:j

    keycode:74 keyChar:j

    keycode:74 keyChar:j

    keycode:75 keyChar:k

    keycode:75 keyChar:k

    keycode:75 keyChar:k

(L缺失,尽管按下了它)

我有可能以某种方式改变这种情况吗?为什么会这样?我希望不必使用KeyBindings,因为我现在不使用JComponent,而是使用Canvas。

欢呼声。

1 个答案:

答案 0 :(得分:1)

这似乎是操作系统和/或键盘硬件的限制,这是我使用的测试代码,我一次只能按下六个键,无论组合如何。在Windows 7和MacOS X Yosemite上进行了测试

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Map<String, Boolean> mapKeys;

        public TestPane() {
            mapKeys = new HashMap<>();
            mapKeys.put("A", false);
            mapKeys.put("S", false);
            mapKeys.put("D", false);
            mapKeys.put(" ", false);
            mapKeys.put("J", false);
            mapKeys.put("K", false);
            mapKeys.put("L", false);

            bindKey(KeyEvent.VK_A, "A");
            bindKey(KeyEvent.VK_S, "S");
            bindKey(KeyEvent.VK_D, "D");
            bindKey(KeyEvent.VK_SPACE, " ");
            bindKey(KeyEvent.VK_J, "J");
            bindKey(KeyEvent.VK_K, "K");
            bindKey(KeyEvent.VK_L, "L");
        }

        protected void bindKey(int keyCode, String name) {

            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();

            im.put(KeyStroke.getKeyStroke(keyCode, 0, false), "pressed." + name);
            im.put(KeyStroke.getKeyStroke(keyCode, 0, true), "released." + name);

            am.put("pressed." + name, new KeyAction(name, true));
            am.put("released." + name, new KeyAction(name, false));

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            FontMetrics fm = g2d.getFontMetrics();
            int charWidth = fm.charWidth('M') + 2;
            int charHeight = fm.getHeight();
            int x = (getWidth() - ((charWidth + 2) * mapKeys.size())) / 2;
            int y = (getHeight() - charHeight) / 2;
            for (String name : mapKeys.keySet()) {
                boolean state = mapKeys.get(name);
                int xPos = x + ((charWidth - fm.stringWidth(name))) / 2;
                Rectangle bounds = new Rectangle(x, y, charWidth, charHeight);
                if (state) {
                    g2d.setColor(Color.RED);
                    g2d.fill(bounds);
                }
                g2d.setColor(Color.BLACK);
                g2d.draw(bounds);
                g2d.drawString(name, xPos, y + fm.getAscent());
                x += charWidth + 2;
            }
            g2d.dispose();
        }

        public class KeyAction extends AbstractAction {

            private String name;
            private boolean state;

            public KeyAction(String name, boolean state) {
                this.name = name;
                this.state = state;
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                mapKeys.put(name, state);
                repaint();
            }

        }

    }

}