andyduke/console-keyboard
Composer 安装命令:
composer require andyduke/console-keyboard
包简介
Cross-platform reading of keyboard events in the terminal.
关键字:
README 文档
README
Cross-platform reading of keyboard events in the terminal.
Makes it possible to process key presses in the terminal (including arrow keys, escape, enter, etc.) in Linux, Windows, MacOS.
Attention: only works with PHP 7.4 and above, and uses FFI on Windows.
Usage
First, you need to create the Keyboard class using the static create() method, which will create the desired version of the class for the current platform:
$k = Keyboard::create();
Then you need to use a foreach loop to read the incoming keystrokes:
foreach($k->read() as $key) { }
You don't have to setup console modes to hide the output of the keys pressed - the
Keyboardclass does this automatically when the loop starts and restores the console mode when exiting the loop.
Attention: Pressing Ctrl-C interrupts the reading cycle.
Inside the loop you do the processing of each key press, for example displaying the name of each key pressed:
$k = Keyboard::create(); foreach($k->read() as $key) { echo $key . PHP_EOL; }
Inside the loop, you can implement any logic, checking which key is pressed, for example, in the above example you can add an exit from the loop using the q key:
$k = Keyboard::create(); foreach($k->read() as $key) { if ($key == 'q') { break; } echo $key . PHP_EOL; }
For keys such as arrow keys, escape, enter, spacebar, insert, delete, etc., the Keyboard class defines a set of constants that can be used in comparison:
$k = Keyboard::create(); foreach($k->read() as $key) { if ($key == 'q' || $key == Keyboard::ESC) { break; } echo $key . PHP_EOL; }
If you want to get not only the name of the key pressed but also the code (for Linux/MacOS this is ANSI code, for Windows this is scan code), you should use the readKey() method to read keys, instead of read().
The readKey() method returns a Key object containing the key name and code, which are accessible using the getKey() and getRawKey() methods, respectively.
A Key object can be compared to a string, which casts it to a string and returns the name of the key. An example of displaying the names and codes of the keys pressed; the q key exits the reading loop:
$k = Keyboard::create(); foreach($k->readKey() as $key) { if ($key == 'q') { break; } echo $key->getKey() . ' (' . $key->getRawKey() . ')' . PHP_EOL; }
Key Constants
| Constant | Key name |
|---|---|
| ESC | Escape |
| SPACE | Space |
| ENTER | Enter |
| TAB | Tab |
| BACKSPACE | Backspace |
| INS | Insert |
| DEL | Delete |
| HOME | Home |
| END | End |
| UP | Up arrow |
| DOWN | Down arrow |
| LEFT | Left arrow |
| RIGHT | Right arrow |
| PGUP | Page Up |
| PGDOWN | Page Down |
| F1 | F1 key |
| F2 | F2 key |
| F3 | F3 key |
| F4 | F4 key |
| F5 | F5 key |
| F6 | F6 key |
| F7 | F7 key |
| F8 | F8 key |
| F9 | F9 key |
| F10 | F10 key |
| F11 | F11 key |
| F12 | F12 key |
| F13 | F13 key |
| F14 | F14 key |
| F15 | F15 key |
| F16 | F16 key |
| F17 | F17 key |
| F18 | F18 key |
| F19 | F19 key |
| F20 | F20 key |
| F21 | F21 key |
| F22 | F22 key |
| F23 | F23 key |
| F24 | F24 key |
Why Generator?
The read() and readKey() methods return a generator, so you must use a foreach loop or another way of working with iterators to read keyboard input.
This is implemented this way because it allows the Keyboard class to automatically setup console modes at the beginning of reading console input and restore console modes after the end of the reading cycle.
License
This project is licensed under the terms of the BSD 3-Clause license.
andyduke/console-keyboard 适用场景与选型建议
andyduke/console-keyboard 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 41 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 09 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cli」 「keyboard」 「keys」 「key press」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 andyduke/console-keyboard 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 andyduke/console-keyboard 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 andyduke/console-keyboard 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Telegram Bot InlineKeyboard Pagination
Vault for private keys and secret data
Laravel SQLite Foreign Keys Support
Slim starter / Slim skeleton package to boost your development with Slim framework
Laravel relationships with support for composite/multiple keys
Generates a trait to help ease the access of custom repo methods
统计信息
- 总下载量: 41
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2023-09-12