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

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

keyPressed() and keyReleased() not being called KeyListener

问题

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

我试图创建一个记录所有按键按下的链表`keyPressed()``keyReleased()`方法没有被调用我已经尝试过`frame.setFocusable(true)`。

Window.java
```java
public class Window {
    protected JFrame frame;

    private boolean clicked = false; // TODO: 鼠标输入
    private LinkedList<Character> keysDown = new LinkedList<>();

    public Window(String title, Dimension resolution, Canvas display) {
        frame = new JFrame(title);
        Dimension decoratedResolution = new Dimension(resolution.width + 16, resolution.height + 38); // 窗口的分辨率不匹配,这样可以确保resolution.height实际上是屏幕底部而不是稍微下面,resolution.width同理。

        frame.setPreferredSize(decoratedResolution);
        frame.setMinimumSize(decoratedResolution);
        frame.setMaximumSize(decoratedResolution);
        frame.setResizable(false);

        frame.setLocationRelativeTo(null); // 居中窗口
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setFocusable(true);

        frame.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {}

            @Override
            public void keyPressed(KeyEvent e) {
                char character = e.getKeyChar();

                if (!keysDown.contains(character))
                    keysDown.add(character);
            }

            @Override
            public void keyReleased(KeyEvent e) {
                char character = e.getKeyChar();

                keysDown.remove((Object) character);
            }
        });

        frame.add(display);
        frame.setVisible(true);
    }

    public boolean isMouseDown() {
        return clicked;
    }

    public Vector2 getMousePosition() {
        Point point = MouseInfo.getPointerInfo().getLocation();
        SwingUtilities.convertPointFromScreen(point, frame);

        return new Vector2(point);
    }

    public LinkedList<Character> getKeysDown() {
        return keysDown;
    }
}

Game.java

public class Game extends Canvas implements Runnable {
    // 通用设置
    public static final String TITLE = "游戏";
    public static final Dimension RESOLUTION = new Dimension(800, 450);
    public static final int FPS = 60;

    // 请不要修改
    protected static final long NANO_SECOND = 1000000000;

    public float timeScale = 1f;
    public volatile boolean running = false; // 设置为false以停止游戏

    public Thread thread;
    public Window window;
    public Renderer renderer;

    public static void main(String[] args) {
        new Game();
    }

    public Game() {
        start();
    }

    public void start() {
        init();
    }

    public void init() {
        window = new Window(TITLE, RESOLUTION, this);
    }

    public void run() {
        final float RENDER_INTERVAL = 1f / FPS;

        long then = System.nanoTime();
        long now = then;

        float deltaTime = 0f;

        int frames = 0;

        while (running) {
            now = System.nanoTime();
            deltaTime = (float)(now - then) / NANO_SECOND;

            update(deltaTime * timeScale);

            if (!running)
                break;

            then = now;
            Thread.onSpinWait();
        }
    }

    public void update(float deltaTime) {
        if (window.getKeysDown().contains('d'))
            System.out.println("按下了 'd' 键");
    }
}

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

英文:

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

public class Window {
protected JFrame frame;
private boolean clicked = false; // TODO: Mouse input
private LinkedList&lt;Character&gt; keysDown = new LinkedList&lt;&gt;();
public Window(String title, Dimension resolution, Canvas display) {
frame = new JFrame(title);
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.
frame.setPreferredSize(decoratedResolution);
frame.setMinimumSize(decoratedResolution);
frame.setMaximumSize(decoratedResolution);
frame.setResizable(false);
frame.setLocationRelativeTo(null); // Center the window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setFocusable(true);
frame.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
char character = e.getKeyChar();
if (!keysDown.contains(character))
keysDown.add(character);
}
@Override
public void keyReleased(KeyEvent e) {
char character = e.getKeyChar();
keysDown.remove((Object)character);
}
});
frame.add(display);
frame.setVisible(true);
}
public boolean isMouseDown() {
return clicked;
}
public Vector2 getMousePosition() {
Point point = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(point, frame);
return new Vector2(point);
}
public LinkedList&lt;Character&gt; getKeysDown() {
return keysDown;
}
}

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

public class Game extends Canvas implements Runnable {
// General Settings
public static final String TITLE = &quot;Game&quot;;
public static final Dimension RESOLUTION = new Dimension(800, 450);
public static final int FPS = 60;
// Do not modify
protected static final long NANO_SECOND = 1000000000;
public float timeScale = 1f;
public volatile boolean running = false; // Set to false to stop the game
public Thread thread;
public Window window;
public Renderer renderer;
public static void main(String[] args) {
new Game();
}
public Game() {
start();
}
public void start() {
init();
}
public void init() {
window = new Window(TITLE, RESOLUTION, this);
}
public void run() {
final float RENDER_INTERVAL = 1f / FPS;
long then = System.nanoTime();
long now = then;
float deltaTime = 0f;
int frames = 0;
while (running) {
now = System.nanoTime();
deltaTime = (float)(now - then) / NANO_SECOND;
update(deltaTime * timeScale);
if (!running)
break;
then = now;
Thread.onSpinWait();
}
}
public void update(float deltaTime) {
if (window.getKeysDown().contains(&#39;d&#39;))
System.out.println(&quot;d pressed&quot;);
}

答案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:

确定