# glfwGetKey always returns 0 (C++/Lua)

**URL:** <https://discourse.glfw.org/t/glfwgetkey-always-returns-0-c-lua/2870>\
**Category:** support\
**Created:** [March 4, 2025, 9:30am UTC](https://discourse.glfw.org/t/glfwgetkey-always-returns-0-c-lua/2870 "2025-03-04T09:30:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bart](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bart](https://discourse.glfw.org/u/bart)\
**Post date:** [March 4, 2025, 9:30am UTC](https://discourse.glfw.org/t/glfwgetkey-always-returns-0-c-lua/2870/1 "2025-03-04T09:30:09Z")

</div>

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?

```auto
#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?

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [March 4, 2025, 9:36am UTC](https://discourse.glfw.org/t/glfwgetkey-always-returns-0-c-lua/2870/2 "2025-03-04T09:36:19Z")

</div>

I would first check that the included test code [events.c](https://github.com/glfw/glfw/blob/master/tests/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](https://www.glfw.org/docs/latest/input_guide.html).

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.
