php / php-src

The PHP Interpreter
https://www.php.net
Other
38.17k stars 7.74k forks source link

Capture keypresses on Windows #12939

Open terremoth opened 11 months ago

terremoth commented 11 months ago

Description

Some tutorials and packages show it is possible to capture and handle keypresses on terminals, but only available on *nix systems

The example below shows a commonly see implementation.

<?php

$stdin = fopen('php://stdin', 'r');

stream_set_blocking($stdin, 0);

while (1) {
    $keypress = fgets($stdin);
    if ($keypress) {
        echo 'Key pressed: ' . $keypress . PHP_EOL;
    }
}

But that does not work on Windows. The intention is to capture a single keypress, of course, without typing enter. This would make possible things like terminal games and many command line apps, working for any O.S.

This was discussed in the past here: https://bugs.php.net/bug.php?id=34972

image

CMB gave some hope this would be implemented, and so, not a bug at all.

bukka commented 10 months ago

Just for the record there is a long standing feature request for this in bug tracker as well: https://bugs.php.net/bug.php?id=34972 . We should of course leave this open as we prefer GH issues now.

terremoth commented 10 months ago

Just for the record there is a long standing feature request for this in bug tracker as well: https://bugs.php.net/bug.php?id=34972 . We should of course leave this open as we prefer GH issues now.

Yeah, I mentioned this URL on top of the image. Thanks.