I would like to implement a "press any key to continue" prompt, similar to many command-line programs, so that a script running under Windows (in particular) will stay open until the user dismisses it, allowing for time to read any messages.

I have the following code, but it only works if the ENTER key is the key pressed:

function anykey($s='Press any key to continue...') {
	echo "\n".$s."\n";
	fgetc(STDIN);
}

Is there a way to get just a single keystroke (in this case, it doesn't even matter what key is pressed), instead of waiting for the enter key?

Thank you for your time.

Kyle

    People there are saying it's not possible.

    Thanks...it wasn't the answer I wanted to hear (need it to work on Windows), but thank you for the confirmation.

    After posting this, I installed PEAR, and noticed that the PEAR installer required you to press ENTER to continue. I think that it is fairly safe to assume that if the numerous developers behind PEAR couldn't come up with a "Press any key to continue..." prompt, then I will probably be unable to as well.

    If anyone has any more information, I would welcome it.

    Thanks,

    Kyle

      I don't know how PHP CLI works, but you can call a batch script which has the line "pause" - PHP can't complete until that script finishes, and it won't finish until the user presses any key.

        It looks like you need to flush the output buffer to get any command line output during script execution, on Windows at least.

        define( 'STDIN', fopen( 'php://stdin', 'r' ));
        function prompt( $msg )
        {
        	echo $msg . "\n";
        	ob_flush();
        	$in = trim( fgets( STDIN ));
        	return $in;
        }
        
        $key = prompt( 'Please press a key' );
        echo "You pressed $key\n";
        

        Additionally STDIN isn't defined in my CLI version, quite annoying.

          I don't know how PHP CLI works, but you can call a batch script which has the line "pause" - PHP can't complete until that script finishes, and it won't finish until the user presses any key.

          At first, when I read this, I dismissed it because I really want the script to be one file, for ease of portability. Then I thought about it, and realized that batch files are just a bunch of command line commands. If the batch file was only going to contain one command ("pause"), that would have the same effect as simply typing "pause" at a command prompt. Well, PHP has functions to execute command line functions, so I ended up with this:

          function anykey() {
          	passthru('pause');
          }

          Thank you for your help in this matter. I am not sure if Shrike's function would work, as the issue was that the prompt text was displayed, but it continued to accept input until the ENTER key was pressed.

          Kyle

            See, I didn't know PHP CLI had a way of passing command-line commands (well, actually, I did know - I just didn't know the right syntax - in PHP for web, I usually use backtics). Glad it worked out for you!

              You can use backticks in CLI, but I wasn't sure if they would pass the message through, or just sit there waiting for the presumably omniscient user to decide to press a key. I also used passthru() when I executed the call to the LAME MP3 encoder binary, so that the progress bar and statistics would be passed through to the end user.

              Kyle

                Allright. Glad it worked out for you!

                  Write a Reply...