Java平台游戏KeyPressed/KeyReleased无响应问题求助
Hey there! Let's dig into why your key inputs aren't registering in your platformer. I've looked over your code and spotted a couple of critical issues that are almost certainly causing the problem:
1. Wrong Key Code Comparison
Your keyPressed and keyReleased methods are using character literals like 'e' to compare against e.getKeyCode()—but that's not how Java's KeyEvent works!
getKeyCode() returns virtual key constants (like KeyEvent.VK_E), not the ASCII value of the character. Using 'e' here will only match if you're checking getKeyChar() instead. Since you want to detect physical key presses (regardless of caps lock state), you need to use the VK constants.
Fixed Key Listener Methods:
@Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_E) { ePressed = true; } if (e.getKeyCode() == KeyEvent.VK_H) { hPressed = true; } } @Override public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_E) { ePressed = false; } if (e.getKeyCode() == KeyEvent.VK_H) { hPressed = false; } }
2. Missing Focus Request
Even though you set setFocusable(true) on your panel, it doesn't guarantee the panel actually has keyboard focus when the game starts. Without focus, KeyListener won't receive any events at all.
Add this line at the end of your initBoard() method (or your constructor) to force the panel to grab focus:
requestFocusInWindow();
3. Bonus: Switch to Key Bindings for More Reliability
KeyListener is notoriously finicky about focus—if any other component in your window steals focus, your key inputs stop working. A better long-term solution is to use Key Bindings, which are part of Swing's input map system and work regardless of which component has focus (as long as the window is active).
Here's how you could set up bindings for your 'E' and 'H' keys:
// In your ReflexPanel constructor or initBoard() method // For pressing 'E' getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_E, 0), "pressE"); getActionMap().put("pressE", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { ePressed = true; } }); // For releasing 'E' getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_E, 0, true), "releaseE"); getActionMap().put("releaseE", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { ePressed = false; } }); // Repeat the same pattern for 'H' getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_H, 0), "pressH"); getActionMap().put("pressH", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { hPressed = true; } }); getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_H, 0, true), "releaseH"); getActionMap().put("releaseH", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { hPressed = false; } });
With this setup, you can even remove the KeyListener entirely—no more focus headaches!
Start with fixing the key code comparison and adding the focus request first; that should get your keys working immediately. If you run into more focus-related issues down the line, switching to Key Bindings will make your input handling much more robust.
内容的提问来源于stack exchange,提问作者Tom Mitchell

