I am trying to capture real-time keypresses in Windows CLI using PHP, but Windows buffers input, forcing me to press Enter before any key detection occurs. In Linux/macOS, I can use stty -icanon to disable input buffering, but Windows does not support this.
I have tried setting stream_set_blocking(STDIN, false) but still requires ENTER.
Capturing the keypress without ENTER works via FFI where C functions like _getch(), _kbhit() which is present in msvcrt.dll are being used.
Is there any pure PHP solution to handle real-time key detection in Windows without using FFI, PowerShell, or Node.js or has anyone tried solving this problem?
sample code does not work
<?php
$stdin = fopen('php://stdin', 'rb+');
stream_set_blocking($stdin, 0);
while(1)
{
$keypress = fgets($stdin);
if($keypress)
{
echo 'Key pressed: ' . $keypress . PHP_EOL;
}
}
still waits for ENTER before detecting input in Windows CLI. Any pure PHP alternative?`