On Sun, 14 May 2017 12:08:59 +0300
Shlomi Fish <shlo...@shlomifish.org> wrote:

> 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.
> 

I've now tried this program and it seems to do what you want:

perl -e 'use Term::ReadKey; ReadMode 4; my $ke = ReadKey(3); print "k: $ke\n";
ReadMode 0;'

The key (pardon the pun) is adding the ReadMode calls.

Regards,

        Shlomi Fish

> Regards,
> 
>       Shlomi Fish
> 



-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Perl Humour - http://perl-begin.org/humour/

XSLT is the number one cause of programmers’ suicides since Visual Basic 1.0.
    — http://www.shlomifish.org/humour/bits/facts/XSLT/

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