lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> On Fri, 29 Nov 2013, Jose Torre-Bueno wrote:
> Is there a way from inside Lua to check whether any key has been
> pressed?  io.read() will get a line from the console but if there
> is no input it pends waiting for input.  I would like to be able
> to write a test that gets input if any is ready but would continue
> if there is none.  It is not obvious to me if this can be done at
> all.

Not a solution, but some background. Here is a a Lua C function to do it extracted from one of my libraries, complete with comment:

/* R1: Boolean, true if there is outstanding data in the keyboard buffer.
** This is a helper to allow a demonstration terminal program implemented on the command line.
** Need a solution for Linux, although _kbhit is POSIX there seem to be theological reasons for not
** having it in Linux and it is not in baseline C.
*/
static int luakbhit(lua_State* L) {
#if defined(LUA_WIN)
	lua_pushboolean(L, _kbhit());
#else
	lua_pushboolean(L, FALSE);
#endif
	return 1;
}

Search online for "_kbhit" for more background on the theology!



---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com