Java未检测键盘输入(字母)
我正在制作一个2d游戏引擎,并试图改进输入系统。当我尝试使用WASD键时,Java未检测键盘输入(字母),java,macos,swing,keyboard,key-bindings,Java,Macos,Swing,Keyboard,Key Bindings,我正在制作一个2d游戏引擎,并试图改进输入系统。当我尝试使用WASD键时,KeyListener有时不会检测到按键被按下,但仍会检测到按键被释放。我尝试使用KeyBindings,但是我遇到了同样的问题。有人知道发生了什么吗 (可能与我的IDE(netbeans)、mac或java有关?) 编辑:这里有其他人的代码也有同样的问题 import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Dimension; im
KeyListener
有时不会检测到按键被按下,但仍会检测到按键被释放。我尝试使用KeyBindings
,但是我遇到了同样的问题。有人知道发生了什么吗
(可能与我的IDE(netbeans)、mac或java有关?)
编辑:这里有其他人的代码也有同样的问题
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
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.setContentPane(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Surface surface;
public TestPane() {
setLayout(new BorderLayout());
surface = new Surface();
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();
System.out.println("yo");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "Pressed.left");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), "Release.left");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "Pressed.right");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "Release.right");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, false), "Pressed.up");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, true), "Release.up");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, false), "Pressed.down");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, true), "Release.down");
actionMap.put("Pressed.left", surface.getLeftPressAction());
actionMap.put("Release.left", surface.getLeftReleaseAction());
actionMap.put("Pressed.right", surface.getRightPressAction());
actionMap.put("Release.right", surface.getRightReleaseAction());
actionMap.put("Pressed.up", surface.getUpPressAction());
actionMap.put("Release.up", surface.getUpReleaseAction());
actionMap.put("Pressed.down", surface.getDownPressAction());
actionMap.put("Release.down", surface.getDownReleaseAction());
add(surface);
}
}
public class Surface extends Canvas {
private String displayText = "...";
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g.create();
FontMetrics fm = g2d.getFontMetrics();
int x = (getWidth() - fm.stringWidth(displayText)) / 2;
int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
g2d.drawString(displayText, x, y);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public Action getLeftPressAction() {
return new TextAction("Left");
}
public Action getLeftReleaseAction() {
return new ClearAction();
}
public Action getRightPressAction() {
return new TextAction("Right");
}
public Action getRightReleaseAction() {
return new ClearAction();
}
public Action getUpPressAction() {
return new TextAction("Up");
}
public Action getUpReleaseAction() {
return new ClearAction();
}
public Action getDownPressAction() {
return new TextAction("Down");
}
public Action getDownReleaseAction() {
return new ClearAction();
}
public class TextAction extends AbstractAction {
private String text;
public TextAction(String text) {
this.text = text;
}
@Override
public void actionPerformed(ActionEvent e) {
displayText = text;
repaint();
}
}
public class ClearAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
displayText = "...";
repaint();
}
}
}
}
我刚刚尝试在Windows10中使用jar文件,它工作得非常好。。。这可能与我的操作系统或我的java版本有关。“请帮助我调试我没有展示的代码”——这很难做到。请修复;请创建并发布您的有效代码。@hoverCraftfullOfels这不是我要问的…您的代码没有按预期运行--这是代码中的一个错误,您要问原因,这需要适当的代码,因此是的,这实际上就是您要问的。请重新查看链接,因为如果您的代码符合协议,我们将更容易找到解决方案。祝你好运。花10-20分钟写一个MCVE(一个新程序)。如果错误在你的MCVE中,那么发布它,我们可以提供帮助。如果错误不在MCVE中,那么您需要找出这两个程序之间的区别。要么你会发现错误,要么你会有一个程序,你可以张贴,不需要我们太长时间来分析。