Term::ReadKey - A perl module for simple terminal control
use Term::ReadKey; ReadMode 4; # Turn off controls keys while (not defined ($key = ReadKey(-1))) { # No key yet } print "Get key $key\n"; ReadMode 0; # Reset tty mode before exiting
0 Restore original settings. 1 Change to cooked mode. 2 Change to cooked mode with echo off. (Good for passwords) 3 Change to cbreak mode. 4 Change to raw mode. 5 Change to ultra-raw mode. (LF to CR/LF translation turned off)
Or, you may use the synonyms:
restore normal noecho cbreak raw ultra-raw
These functions are automatically applied to the STDIN handle if no other handle is supplied. Modes 0 and 5 have some special properties worth mentioning: not only will mode 0 restore original settings, but it cause the next ReadMode call to save a new set of default settings. Mode 5 is similar to mode 4, except no CR/LF translation is performed, and if possible, parity will be disabled (only if not being used by the terminal, however. It is no different from mode 4 under Windows.)
If you are executing another program that may be changing the terminal mode, you will either want to say
ReadMode 1 system('someprogram'); ReadMode 1;
which resets the settings after the program has run, or:
$somemode=1; ReadMode 0; system('someprogram'); ReadMode 1;
which records any changes the program may have made, before resetting the mode.
0 Perform a normal read using getc -1 Perform a non-blocked read >0 Perform a timed read
(If the filehandle is not supplied, it will default to STDIN.) If there is nothing waiting in the buffer during a non-blocked read, then undef will be returned. Note that if the OS does not provide any known mechanism for non-blocking reads, then a "ReadKey -1" can die with a fatal error. This will hopefully not be common.
If MODE is greater then zero, then ReadKey will use it as a timeout value in seconds (fractional seconds are allowed), and won't return "undef" until that time expires. (Note, again, that some OS's may not support this timeout behaviour.) If MODE is less then zero, then this is treated as a timeout of zero, and thus will return immediately if no character is waiting. A MODE of zero, however, will act like a normal getc.
There are currently some limitations with this call under Windows. It may be possible that non-blocking reads will fail when reading repeating keys from more then one console.
0 Perform a normal read using scalar(<FileHandle>) -1 Perform a non-blocked read >0 Perform a timed read
If there is nothing waiting in the buffer during a non-blocked read, then undef will be returned. Note that if the OS does not provide any known mechanism for non-blocking reads, then a "ReadLine 1" can die with a fatal error. This will hopefully not be common. Note that a non-blocking test is only performed for the first character in the line, not the entire line. This call will probably not do what you assume, especially with ReadMode's higher then 1. For example, pressing Space and then Backspace would appear to leave you where you started, but any timeouts would now be suspended.
This call is currently not available under Windows.
Under Windows, this function must be called with an ``output'' filehandle, such as STDOUT, or a handle opened to CONOUT$.
This call does not work under Windows.
Each key will be an entry from the following list:
DISCARD DSUSPEND EOF EOL EOL2 ERASE ERASEWORD INTERRUPT KILL MIN QUIT QUOTENEXT REPRINT START STATUS STOP SUSPEND SWITCH TIME
Thus, the following will always return the current interrupt character, regardless of platform.
%keys = GetControlChars; $int = $keys{INTERRUPT};
%cchars = GetControlChars(); @cnames = keys %cchars;
This call does nothing under Windows.
Currently maintained by Jonathan Stowe <jns@gellyfish.com>
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |