note 71157 modified in function.ncurses-getch by danbrown

From: Date: Thu, 18 Aug 2011 16:50:08 +0000
Subject: note 71157 modified in function.ncurses-getch by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-182571@lists.php.net to get a copy of this message
While function ncurses_nodelay() is still not implemented and if you need a non-blocking getch, following code might help. <?php $init = ncurses_init(); $full = ncurses_newwin (0,0,0,0); ncurses_wborder($full,0,0,0,0,0,0,0,0); ncurses_wrefresh($full); $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) { //fgets is required if we want to handle escape sequenced keys $buffer .= $buf; } if ($buffer != "") { switch ($buffer) { case " ": { //exit on space key ncurses_end(); exit; } default: { ncurses_mvwaddstr($full,2,2,"$buffer"); //display input } } $buffer = ""; //empty buffer } // You can do something interesting here, while we're not waiting for an input ncurses_mvwaddstr($full,4,4,microtime(true)); ncurses_wrefresh($full); usleep(1); //reduce cpu usage } ?> --was-- While function ncurses_nodelay() is still not implemented and if you need a non-blocking getch, following code might help. $init = ncurses_init(); $full = ncurses_newwin (0,0,0,0); ncurses_wborder($full,0,0,0,0,0,0,0,0); ncurses_wrefresh($full); $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) { //fgets is required if we want to handle escape sequenced keys $buffer .= $buf; } if ($buffer != "") { switch ($buffer) { case " ": { //exit on space key ncurses_end(); exit; } default: { ncurses_mvwaddstr($full,2,2,"$buffer"); //display input } } $buffer = ""; //empty buffer } // You can do something interesting here, while we're not waiting for an input ncurses_mvwaddstr($full,4,4,microtime(true)); ncurses_wrefresh($full); usleep(1); //reduce cpu usage } http://php.net/manual/en/function.ncurses-getch.php

« previous php.notes (#182571) next »