note 114608 added to function.ncurses-getch
| From: | Anonymous | Date: | Tue, 11 Mar 2014 21:50:05 +0000 |
| Subject: | note 114608 added to function.ncurses-getch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198864@lists.php.net to get a copy of this message | ||
ncurses_getch() will block if there is no input available. Using fgets() together with
stream_set_blocking() has been proposed for non-blocking input, however, fgets() returns escape
sequences for cursor, function, and other special keys. Interpreting these is not trivial as the
escape sequences depend on the terminal/emulation used.
A possible solution is to use stream_select() to find out whether input is available and then read
it with ncurses_getch().
<?php
function getch_nonblock($timeout) {
$read = array(STDIN);
$null = null; // stream_select() uses references, thus variables are necessary for the first
3 parameters
if(stream_select($read,$null,$null,floor($timeout / 1000000),$timeout % 1000000) != 1) return
null;
return ncurses_getch();
}
while(true) {
$key = getch_nonblock(1000000);
if(is_null($key)) {
// do something useful here
continue;
}
if($key == NCURSES_KEY_F10) break; // Quit on F10
}
?>
Low timeout values can yield high CPU usage of your script, so set the timeout as high as possible
for your application.
----
Server IP: 213.172.110.14
Probable Submitter: 79.204.62.204
----
Manual Page -- http://php.net/manual/en/function.ncurses-getch.php
Edit -- https://master.php.net/note/edit/114608
Del: integrated -- https://master.php.net/note/delete/114608/integrated
Del: useless -- https://master.php.net/note/delete/114608/useless
Del: bad code -- https://master.php.net/note/delete/114608/bad+code
Del: spam -- https://master.php.net/note/delete/114608/spam
Del: non-english -- https://master.php.net/note/delete/114608/non-english
Del: in docs -- https://master.php.net/note/delete/114608/in+docs
Del: other reasons-- https://master.php.net/note/delete/114608
Reject -- https://master.php.net/note/reject/114608
Search -- https://master.php.net/manage/user-notes.php