in reply to Re: howto capture a keystroke
in thread howto capture a keystroke

If you have a while loop that is spinning and you don't want to prompt the user for any key to be pressed such as with <STDIN> but to silently break out when she hits ENTER key use the following 3 line function called "keystroke()" where $j variable will change from 0 to 1 when the ENTER key is pressed: while (true) { ... if (&keystroke == 1) { last; } ... } sub keystroke { $i = ''; vec($i, fileno(STDIN), 1) = 1; $j = select ($i, undef, undef, 0); }

Replies are listed 'Best First'.
Re^3: howto capture a keystroke
by Anonymous Monk on May 17, 2009 at 00:31 UTC
    If you have a while loop that is spinning and you don't want to prompt the user for any key to be pressed such as with <STDIN> and you don't want to use Term:ReadKey module but to silently break out when she hits ENTER key use the following 3 line function called "keystroke()" where $j variable will change from 0 to 1 when the ENTER key is pressed:

    while (true) {
        ...
        if (&keystroke == 1) {
            last;
        }
        ...
    }

    sub keystroke {
        $i = '';
        vec($i, fileno(STDIN), 1) = 1;
        $j = select ($i, undef, undef, 0);
    }
      Any chance of changing the ENTER key for something else (like Q or whatever)?