Capture keypresses on Windows
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 40.4k
- Forks
- 8.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 96
Description
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
CMB gave some hope this would be implemented, and so, not a bug at all.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the historical discussion in bugs.php.net/bug.php?id=34972 and the existing terminal input handling in php-src. Determine the appropriate Windows entry point and how single keypress capture should behave across supported operating systems. Done means a documented, tested cross-platform way to read a key without requiring Enter.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, php
- Domain
- cli, operating-systems
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100