From 5f4cec42c1b74b0fb4cbe2a0794b4227985024e2 Mon Sep 17 00:00:00 2001 From: Sebastian Utbult Date: Wed, 22 Mar 2023 16:21:06 +0100 Subject: [PATCH] Various QOL touch-ups & update README --- README.md | 4 +- examples/intercept-arrow-keys.php | 3 +- src/Zenithies/Toolkit/ReadKey/Interceptor.php | 67 +++++++++++++------ 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 66880f9..03c4fda 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,6 @@ Detecting key press (arrow keys) in command line (console, cli) environment in P Note, at the moment it's available only on x64 version of windows and was tested on Windows 10 with PHP 8.1.x and 8.2.x TS -* PHP COM extension required: extension=php_com_dotnet -* You have to register DLL file from bin/win/ReadKey, using `regsvr32 ZenithiesCLIKeys_x64.dll` from command line with administrator permissions. +* PHP COM extension required. Enable by setting `extension=php_com_dotnet` in your php.ini +* You have to register the DLL file from bin/win/ReadKey, using `regsvr32 ZenithiesCLIKeys_x64.dll` from command line with administrator permissions. * For your own convenience put the `ZenithiesCLIKeys_x64.dll` on your system drive. \ No newline at end of file diff --git a/examples/intercept-arrow-keys.php b/examples/intercept-arrow-keys.php index 3d65900..5a819fb 100644 --- a/examples/intercept-arrow-keys.php +++ b/examples/intercept-arrow-keys.php @@ -1,4 +1,5 @@ = self::PHP_REQUIRED_MAJOR_VERSION && + PHP_MINOR_VERSION >= self::PHP_REQUIRED_MINOR_VERSION + ) { + } else { + throw new \Exception('PHP version must be ' . self::PHP_REQUIRED_MAJOR_VERSION . '.' . self::PHP_REQUIRED_MINOR_VERSION . ' or greater, currently using ' . PHP_VERSION); + } + // Windows specific checking if (self::isWindows()) { + // are we running (at least) windows 10? + if ((float)php_uname('r') < self::WINDOWS_REQUIRED_VERSION) { + throw new \Exception('Windows version ' . self::WINDOWS_REQUIRED_VERSION . ' or higher required.'); + } + // are we on a 64-bit architecture? + if (strpos(php_uname('m'), self::WINDOWS_ARCHITECTURE) === false) { + throw new \Exception('This script requires a ' . self::WINDOWS_ARCHITECTURE . ' bit architecture, you are currently on ' . php_uname('m')); + } + // does the COM class exist? if (!\class_exists('COM')) { - self::eprintln('Warning: COM extension si require On Windows: extension=php_com_dotnet (PHP 8.1)'); + self::eprintln('Warning: COM extension is required on windows. Please set extension=php_com_dotnet in your php.ini and register the DLL found in this repo.'); return false; } - + // can we load the DLL? try { $this->dll = new \COM('ZenithiesCLIKeys.ReadKey'); } catch (\Throwable $e) { self::eprintln("Unable to initialize ZenithiesCLIKeys.ReadKey, make sure it is registered by regsvr32 and you've picked architecture matching your PHP installation: {$e->getMessage()}"); return false; } + } + } + public function init(): bool + { + if (self::isWindows()) { $this->sequences = [ - 224 => [ + 224 => [ // normal arrow keys 72 => self::KEY_UP, // Up 77 => self::KEY_RIGHT, // Right 80 => self::KEY_DOWN, // Down 75 => self::KEY_LEFT, // Left ], - 0 => [ + 0 => [ // numpad arrow keys 72 => self::KEY_UP, // Up 77 => self::KEY_RIGHT, // Right 80 => self::KEY_DOWN, // Down @@ -94,9 +119,7 @@ private function interceptWIN(): int while (true) { $key = new \VARIANT(null, \VT_I8); - $this->dll->GetKey($key); - $key = (int)$key; if (isset($this->sequences[$key])) { @@ -117,16 +140,18 @@ private function interceptWIN(): int private function interceptNIX(): int { - \readline_callback_handler_install('', function(){}); + \readline_callback_handler_install('', function () { + }); $sequence = null; while (true) { - $s = [STDIN]; $w = null; $e = null; + $s = [STDIN]; + $w = null; + $e = null; if (stream_select($s, $w, $e, null)) { $key = \ord(\stream_get_contents(STDIN, 1)); - if (isset($this->sequences[$key])) { $sequence = $this->sequences; } @@ -183,9 +208,9 @@ public static function I(): Interceptor $class = __CLASS__; if (self::$__instance === false) { - self::$__instance = new $class; + self::$__instance = new $class(); } return self::$__instance; } -} \ No newline at end of file +}