glfwGetKey always returns 0 (C++/Lua)

I’m making a little game framework in C++ with full Lua scripting using OpenGL, GLFW and GLEW and it seems that input is not being proccessed correctly by GLFW?

#include <iostream>
#include <cstring> 
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <lua.hpp>

class Framework {
public:
    Framework() : L(luaL_newstate()) {
        luaL_openlibs(L);
        registerLuaAPI();

        // Initialize GLFW
        if (!glfwInit()) {
            std::cerr << "Failed to initialize GLFW\n";
            exit(1);
        }

        // Create window
        window = glfwCreateWindow(800, 600, "Lua OpenGL Game", nullptr, nullptr);
        if (!window) {
            std::cerr << "Failed to create GLFW window\n";
            glfwTerminate();
            exit(1);
        }
        glfwMakeContextCurrent(window);

        // Initialize GLEW
        glewExperimental = GL_TRUE;
        if (glewInit() != GLEW_OK) {
            std::cerr << "Failed to initialize GLEW\n";
            glfwTerminate();
            exit(1);
        }

        // Set viewport
        glViewport(0, 0, 800, 600);
    }

    ~Framework() {
        glfwDestroyWindow(window);
        glfwTerminate();
        lua_close(L);
    }
    
    void run() {
        loadLuaScript("scripts/main.lua");
    
        double lastTime = glfwGetTime();
        while (!glfwWindowShouldClose(window)) {
            double currentTime = glfwGetTime();
            float dt = static_cast<float>(currentTime - lastTime);
            lastTime = currentTime;

            glfwPollEvents();
            update(dt);
            render();
    
            glfwSwapBuffers(window);
        }
    }

private:
    lua_State* L;
    GLFWwindow* window;

    void loadLuaScript(const std::string& filename) {
        if (luaL_dofile(L, filename.c_str()) != LUA_OK) {
            std::cerr << "Lua Error: " << lua_tostring(L, -1) << std::endl;
            lua_pop(L, 1);
        }
    }

    void update(float dt) {
        lua_getglobal(L, "update");
        if (lua_isfunction(L, -1)) {
            lua_pushnumber(L, dt);
            if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
                std::cerr << "Update Error: " << lua_tostring(L, -1) << std::endl;
                lua_pop(L, 1);
            }
        } else {
            lua_pop(L, 1);
        }
    }

    void render() {}

    void registerLuaAPI(); // Defined below
};

static int l_isKeyDown(lua_State* L) {
    GLFWwindow* window = static_cast<GLFWwindow*>(lua_touserdata(L, lua_upvalueindex(1)));
    const char* key = luaL_checkstring(L, 1);
    
    int keyCode = GLFW_KEY_UNKNOWN;
    std::string k = key;
    if (k == "w") keyCode = GLFW_KEY_W;
    else if (k == "a") keyCode = GLFW_KEY_A;
    else if (k == "s") keyCode = GLFW_KEY_S;
    else if (k == "d") keyCode = GLFW_KEY_D;

    std::cout << "Key: " << key << " mapped to code: " << keyCode << "\n";  // Debug output
    
    bool isDown = (keyCode != GLFW_KEY_UNKNOWN) && 
                  (glfwGetKey(window, keyCode) == GLFW_PRESS);
    std::cout << "Key state: " << glfwGetKey(window, keyCode) << "\n";  // Debug state
    
    lua_pushboolean(L, isDown);
    return 1;
}

void Framework::registerLuaAPI() {
    lua_newtable(L); // Create framework table

    lua_newtable(L); // Create input table
    lua_pushlightuserdata(L, window);
    lua_pushcclosure(L, l_isKeyDown, 1);
    lua_setfield(L, -2, "isKeyDown");
    lua_setfield(L, -2, "input"); // framework.input = {}

    lua_setglobal(L, "framework"); // Set framework table
}

int main() {
    Framework game;
    game.run();
    return 0;
};

The debug print always says Key state: 0, even when I’m pressing the buttons instead of properly returning 1. How do I fix this issue so it properly detects if a button is down?

I would first check that the included test code events.c outputs key presses when the window is focussed and you press a key. This can be built when building GLFW. If this works, then check your code against that in events.c and the GLFW input guide documentation.

If there is an issue with events.c then check you have the latest version of GLFW and I can help you diagnose the issue here.