sikofitt / getch

此包已被 废弃 且不再维护。作者建议使用 olivebbs/getch 包。

使用 ffi 实现了 windows 和 linux 的 _getch 和 _ungetch

维护者

详细信息

code.bgemi.net/sikofitt/getch

1.5.0 2022-05-17 17:04 UTC

This package is auto-updated.

Last update: 2022-05-17 17:33:14 UTC


README

此代码仅使用 FFI 扩展在 Windows 和 Linux 中启用 _getch 和 _ungetch。

pipeline status coverage report

$ composer require sikofitt/getch:dev-master
 use Sikofitt\Console\Getch;
 $g = new Getch($linuxLibrary = null); // can also be a library that implements a function called _getch;
                                       // by default uses the bundled Resources/libgetch.so
                                       // on windows uses the built in _getch function.
 $ord = $g->getch();
 print \chr($ord);
 
 $ord = $g->ungetch('A');
 $res = $g->getch();
 $ord === $res // 65

注意:如果您要将单词放入 STDIN 栈,则需要反转操作。


    foreach(\str_split(\strrev('Hello World!')) as $char) {
        ungetch($char);
    }

    $result = '';

    do {
        $ord = getch();
        $result .= \chr($ord);
   } while($ord !== ord('!'));

   print $result; // Hello World!

还有名为 getch() 和 ungetch() 的辅助函数;

use function Sikofitt\Console\getch;
$ord = getch($linuxLibrary = null);
print \chr($ord);

$ord = ungetch('B');
$res = getch();
 $ord === $res // 66

测试

vendor/bin/phpunit