[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: inkey$ in lua?
- From: Thatcher Ulrich <tu@...>
- Date: Fri, 31 Oct 2003 10:06:34 -0500
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 http://luacheia.lua-users.org
A luacheia key-press example would be:
---- key_press.lua ---
assert(cheia.load("SDL"))
-- need a window to receive events
assert(SDL.SDL_Init(SDL.SDL_INIT_VIDEO))
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
end
-- 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
end
end
end
SDL.SDL_Quit()
---- end key_test.lua ----
--
Thatcher Ulrich
http://tulrich.com