1

I've started working on a little project in Lua that involves making a text-based interface that updates constantly, and allows keyboard input for interaction.

I need a way to get keyboard input, but I also need it to either not block, or have some kind of timeout (which can be set to a fraction of a second, preferably). I've done research myself, but I found nothing that worked for me.

I need something that works with Lua 5.1.5 and Linux. Windows compatibility would be nice, but is not a requirement as I'm also doing things that require an ANSI terminal.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
account3r2
  • 43
  • 1
  • 5
  • 2
    Try ncurses, there must be a Lua wrapper for it. – hyde Mar 01 '15 at 07:22
  • @hyde I'd kind of rather not have to depend on ncurses, but it looks like I'm going to have to. Thanks for your suggestion though! I didn't even think of it until you said it. – account3r2 Mar 01 '15 at 08:00
  • Also see http://stackoverflow.com/questions/25689610/lua-io-read-with-time-limit/25717699#25717699 – siffiejoe Mar 01 '15 at 09:26
  • You could also use select/poll C library function, Lua surely provides that. With it you can check if you can read from a socket without blocking, and then read only if there's something to read. But it won't work on Windows (Windows stdin is not real file/socket). – hyde Mar 01 '15 at 10:25
  • Oh yeah, and with standard input, you probably have to disable buffering, otherwise your application will not get anything until user presses enter, as line buffering is default on terminal. With ncurses or similar libs you don't have to worry about this, they do it for you I think. – hyde Mar 01 '15 at 10:31
  • Also you can use libuv binding: require"lluv".tty(0, true):start_read(function(t,err,str) print(str) end) uv.run() – moteus Mar 01 '15 at 13:03

2 Answers2

0

As stated in the comments of my post by hyde, I can use a Lua wrapper for ncurses to get input. In addition to this, I can use its features for some other parts of my code that I was going to program myself anyways.

account3r2
  • 43
  • 1
  • 5
0

I'm doing this in Lua 5.1 with Luasocket and opening two separate Lua processes. I have two Lua console windows- "INPUT WINDOW" AND "OUTPUT WINDOW". INPUT WINDOW sends keypresses over localhost. OUTPUT WINDOW reads the localhost socket I'm using for this. It's non-blocking; you can set a very quick timeout on the udp receive. It's ugly, but it's the most vanilla solution I've found. That said, input data from INPUT window doesn't appear on the OUTPUT window (unless I want it to), which can be nice for a console-based UI.