Skip to content

Capture keypresses on Windows #12939

Open
@terremoth

Description

@terremoth

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');
system('stty cbreak -echo');

stream_set_blocking($stdin, false);

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

EDIT:
This example captures keys presses in the open terminal, except: shift, alt, control, caps lock, meta and fn


This example above 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions