我的解决方案是
-icanon在TTY上设置模式(使用
stty)。例如。:
stty -icanon
因此,现在可以使用的代码是:
#!/usr/bin/php<?phpsystem("stty -icanon");echo "input# ";while ($c = fread(STDIN, 1)) { echo "Read from STDIN: " . $c . "ninput# ";}?>输出:
input# fRead from STDIN: finput# oRead from STDIN: oinput# oRead from STDIN: oinput# Read from STDIN:input#
完成操作后,别忘了还原TTY …
恢复tty配置
通过在更改终端状态之前保存tty状态,可以将终端重置回原来的状态。完成后,您可以恢复到该状态。
例如:
<?php// Save existing tty configuration$term = `stty -g`;// Make lots of drastic changes to the ttysystem("stty raw opost -ocrnl onlcr -onocr -onlret icrnl -inlcr -echo isig intr undef");// Reset the tty back to the original configurationsystem("stty '" . $term . "'");?>这是保存tty并将其放回用户开始之前的方式的唯一方法。
请注意,如果您不担心保留原始状态,只需执行以下操作即可将其重置回默认的“健全”配置:
<?php// Make lots of drastic changes to the ttysystem("stty raw opost -ocrnl onlcr -onocr -onlret icrnl -inlcr -echo isig intr undef");// Reset the tty back to sane defaultssystem("stty sane");?>


