Skip to content

Instantly share code, notes, and snippets.

@bferguson3
Created November 23, 2025 19:20
Show Gist options
  • Select an option

  • Save bferguson3/db93a12c923dd56f80b8876605431f2e to your computer and use it in GitHub Desktop.

Select an option

Save bferguson3/db93a12c923dd56f80b8876605431f2e to your computer and use it in GitHub Desktop.
Lua DLL example for non-blocking read on Windows
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
/* in Lua:
require "tty"
...
local c = tty.read_chr()
if c ~= 0 then
...
*/
//__declspec(dllexport)
// gcc -shared -o tty.dll -lluajit tty.c -I..\luajit-dist\include -L..\luajit-dist
static int l_read_chr(lua_State* L) {
if (kbhit()){
lua_pushnumber(L, getch());
} else {
lua_pushnumber(L, 0);
}
return 1;
}
static const struct luaL_Reg mylib[] = {
{"read_chr", l_read_chr},
{NULL, NULL}
};
int luaopen_tty(lua_State* L){
luaL_register(L, "tty", mylib);
return 1;
}
int main() {
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment