Shlomi Fish <shlo...@shlomifish.org> writes: > 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. >>
Such a loop is what I'm trying to avoid. It would probably keep a CPU busy 100% doing basically nothing. > 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;' Thanks, that seems to work :) > The key (pardon the pun) is adding the ReadMode calls. Indeed --- I didn't change the ReadMode because I didn't see any need to disable control keys. Without changing the ReadMode, I have to press Enter. -- "Didn't work" is an error. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/