我试图处理最多五个并发的keyPressed事件。
我已尝试分别侦听keyPressed和keyReleased事件以及从ArrayList添加/删除keyCodes。这适用于两个键,有时三个,但不止于此,并且在当前按下的键发生keyReleased事件之前不再注册keyPressed事件:
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
public class KeyboardHandler implements KeyListener {
public static ArrayList<Integer> keys = new ArrayList<Integer>();
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
// System.out.println("Key pressed: " + e.getKeyCode());
if (!keys.contains((Integer) e.getKeyCode())) {
keys.add((Integer) e.getKeyCode());
// System.out.println("Key added to keylist: " + e.getKeyCode());
}
}
public void keyReleased(KeyEvent e) {
// System.out.println("Key released: " + e.getKeyCode());
if (keys.contains((Integer) e.getKeyCode())) {
keys.remove((Integer) e.getKeyCode());
// System.out.println("Key removed from keylist: " + e.getKeyCode());
}
}
public static void handleKeyInput() {
// I then go on to handle the items in the array list appropriately
//using this method that is called by a thread.
我得出的结论是,这与使用arraylist无关,因此最终查看了键绑定。据我所知,关键绑定是处理关键事件的更好方法。但是,我的问题仍然存在,在释放当前按下的键之前,按下的键不会超过三个:
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
public class KeyBinding extends JComponent{
public KeyBinding(){
InputMap im = this.getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, false), "w_pressed");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, true), "w_released");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "a_pressed");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), "a_released");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, false), "s_pressed");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, true), "s_released");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "d_pressed");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "d_released");
am.put("w_pressed", new AbstractAction(){
public void actionPerformed(ActionEvent e) {
System.out.println("W key is pressed");
}
});
am.put("w_released", new AbstractAction(){
public void actionPerformed(ActionEvent e) {
System.out.println("W key is released");
}
});
am.put("a_pressed", new AbstractAction(){
public void actionPerformed(ActionEvent e) {
System.out.println("A key is pressed");
}
});
am.put("a_released", new AbstractAction(){
public void actionPerformed(ActionEvent e) {
System.out.println("A key is released");
}
});
am.put("s_pressed", new AbstractAction(){
public void actionPerformed(ActionEvent e) {
System.out.println("S key is pressed");
}
});
am.put("s_released", new AbstractAction(){
public void actionPerformed(ActionEvent e) {
System.out.println("S key is released");
}
});
am.put("d_pressed", new AbstractAction(){
public void actionPerformed(ActionEvent e) {
System.out.println("D key is pressed");
}
});
am.put("d_released", new AbstractAction(){
public void actionPerformed(ActionEvent e) {
System.out.println(" key is released");
}
});
}
}
我在Mac上运行Eclipse,可能是某些特定于操作系统的错误,还是我做错了什么?我只学习了几个星期的Java,这让我很难过。我讨厌询问非特定问题,只是发布代码块 - 我似乎很懒,但我已经尝试过但未能自己解决这个问题 - 所有的帮助都得到了大量的赞赏。