javajframekeylistener

keyPressed() and keyReleased() not being called KeyListener


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<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); // 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<Character> 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 = "Game";
    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('d'))
            System.out.println("d pressed");
    }

Solution

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