Hi lee,

On Sat, 06 May 2017 02:06:19 +0100
lee <l...@yagibdah.de> wrote:

> Hi,
> 
> how can a sleeping program react to a key that was pressed without
> return being pressed?
> 
> 
> perl -e 'use Term::ReadKey; my $ke = ReadKey(10); print "k: $ke\n";'
> 
> 
> ... shows that:
> 
> 
> + ReadKey() returns undef after the timeout
> 
> + ReadKey() returns undef after the timeout even when you pressed keys
> 
> + ReadKEy() returns the first key of those that were pressed before the
>     timeout expires when you hit return before it does
> 
> 
> So you always have to hit return :(
> 
> I'm finding that entirely useless for instances in which a program is
> sleeping for some time (let's say 10 minutes) but supposed to do
> something immediately when a key is pressed, and without pressing
> return.  For example, I might want to press 'q' to quit and don't want
> to wait 10 minutes for the program to react, and of course, I don't want
> to press enter.
> 
> How can that be done?
> 
> 

This program from perldoc Term::ReadKey is working as advertised:

«
#!/usr/bin/perl

use strict;
use warnings;

use Term::ReadKey;
ReadMode 4; # Turn off controls keys
my $key;
while (not defined ($key = ReadKey(-1))) {
    # No key yet
}
print "Get key $key\n";
ReadMode 0; # Reset tty mode before exiting

»

Note that it may be a busy loop. It mad be a bug in Term::ReadKey or you're
doing something wrong.

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
http://www.shlomifish.org/humour/bits/New-versions-of-the-GPL/

As it turns out, compiling a C program from more than 20 years ago is actually
a lot easier than getting a Rails app from last year to work.
    — https://passy.svbtle.com/building-vim-from-1993-today

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to