Skip to content

Instantly share code, notes, and snippets.

Created April 22, 2012 20:36
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Lua getch extension
/* Build me with:
gcc -shared -o -undefined dynamic_lookup kb.c -lncurses
/* Copyright (C) 2012 Ross Andrews
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- GNU General Public License for more details.
-- You should have received a copy of the GNU Lesser General Public License
-- along with this program. If not, see <>. */
#include <ncurses.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
int getch_wrapper(lua_State *L);
int luaopen_kb(lua_State *L){
luaL_Reg fns[] = {
{"getch", getch_wrapper},
luaL_openlib(L, "kb", fns, 0);
initscr(); /* Start curses */
raw(); /* Turn off line buffering */
set_escdelay(25); /* Shorten delay after ESC key to something reasonable */
keypad(stdscr, TRUE); /* Grab ALL kbd input */
refresh(); /* Store screen state so endwin works */
endwin(); /* Leave curses mode */
return 0;
int getch_wrapper(lua_State *L){
reset_prog_mode(); /* Get back into curses */
lua_pushnumber(L, getch()); /* Grab a char and push it */
endwin(); /* Get out of curses again */
return 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment