Non blocking user input

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Non blocking user input

aryajur
Hi,
      Is it possible to have a non blocking keyboard poll in Lua? If there is a keypress I get a key code otherwise I get no key pressed.

Thanks,
Milind


Reply | Threaded
Open this post in threaded view
|

Re: Non blocking user input

Steve Litt
On Thu, 20 Mar 2014 07:00:12 -0700
Milind Gupta <[hidden email]> wrote:

> Hi,
>       Is it possible to have a non blocking keyboard poll in Lua? If
> there is a keypress I get a key code otherwise I get no key pressed.
>
> Thanks,
> Milind


=======================================
function getch_unix()
        os.execute("stty cbreak </dev/tty >/dev/tty 2>&1")
        local key = io.read(1)
        os.execute("stty -cbreak </dev/tty >/dev/tty 2>&1");
        return(key);      
end
=======================================

SteveT

Steve Litt                *  http://www.troubleshooters.com/
Troubleshooting Training  *  Human Performance

Reply | Threaded
Open this post in threaded view
|

Re: Non blocking user input

steve donovan
On Thu, Mar 20, 2014 at 4:08 PM, Steve Litt <[hidden email]> wrote:
> function getch_unix()
>         os.execute("stty cbreak </dev/tty >/dev/tty 2>&1")
>         local key = io.read(1)
>         os.execute("stty -cbreak </dev/tty >/dev/tty 2>&1");
>         return(key);
> end

That is indeed ingenious!  I was about to suggest using luaposix and use select.

The point is that there's no platform-independent way of doing this....

For Windows, you can get async i/o on the console using winapi:

http://stevedonovan.github.io/winapi/api.html#get_console

This returns a File object which has a read_async method, which you
pass a callback to receive anything read.

Reply | Threaded
Open this post in threaded view
|

Re: Non blocking user input

Justin Cormack


On Mar 20, 2014 2:16 PM, "steve donovan" <[hidden email]> wrote:
>
> On Thu, Mar 20, 2014 at 4:08 PM, Steve Litt <[hidden email]> wrote:
> > function getch_unix()
> >         os.execute("stty cbreak </dev/tty >/dev/tty 2>&1")
> >         local key = io.read(1)
> >         os.execute("stty -cbreak </dev/tty >/dev/tty 2>&1");
> >         return(key);
> > end
>
> That is indeed ingenious!  I was about to suggest using luaposix and use select.
>
> The point is that there's no platform-independent way of doing this....
>
> For Windows, you can get async i/o on the console using winapi:
>
> http://stevedonovan.github.io/winapi/api.html#get_console
>
> This returns a File object which has a read_async method, which you
> pass a callback to receive anything read.
>

For Linux you can also use the /dev/input API if you want actual raw keypress and mouse data.

Justin

Reply | Threaded
Open this post in threaded view
|

Re: Non blocking user input

Victor Bombi
In reply to this post by steve donovan

>
> The point is that there's no platform-independent way of doing this....
>

Yes, with luaLanes

Reply | Threaded
Open this post in threaded view
|

Re: Non blocking user input

Philipp Janda
Am 20.03.2014 19:19 schröbte Victor Bombi:
>
>>
>> The point is that there's no platform-independent way of doing this....
>>
>
> Yes, with luaLanes
>

LuaLanes isn't platform independent (although it runs on Windows and
POSIX at least). Additionally, while you can avoid blocking your program
via an extra thread, you might still have to wait for a newline
character, before any key presses are signaled to you (terminal input
usually is line-buffered).

If you are on a POSIX machine, see here[1] for a possible solution using
luaposix ...

Philipp

   [1]: http://permalink.gmane.org/gmane.comp.lang.lua.general/104876



Reply | Threaded
Open this post in threaded view
|

Re: Non blocking user input

aryajur
Thank you for all the suggestions. This is very helpful.


On Thu, Mar 20, 2014 at 6:37 PM, Philipp Janda <[hidden email]> wrote:
Am 20.03.2014 19:19 schröbte Victor Bombi:



The point is that there's no platform-independent way of doing this....


Yes, with luaLanes


LuaLanes isn't platform independent (although it runs on Windows and POSIX at least). Additionally, while you can avoid blocking your program via an extra thread, you might still have to wait for a newline character, before any key presses are signaled to you (terminal input usually is line-buffered).

If you are on a POSIX machine, see here[1] for a possible solution using luaposix ...

Philipp

  [1]: http://permalink.gmane.org/gmane.comp.lang.lua.general/104876




Reply | Threaded
Open this post in threaded view
|

Re: Non blocking user input

Steve Litt
In reply to this post by steve donovan
On Thu, 20 Mar 2014 16:15:16 +0200
steve donovan <[hidden email]> wrote:

> On Thu, Mar 20, 2014 at 4:08 PM, Steve Litt
> <[hidden email]> wrote:
> > function getch_unix()
> >         os.execute("stty cbreak </dev/tty >/dev/tty 2>&1")
> >         local key = io.read(1)
> >         os.execute("stty -cbreak </dev/tty >/dev/tty 2>&1");
> >         return(key);
> > end
>
> That is indeed ingenious!

Isn't that brilliant (Litt puffs out his chest).

I'd like to thank the guys who documented it, last century. You can see
it by:

man perlfunc

Then search for getc, and you'll see where I learned about it, back in
1999.

But of course, I'm taking complete credit for it :-)

Since then, I've used this same technique to read a single character,
sans newline, in Perl, Python, Ruby, Lua, and maybe even Bash and maybe
C.

Reading a keyhit without requiring a following Enter is a core
requirement for my UMENU software, which I've written in Perl, Python,
Ruby and Lua.

That getch_unix() I posted was copied and pasted out of the Lua
version of UMENU.

I love computers.

Thanks,

SteveT

Steve Litt                *  http://www.troubleshooters.com/
Troubleshooting Training  *  Human Performance