ncurses_getch
(PHP 4 >= 4.0.7, PHP 5, PECL ncurses:1.0.0)
ncurses_getch — Read a character from keyboard
Описание
int ncurses_getch
( void
)
Внимание
Эта функция является ЭКСПЕРИМЕНТАЛЬНОЙ. Поведение этой функции, ее имя и относящаяся к ней документация могут измениться в последующих версиях PHP без уведомления. Используйте эту функцию на свой страх и риск.
Внимание
К настоящему времени эта функция еще не была документирована; для ознакомления доступен только список аргументов.
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с командной строкой
- Ncurses Terminal Screen Control
- ncurses_addch
- ncurses_addchnstr
- ncurses_addchstr
- ncurses_addnstr
- ncurses_addstr
- ncurses_assume_default_colors
- ncurses_attroff
- ncurses_attron
- ncurses_attrset
- ncurses_baudrate
- ncurses_beep
- ncurses_bkgd
- ncurses_bkgdset
- ncurses_border
- ncurses_bottom_panel
- ncurses_can_change_color
- ncurses_cbreak
- ncurses_clear
- ncurses_clrtobot
- ncurses_clrtoeol
- ncurses_color_content
- ncurses_color_set
- ncurses_curs_set
- ncurses_def_prog_mode
- ncurses_def_shell_mode
- ncurses_define_key
- ncurses_del_panel
- ncurses_delay_output
- ncurses_delch
- ncurses_deleteln
- ncurses_delwin
- ncurses_doupdate
- ncurses_echo
- ncurses_echochar
- ncurses_end
- ncurses_erase
- ncurses_erasechar
- ncurses_filter
- ncurses_flash
- ncurses_flushinp
- ncurses_getch
- ncurses_getmaxyx
- ncurses_getmouse
- ncurses_getyx
- ncurses_halfdelay
- ncurses_has_colors
- ncurses_has_ic
- ncurses_has_il
- ncurses_has_key
- ncurses_hide_panel
- ncurses_hline
- ncurses_inch
- ncurses_init_color
- ncurses_init_pair
- ncurses_init
- ncurses_insch
- ncurses_insdelln
- ncurses_insertln
- ncurses_insstr
- ncurses_instr
- ncurses_isendwin
- ncurses_keyok
- ncurses_keypad
- ncurses_killchar
- ncurses_longname
- ncurses_meta
- ncurses_mouse_trafo
- ncurses_mouseinterval
- ncurses_mousemask
- ncurses_move_panel
- ncurses_move
- ncurses_mvaddch
- ncurses_mvaddchnstr
- ncurses_mvaddchstr
- ncurses_mvaddnstr
- ncurses_mvaddstr
- ncurses_mvcur
- ncurses_mvdelch
- ncurses_mvgetch
- ncurses_mvhline
- ncurses_mvinch
- ncurses_mvvline
- ncurses_mvwaddstr
- ncurses_napms
- ncurses_new_panel
- ncurses_newpad
- ncurses_newwin
- ncurses_nl
- ncurses_nocbreak
- ncurses_noecho
- ncurses_nonl
- ncurses_noqiflush
- ncurses_noraw
- ncurses_pair_content
- ncurses_panel_above
- ncurses_panel_below
- ncurses_panel_window
- ncurses_pnoutrefresh
- ncurses_prefresh
- ncurses_putp
- ncurses_qiflush
- ncurses_raw
- ncurses_refresh
- ncurses_replace_panel
- ncurses_reset_prog_mode
- ncurses_reset_shell_mode
- ncurses_resetty
- ncurses_savetty
- ncurses_scr_dump
- ncurses_scr_init
- ncurses_scr_restore
- ncurses_scr_set
- ncurses_scrl
- ncurses_show_panel
- ncurses_slk_attr
- ncurses_slk_attroff
- ncurses_slk_attron
- ncurses_slk_attrset
- ncurses_slk_clear
- ncurses_slk_color
- ncurses_slk_init
- ncurses_slk_noutrefresh
- ncurses_slk_refresh
- ncurses_slk_restore
- ncurses_slk_set
- ncurses_slk_touch
- ncurses_standend
- ncurses_standout
- ncurses_start_color
- ncurses_termattrs
- ncurses_termname
- ncurses_timeout
- ncurses_top_panel
- ncurses_typeahead
- ncurses_ungetch
- ncurses_ungetmouse
- ncurses_update_panels
- ncurses_use_default_colors
- ncurses_use_env
- ncurses_use_extended_names
- ncurses_vidattr
- ncurses_vline
- ncurses_waddch
- ncurses_waddstr
- ncurses_wattroff
- ncurses_wattron
- ncurses_wattrset
- ncurses_wborder
- ncurses_wclear
- ncurses_wcolor_set
- ncurses_werase
- ncurses_wgetch
- ncurses_whline
- ncurses_wmouse_trafo
- ncurses_wmove
- ncurses_wnoutrefresh
- ncurses_wrefresh
- ncurses_wstandend
- ncurses_wstandout
- ncurses_wvline
Коментарии
A custom php ncurses_getstr function..
<?php
function ncurses_getstr($strlen){
for ($x=0;$x<$strlen;$x++){
$string .= chr(ncurses_getch());
}
return $string;
}
ncurses_init();
ncurses_addstr(ncurses_getstr(6));
ncurses_refresh();
ncurses_getch();
ncurses_end();
?>
When using getch to capture KEY_* events remember that the keypad is arranged like this:
+-----+------+-------+
| A1 | up | A3 |
+-----+------+-------+
|left | B2 | right |
+-----+------+-------+
| C1 | down | C3 |
+-----+------+-------+
You use has_key to capture these and act upon them.
man curs_getch for more info.
After banging my head over this for awhile, I discovered, you must use ncurses_keypad($window, true); to enable the arrow keys and f keys to work correctly.
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
}
?>
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.