in reply to How to know whether user terminates system("$cmd|more") with q or not?
It probably will get more complicated than you want. You want to intercept the q keypress.
Here is a simple example, (or you can move up to an eventloop system, or watch STDIN in the separate thread, and let it watch for q). I 'm not sure how to pass the q to killing your command , unless you use a piped open or some other IPC module that returns a pid. Maybe someone knows a clever trick for that using return values from system.
#!/usr/bin/perl use warnings; use strict; use Term::ReadKey; #passing ReadKey() an argument of -1 to indicate not to block: ReadMode('cbreak'); while(1){ my $char; if (defined ($char = ReadKey(0)) ) { print "$char->", ord($char),"\n"; # input was waiting and i +t was $char if(ord($char) == 113){ print "Got a q\n"; print "Do your thing + here\n" } if(ord($char) == 27){ print "Got an Escape\n"; exit; } } else { # no input was waiting } } ReadMode('normal'); # restore normal tty settings
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to know whether user terminates system("$cmd|more") with q or not?
by PerlOnTheWay (Monk) on Sep 13, 2011 at 12:06 UTC | |
by zentara (Cardinal) on Sep 13, 2011 at 15:17 UTC | |
by PerlOnTheWay (Monk) on Sep 13, 2011 at 15:55 UTC | |
by zentara (Cardinal) on Sep 13, 2011 at 16:08 UTC |