olivebbs/getch

使用 ffi 在 Windows 和 Linux 上实现了 _getch 和 _ungetch

该软件包的规范仓库似乎已不存在,因此该软件包已被冻结。

维护者

详细信息

code.bgemi.net/Olive/getch.git

1.6.0 2023-02-10 04:49 UTC

This package is auto-updated.

Last update: 2024-01-17 19:50:21 UTC


README

此软件包仅使用 FFI 扩展来在 Windows 和 Linux 上启用 _getch 和 _ungetch。

Pipeline status Coverage report Latest Release

$ composer require olivebbs/getch
 use Olive\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 Olive\Console\getch;
$ord = getch($linuxLibrary = null);
print \chr($ord);

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

测试

vendor/bin/phpunit