A PHP based API to capture key presses from the terminal.

Maintainers

👁 ignasbernotas

Package info

github.com/ignasbernotas/phkey

Homepage

pkg:composer/ignasbernotas/phkey

Statistics

Installs: 193

Dependents: 2

Suggesters: 0

Stars: 5

Open Issues: 0

0.1.1 2016-04-13 18:53 UTC

Requires

Requires (Dev)

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT c6d5e13d2dd5a9e76eecd01e6adab5e8287eb2f3

clikeyboardkeyskey press

This package is not auto-updated.

Last update: 2026-06-21 04:16:33 UTC


README

PHP based command line key listener.

👁 Example

This library provides an API to capture keys from the terminal. It currently supports latin-basic range of characters such as direction keys, function keys (F*), enter, space, insert, delete, backspace, escape, a-z, etc.

Installation

You can either download this library as a zip, or simply install it via composer:

composer require ignasbernotas/phkey

Limitations

This package only works on UNIX/Linux based systems since the Windows PHP version doesn't not support the readline extension.

Example

Capture all keys
use \Iber\Phkey\Events\KeyPressEvent;
use \Iber\Phkey\Environment\Detector;

$detect = new Detector();
$listener = $detect->getListenerInstance();

$eventDispatcher = $listener->getEventDispatcher();

$eventDispatcher->addListener('key:press', function(KeyPressEvent $event) {
 echo $event->getKey(), PHP_EOL;
});

$eventDispatcher->addListener('key:enter', function(KeyPressEvent $event) use ($eventDispatcher) {
 echo 'Key "', $event->getKey(), '" was pressed. Quitting listener.', PHP_EOL;

 // notify the listener to stop
 $eventDispatcher->dispatch('key:stop:listening');
});

$listener->start();

License

Licensed under MIT.