http://qs1969.pair.com?node_id=869605


in reply to Re: Problem with ReadKey
in thread Problem with ReadKey

I apologize for the partial, unrunnable code in my post; I shouldn't post when sleep-deprived and frustrated. If I had taken the time to figure out a minimal script that reproduces the problem, I could have asked a much more specific question -- which I'll do now. The sample script provided by jethro worked fine on my system. I then tried to figure out what was the salient difference between his script and mine, and quickly remembered that my script was (because of the huge number of text files it might need to handle) reading a long list of filenames from stdin rather than the command line. It's normally run in a pipe from a find command, like so:
find $HOME -name \*.txt | textual-slideshow.pl $*
It slurps the filenames from standard input, then reads random sample paragraphs from some of them, prints random paragraphs a line at a time, and repeats. My simplified version that's partway between jethro's script and mine looks like this:
#! /usr/bin/perl use Term::ReadKey; use Time::HiRes qw(time); my @slurp_stdin = <>; ReadMode 3; while (1) { my $key; my $wait_until = time + 3; while ( time < $wait_until ) { $key = ReadKey( -1 ); if ( defined $key ) { print STDERR "keystroke $key\t"; } } print "Something\n"; }
And it has the same problem as my script, not surprisingly. The problem clearly has to do with running in an environment where stdin is redirected. So I tried to fix that by closing STDIN and reopening it after reading the filenames. That didn't help. I tried adding an explicit STDIN argument to the ReadMode and ReadKey calls; that didn't help either. This is what I've got now, and it also has the same problem as my original script or the above modification of jethro's script:
#! /usr/bin/perl -w use Term::ReadKey; use Time::HiRes qw(time); my @slurp_stdin = <STDIN>; close STDIN; open STDIN, "-"; while (1) { my $key; my $wait_until = time + 3; while ( time < $wait_until ) { ReadMode 3, STDIN; # 'noecho'; $key = ReadKey( -1, STDIN ); if ( defined $key ) { print STDERR "keystroke $key\t"; } ReadMode 0, STDIN; } print "Something\n"; }
This repeatedly prints "Something"; but when I press keys, I don't get the "keystroke (key value)" message, only the value of the key. My full original script is at http://jimhenry.conlang.org/scripts/textual-slideshow.zip.