PHPackages                             andyduke/console-keyboard - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. andyduke/console-keyboard

ActiveLibrary[CLI &amp; Console](/categories/cli)

andyduke/console-keyboard
=========================

Cross-platform reading of keyboard events in the terminal.

v1.1.0(2y ago)339BSD-3-ClausePHPPHP &gt;=7.4.0

Since Sep 12Pushed 2y ago1 watchersCompare

[ Source](https://github.com/andyduke/console-keyboard)[ Packagist](https://packagist.org/packages/andyduke/console-keyboard)[ Docs](https://github.com/andyduke/console-keyboard)[ RSS](/packages/andyduke-console-keyboard/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (3)Used By (0)

ConsoleKeyboard
===============

[](#consolekeyboard)

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
-----

[](#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 `Keyboard` class 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](#key-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

[](#key-constants)

ConstantKey nameESCEscapeSPACESpaceENTEREnterTABTabBACKSPACEBackspaceINSInsertDELDeleteHOMEHomeENDEndUPUp arrowDOWNDown arrowLEFTLeft arrowRIGHTRight arrowPGUPPage UpPGDOWNPage DownF1F1 keyF2F2 keyF3F3 keyF4F4 keyF5F5 keyF6F6 keyF7F7 keyF8F8 keyF9F9 keyF10F10 keyF11F11 keyF12F12 keyF13F13 keyF14F14 keyF15F15 keyF16F16 keyF17F17 keyF18F18 keyF19F19 keyF20F20 keyF21F21 keyF22F22 keyF23F23 keyF24F24 keyWhy Generator?
--------------

[](#why-generator)

The `read()` and `readKey()` methods return a [generator](https://www.php.net/manual/en/language.generators.overview.php), 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
-------

[](#license)

This project is licensed under the terms of the BSD 3-Clause license.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Total

2

Last Release

970d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ac9cd1e7835990433f0a1963502707f2d4fc5eec4aff5fa568dd19b5f03ce190?d=identicon)[andyduke](/maintainers/andyduke)

---

Top Contributors

[![andyduke](https://avatars.githubusercontent.com/u/52318?v=4)](https://github.com/andyduke "andyduke (27 commits)")

---

Tags

cliconsolekeyboardphpterminalclikeyskeyboardkey press

### Embed Badge

![Health badge](/badges/andyduke-console-keyboard/health.svg)

```
[![Health](https://phpackages.com/badges/andyduke-console-keyboard/health.svg)](https://phpackages.com/packages/andyduke-console-keyboard)
```

###  Alternatives

[symfony/console

Eases the creation of beautiful and testable command line interfaces

9.8k1.1B11.3k](/packages/symfony-console)[nunomaduro/collision

Cli error handling for console/command-line PHP applications.

4.6k331.8M8.5k](/packages/nunomaduro-collision)[nunomaduro/termwind

It's like Tailwind CSS, but for the console.

2.5k239.8M286](/packages/nunomaduro-termwind)[wp-cli/wp-cli

WP-CLI framework

5.0k17.2M320](/packages/wp-cli-wp-cli)[wp-cli/php-cli-tools

Console utilities for PHP

68325.0M367](/packages/wp-cli-php-cli-tools)[socialengine/sniffer-rules

A Lumen 5 and Laravel 5 SquizLabs Code Sniffer 2.0 artisan command. Detect violations of a defined coding standard. It helps your code remains clean and consistent.

1248.2k1](/packages/socialengine-sniffer-rules)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
