keyPressed()和keyReleased()未被调用KeyListener。

huangapple go评论79阅读模式
英文:

keyPressed() and keyReleased() not being called KeyListener

问题

以下是您提供的代码的中文翻译:

  1. 我试图创建一个记录所有按键按下的链表`keyPressed()``keyReleased()`方法没有被调用我已经尝试过`frame.setFocusable(true)`
  2. Window.java
  3. ```java
  4. public class Window {
  5. protected JFrame frame;
  6. private boolean clicked = false; // TODO: 鼠标输入
  7. private LinkedList<Character> keysDown = new LinkedList<>();
  8. public Window(String title, Dimension resolution, Canvas display) {
  9. frame = new JFrame(title);
  10. Dimension decoratedResolution = new Dimension(resolution.width + 16, resolution.height + 38); // 窗口的分辨率不匹配,这样可以确保resolution.height实际上是屏幕底部而不是稍微下面,resolution.width同理。
  11. frame.setPreferredSize(decoratedResolution);
  12. frame.setMinimumSize(decoratedResolution);
  13. frame.setMaximumSize(decoratedResolution);
  14. frame.setResizable(false);
  15. frame.setLocationRelativeTo(null); // 居中窗口
  16. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17. frame.setFocusable(true);
  18. frame.addKeyListener(new KeyListener() {
  19. @Override
  20. public void keyTyped(KeyEvent e) {}
  21. @Override
  22. public void keyPressed(KeyEvent e) {
  23. char character = e.getKeyChar();
  24. if (!keysDown.contains(character))
  25. keysDown.add(character);
  26. }
  27. @Override
  28. public void keyReleased(KeyEvent e) {
  29. char character = e.getKeyChar();
  30. keysDown.remove((Object) character);
  31. }
  32. });
  33. frame.add(display);
  34. frame.setVisible(true);
  35. }
  36. public boolean isMouseDown() {
  37. return clicked;
  38. }
  39. public Vector2 getMousePosition() {
  40. Point point = MouseInfo.getPointerInfo().getLocation();
  41. SwingUtilities.convertPointFromScreen(point, frame);
  42. return new Vector2(point);
  43. }
  44. public LinkedList<Character> getKeysDown() {
  45. return keysDown;
  46. }
  47. }

Game.java

  1. public class Game extends Canvas implements Runnable {
  2. // 通用设置
  3. public static final String TITLE = "游戏";
  4. public static final Dimension RESOLUTION = new Dimension(800, 450);
  5. public static final int FPS = 60;
  6. // 请不要修改
  7. protected static final long NANO_SECOND = 1000000000;
  8. public float timeScale = 1f;
  9. public volatile boolean running = false; // 设置为false以停止游戏
  10. public Thread thread;
  11. public Window window;
  12. public Renderer renderer;
  13. public static void main(String[] args) {
  14. new Game();
  15. }
  16. public Game() {
  17. start();
  18. }
  19. public void start() {
  20. init();
  21. }
  22. public void init() {
  23. window = new Window(TITLE, RESOLUTION, this);
  24. }
  25. public void run() {
  26. final float RENDER_INTERVAL = 1f / FPS;
  27. long then = System.nanoTime();
  28. long now = then;
  29. float deltaTime = 0f;
  30. int frames = 0;
  31. while (running) {
  32. now = System.nanoTime();
  33. deltaTime = (float)(now - then) / NANO_SECOND;
  34. update(deltaTime * timeScale);
  35. if (!running)
  36. break;
  37. then = now;
  38. Thread.onSpinWait();
  39. }
  40. }
  41. public void update(float deltaTime) {
  42. if (window.getKeysDown().contains('d'))
  43. System.out.println("按下了 'd' 键");
  44. }
  45. }

希望这能帮助您理解代码的内容。如果您有任何进一步的问题,请随时提出。

英文:

I am trying to create a LinkedList of all the keys pressed, but the keyPressed() and keyReleased() methods aren't being called. I've already tried frame.setFocusable(true).
Here is my code (also sorry if's a big pile of spaghetti):

Window.java

  1. public class Window {
  2. protected JFrame frame;
  3. private boolean clicked = false; // TODO: Mouse input
  4. private LinkedList&lt;Character&gt; keysDown = new LinkedList&lt;&gt;();
  5. public Window(String title, Dimension resolution, Canvas display) {
  6. frame = new JFrame(title);
  7. Dimension decoratedResolution = new Dimension(resolution.width + 16, resolution.height + 38); // The resolution of the window is mismatched, this makes it so resolution.height is actually the bottom of the screen and not a little further below and the same for resolution.width.
  8. frame.setPreferredSize(decoratedResolution);
  9. frame.setMinimumSize(decoratedResolution);
  10. frame.setMaximumSize(decoratedResolution);
  11. frame.setResizable(false);
  12. frame.setLocationRelativeTo(null); // Center the window
  13. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14. frame.setFocusable(true);
  15. frame.addKeyListener(new KeyListener() {
  16. @Override
  17. public void keyTyped(KeyEvent e) {}
  18. @Override
  19. public void keyPressed(KeyEvent e) {
  20. char character = e.getKeyChar();
  21. if (!keysDown.contains(character))
  22. keysDown.add(character);
  23. }
  24. @Override
  25. public void keyReleased(KeyEvent e) {
  26. char character = e.getKeyChar();
  27. keysDown.remove((Object)character);
  28. }
  29. });
  30. frame.add(display);
  31. frame.setVisible(true);
  32. }
  33. public boolean isMouseDown() {
  34. return clicked;
  35. }
  36. public Vector2 getMousePosition() {
  37. Point point = MouseInfo.getPointerInfo().getLocation();
  38. SwingUtilities.convertPointFromScreen(point, frame);
  39. return new Vector2(point);
  40. }
  41. public LinkedList&lt;Character&gt; getKeysDown() {
  42. return keysDown;
  43. }
  44. }

Edit: All the other code relevant is below

P.S. This is one of my first times using stack overflow so If I made a mistake in formatting the question, let me know.

Game.java

  1. public class Game extends Canvas implements Runnable {
  2. // General Settings
  3. public static final String TITLE = &quot;Game&quot;;
  4. public static final Dimension RESOLUTION = new Dimension(800, 450);
  5. public static final int FPS = 60;
  6. // Do not modify
  7. protected static final long NANO_SECOND = 1000000000;
  8. public float timeScale = 1f;
  9. public volatile boolean running = false; // Set to false to stop the game
  10. public Thread thread;
  11. public Window window;
  12. public Renderer renderer;
  13. public static void main(String[] args) {
  14. new Game();
  15. }
  16. public Game() {
  17. start();
  18. }
  19. public void start() {
  20. init();
  21. }
  22. public void init() {
  23. window = new Window(TITLE, RESOLUTION, this);
  24. }
  25. public void run() {
  26. final float RENDER_INTERVAL = 1f / FPS;
  27. long then = System.nanoTime();
  28. long now = then;
  29. float deltaTime = 0f;
  30. int frames = 0;
  31. while (running) {
  32. now = System.nanoTime();
  33. deltaTime = (float)(now - then) / NANO_SECOND;
  34. update(deltaTime * timeScale);
  35. if (!running)
  36. break;
  37. then = now;
  38. Thread.onSpinWait();
  39. }
  40. }
  41. public void update(float deltaTime) {
  42. if (window.getKeysDown().contains(&#39;d&#39;))
  43. System.out.println(&quot;d pressed&quot;);
  44. }

答案1

得分: 0

It turns out I had to call addKeyListener() in the Game class.

英文:

It turns out I had to call addKeyListener() in the Game class.

huangapple
  • 本文由 发表于 2023年2月8日 19:52:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定