David,

Thank you for your response.

The code does not run on my box. I get an undefined variable error on the:
if ($buffer != "") {
line.

Also, I need the code to block until the user presses a single key. Plus, the fgets() routine does not return extended codes for pgUp, pgDn, etc, only codes for regular keys.

-Andres




David OBrien wrote:
?

On Fri, May 4, 2012 at 10:48 AM, Andres Gonzalez <and...@packetstorm.com <mailto:and...@packetstorm.com>> wrote:

    I am trying to code a CLI PHP script that will give me the
    keycodes for these (and other) keyboard keys. I want to basically
    block until the user presses a single key, then I want to process
    that event without the user having to press the Enter key after
    pressing a single key.

    I have tried several approaches but with no success. Using fopen()
    to open stdin, and fgetc() will give me the regular keys but
    requires the user to press the Enter key after the initial key is
    pressed. When I press the the PgDw key for example, those routines
    return a single character string even though the stty echos a 5
    character string (using PgDw echos "^[[6~")

    I have tried other approaches using bash read commands to get a
    single character but will little success also.

    Anybody here know how to do this?

    Thanks,

    -Andres

    |
    |

-- PHP General Mailing List (http://www.php.net/)
    To unsubscribe, visit: http://www.php.net/unsub.php


I think something like this would work

$running = true;
$fp = fopen("php://stdin","r"); //open direct input stream for reading
stream_set_blocking($fp,0); //set non-blocking mode

while ($running) {
while (($buf = fgets($fp, 4096)) != false) {
$buffer .= $buf;
}
if ($buffer != "") {
switch ($buffer) {
case " ": { //exit on space key
exit;
}
default: {
//space not pressed
}
}
$buffer = ""; //empty buffer
}



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to