jcsoybomb has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a program to run in a DOS command shell (please don't tell me to switch to linux :) - I want to add functionality to display a progress report when the user presses [SPACEBAR], and increase verbosity when user presses [V], decrease it when they press [SHIFT] + [V]. This functions similarly to nmap if you're familiar.

I can't get the process to run silently until a keypress, it either waits for a keypress each loop, or it just ignores them until the end.

I've tried Term::Readkey with no luck.
In previous programs I've caught SIG{INT}, but apparently that only registers with [CTRL]+[C].

For clarity, the basic structure would be this:

While (1) { # Do work here... $keyPressed = CatchTheInput(); # This is where I'm stuck if( $keyPressed =~ /[Vv]/ ){ $verbosity++; } if( $keyPressed == spacebar ){ GiveProgressReport(); } }

Replies are listed 'Best First'.
Re: Capture keypress while process running
by BrowserUk (Patriarch) on Jul 31, 2009 at 16:57 UTC

    This should get you started:

    #! perl -slw use strict; use Term::ReadKey; while( 1 ) { my $key = ReadKey 0.1; print 'Got: ' , defined $key ? "'$key'" : ' nothing!'; } __END__ c:\test>"ReadKey(Term).pl" Got: nothing! ... Got: nothing! Got: ' ' Got: nothing! ... Got: nothing! Got: 'v' Got: nothing! Got: nothing! Got: nothing! Got: 'V' Got: nothing! ... Got: nothing! Terminating on signal SIGINT(2)

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Yes! That is exactly what I wanted, thank you! Now to dissect your code... copying without understanding is cheating.
        On Linux, this code produces: Segmentation fault I do not understand what is happening behind the scenes using ReadKey. But what I can tell is: my $inp = ReadKey 0; works just fine on Linux, and for me, also on Win 7 x64, perl 5.14.4 MSWin32-x86-multi-thread For the records: (v5.16.0) built for x86_64-linux-thread-multi, openSUSE 12.2 (x86_64) Hope to be of help to someone. Bogi
Re: Capture keypress while process running
by Anonymous Monk on Jul 31, 2009 at 16:43 UTC
    I've tried Term::Readkey with no luck.

    Show what you tried. Try Win32::Console