我已经为以下问题苦苦挣扎了好几个小时,却找不到解决方案。我目前正在开发音乐/节奏游戏,其中用户必须在正确的时间按下某些键才能得分。
现在,由于它应该类似于弹钢琴,因此也必须有组合键。游戏中目前有7个键(A,S,D,SPACE,J,K和L),除了K + L + ANY以外,它们的每种组合都可以正常工作。
起初,我认为可能无法一次按下两个以上的键,但是同时按下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,此示例的控制台将显示的内容:
关键码:65 keyChar:a
关键码:65 keyChar:a
关键码:65 keyChar:a
关键码:83 keyChar:s
关键码:83 keyChar:s
关键码:83 keyChar:s
键码:68 keyChar:d
键码:68 keyChar:d
键码:68 keyChar:d
而J然后J + K然后J + K + L导致以下结果:
键码:74 keyChar:j
键码:74 keyChar:j
键码:74 keyChar:j
关键码:75 keyChar:k
关键码:75 keyChar:k
关键码:75 keyChar:k
(即使按了,L也丢失了)
我是否有可能以某种方式更改此设置?为什么会这样呢?我希望不必使用KeyBindings,因为我现在不使用JComponent,而是使用Canvas。
干杯。
最佳答案
这似乎是操作系统和/或键盘硬件的限制,这是我使用的测试代码,无论组合如何,一次只能按下六个键。已在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();
}
}
}
}
关于java - KeyEvent keyPressed组合键被阻止,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28356494/