lua-users home
lua-l archive

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

On Oct 31, 2003 at 09:30 +0100, Philippe Lhoste wrote:
> > Is there a way to gather single keystrokes from the user in lua?
> > 
> > or perhaps a way to see if any input is waiting without blocking on the 
> > request?
> In base Lua, no way to do this, it is highly dependent on the system
> you are running.  For example, you don't use the same way in MS-Dos,
> in Linux, VMS, Windows or Mac-OS...

You can try luacheia, which is Lua, plus some extension modules.  It
includes an SDL module among other things.  SDL is an excellent
library that wraps graphics, audio and input API's (plus some others)
for writing cross-platform games.  luacheia includes a sample
videogame written in Lua.  See

A luacheia key-press example would be:

---- key_press.lua ---

-- need a window to receive events
assert(SDL.SDL_SetVideoMode(640, 480, 16, SDL.SDL_HWSURFACE + SDL.SDL_ANYFORMAT))

print("press 'q' to exit...")

-- count upwards while watching for keypress

local start_ticks = SDL.SDL_GetTicks()
local last_count_ticks = start_ticks
local running = true
local event_buffer = SDL.SDL_Event_new()

while running do
	local ticks = SDL.SDL_GetTicks()
	if (ticks - last_count_ticks > 1000) then
		print(math.floor((ticks - start_ticks) / 1000))
		last_count_ticks = ticks
	-- consume events
	while SDL.SDL_PollEvent(event_buffer) ~= 0 do
		if event_buffer.type == SDL.SDL_QUIT then
			-- user wants to close our window
			running = false

		elseif event_buffer.type == SDL.SDL_KEYDOWN then
			local	sym = event_buffer.key.keysym.sym
			if sym == SDL.SDLK_q then
				-- 'q' pressed, exit the loop
				running = false

---- end key_test.lua ----

Thatcher Ulrich