11

I'm attempting to write a cross-platform PHP utility for personal use, not to reinvent anything. The code will run within more or less

while (true) {
    $key = null;
    $key = check_key_pressed();
    if ($key) do_relevant_magic($key);
    usleep(250000);
}

I need something for check_key_pressed() that will return a value of a pressed key, not waiting for EOF/EOL, not asking for input explicitly until any key is actually pressed.

This has to work on *nix and windows so ncurses is not an option. I also do not want exec calls to external vbs or bash scripts, it has to be done purely in PHP, hope I'm not wishing for too much.

EDIT: Of course I checked other SO solutions and none of them seems to be truly cross-platform. Ncrurses and readline are not available on windows and this has to intercept a keydown or keypress or input ready on both linux and windows.

DeDee
  • 1,972
  • 21
  • 30
  • possible duplicate of [PHP CLI: How to read a single character of input from the TTY (without waiting for the enter key)?](http://stackoverflow.com/questions/3684367/php-cli-how-to-read-a-single-character-of-input-from-the-tty-without-waiting-f) – Piotr Olaszewski Jan 24 '15 at 18:31
  • 1
    The first solution requires TTY calls on *nix and the second one requires readline library which is not available on windows platforms. – DeDee Jan 24 '15 at 18:46
  • And that other one is about the TTY anyway (even tagged as such), so "ab ovo" not a cross-platform question. Accordingly, that one has a solution (not even only one), while this one, which is a real pain point, doesn't. – Sz. Oct 15 '18 at 16:30

2 Answers2

3

Unfortunately, this is not possible (still today) without a similar platform-specific extension for Windows like the ones for *n*x (readline, ncurses).

Worse yet, even if you give up the cross-platform requirement, I doubt that there's any such (reasonably well-known) extension for that on Windows. (People tend to recommend writing the bindings for things like pdcurses (which is at least itself cross-platform) yourself, since apparently no-one has quite done and published one yet.)

Things are slowly progressing though, and at least the readline extension is now available for Windows (since 7.1), but it happens to be just short of the unbuffered char read functionality...

Sz.
  • 3,342
  • 1
  • 30
  • 43
0

As of 2023, there is still no native solution for Windows. However, it is possible to intercept keys via the PHP COM extension. A full solution for the Windows x64 platform and *nix with readline support is available here:

https://github.com/zenithies/php-toolkit-read-key

You can create your own Windows solution if you like, recipe:

  • create ATL COM DLL with method for intercepting keypress
  • register dll using regsrv32
  • use PHP COM extension and create wrapper for you DLL
Zenithies
  • 66
  • 6
  • "This has to work on *nix and windows". What you suggest does not work on Linux, and doesn't even work on 32 bit windows, and potentially earlier PHP or windows versions. – DeDee Mar 22 '23 at 16:53
  • @DeDee Wrapper utilizes readline for *nix and for windows it uses COM DLL, if the ATL COM DLL is compiled for x86 platform, it will work as well. As of PHP version, if the COM extension is available for it on WIn platform, then it should work too. Altering description. – Zenithies Mar 23 '23 at 00:28
  • thank you then for bringing it to my attention, but the "do not want exec calls to external vbs or bash scripts" could be easily extended to DLLs - it's virtually the same idea. Your answer was useful but it did not answer my question really, sorry. – DeDee Mar 24 '23 at 00:37