#!/usr/bin/env perl
use Term::ReadKey;
ReadMode 4;
END {
ReadMode 0; # Reset tty mode before exiting
}
while (<>) {
$key = ReadKey(0);
$key == "\x04" and last; # Ctrl+D breaks the loop
print $key;
}
When I had it without the while loop, it was printing back what I typed in.
It doesn't even produce any output at the end (if it was buffering it or something). Like I'd run it and type a few letters and hit Ctrl+D. It prints nothing.
I'm trying to make a program to convert mouse scroll escape codes into keypresses. I hope I'm not barking up the wrong tree.
eqoperator. The==converts the arguments to numbers first, which will usually be zero. You would have been warned had you putuse strict; use warningsat the top of your script.echo "abc" | this_programstill prints nothing. However if I take the loop out and have it read and print 2 char's,echo "abc" | this_programwill in fact printab\n) with the<>operator. Then, you read a single keypress withReadKey(0)which you may echo. The line (in$_) is discarded. Change your loop condition towhile(defined(my $key = ReadKey(0))){...}and reallyuse strict; use warnings;.