/** * Asks the question * * @return array */ public function ask() { $this->drawer->open(); $phkey = new Detector(); $listener = $phkey->getListenerInstance(); $event = $listener->getEventDispatcher(); $event->addListener('key:up', function () { $this->cursor->moveUp(count($this->choices)); $this->draw(); }); $event->addListener('key:down', function () { $this->cursor->moveDown(count($this->choices)); $this->draw(); }); $event->addListener('key:space', function () { $this->selectChoice(); $this->draw(); }); $event->addListener('key:enter', function () use($event) { $event->dispatch('key:stop:listening'); $this->drawer->closeWindow(); }); $this->draw(); $listener->start(); return $this->getAnswers(); }
<?php include __DIR__ . '/../vendor/autoload.php'; 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();