in reply to howto capture a keystroke
#!/usr/bin/perl -w # # Example of getting a single, non-blocking character in Linux. # # 060306 liverpole # # Strict use strict; use warnings; # Create a subroutine (closure) for capturing a key sub one_key { chomp(my $stty = `stty -g`); # Save 'cooked' mode t +ty `stty -icrnl -icanon -echo min 0 time 0`; # Begin raw mode sub { # Create a closure if (!$_[0]) { # If argument is zero +... system("stty $stty"); # restore 'cooked' m +ode } else { # Otherwise get and re +turn `dd bs=1 count=1 <&0 2>/dev/null`; # a single keystroke } }; } # Main program my $p_one_key = one_key(); while (1) { print ' ' x int(rand(20)), "*\n"; select(undef, undef, undef, 0.1); last if $p_one_key->(1); } $p_one_key->(0);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: howto capture a keystroke
by Anonymous Monk on May 17, 2009 at 00:10 UTC | |
by Anonymous Monk on May 17, 2009 at 00:31 UTC | |
by Anonymous Monk on Aug 11, 2009 at 14:17 UTC |