1ReadKey(3) User Contributed Perl Documentation ReadKey(3)
2
3
4
6 Term::ReadKey - A perl module for simple terminal control
7
9 use Term::ReadKey;
10 ReadMode 4; # Turn off controls keys
11 while (not defined ($key = ReadKey(-1))) {
12 # No key yet
13 }
14 print "Get key $key\n";
15 ReadMode 0; # Reset tty mode before exiting
16
18 Term::ReadKey is a compiled perl module dedicated to providing simple
19 control over terminal driver modes (cbreak, raw, cooked, etc.,) support
20 for non-blocking reads, if the architecture allows, and some general‐
21 ized handy functions for working with terminals. One of the main goals
22 is to have the functions as portable as possible, so you can just plug
23 in "use Term::ReadKey" on any architecture and have a good likelyhood
24 of it working.
25
26 ReadMode MODE [, Filehandle]
27 Takes an integer argument, which can currently be one of the
28 following values:
29
30 0 Restore original settings.
31 1 Change to cooked mode.
32 2 Change to cooked mode with echo off.
33 (Good for passwords)
34 3 Change to cbreak mode.
35 4 Change to raw mode.
36 5 Change to ultra-raw mode.
37 (LF to CR/LF translation turned off)
38
39 Or, you may use the synonyms:
40
41 restore
42 normal
43 noecho
44 cbreak
45 raw
46 ultra-raw
47
48 These functions are automatically applied to the STDIN handle
49 if no other handle is supplied. Modes 0 and 5 have some special
50 properties worth mentioning: not only will mode 0 restore orig‐
51 inal settings, but it cause the next ReadMode call to save a
52 new set of default settings. Mode 5 is similar to mode 4,
53 except no CR/LF translation is performed, and if possible, par‐
54 ity will be disabled (only if not being used by the terminal,
55 however. It is no different from mode 4 under Windows.)
56
57 If you are executing another program that may be changing the
58 terminal mode, you will either want to say
59
60 ReadMode 1
61 system('someprogram');
62 ReadMode 1;
63
64 which resets the settings after the program has run, or:
65
66 $somemode=1;
67 ReadMode 0;
68 system('someprogram');
69 ReadMode 1;
70
71 which records any changes the program may have made, before
72 resetting the mode.
73
74 ReadKey MODE [, Filehandle]
75 Takes an integer argument, which can currently be one of the
76 following values:
77
78 0 Perform a normal read using getc
79 -1 Perform a non-blocked read
80 >0 Perform a timed read
81
82 (If the filehandle is not supplied, it will default to STDIN.)
83 If there is nothing waiting in the buffer during a non-blocked
84 read, then undef will be returned. Note that if the OS does not
85 provide any known mechanism for non-blocking reads, then a
86 "ReadKey -1" can die with a fatal error. This will hopefully
87 not be common.
88
89 If MODE is greater then zero, then ReadKey will use it as a
90 timeout value in seconds (fractional seconds are allowed), and
91 won't return "undef" until that time expires. (Note, again,
92 that some OS's may not support this timeout behaviour.) If MODE
93 is less then zero, then this is treated as a timeout of zero,
94 and thus will return immediately if no character is waiting. A
95 MODE of zero, however, will act like a normal getc.
96
97 There are currently some limitations with this call under Win‐
98 dows. It may be possible that non-blocking reads will fail when
99 reading repeating keys from more then one console.
100
101 ReadLine MODE [, Filehandle]
102 Takes an integer argument, which can currently be one of the
103 following values:
104
105 0 Perform a normal read using scalar(<FileHandle>)
106 -1 Perform a non-blocked read
107 >0 Perform a timed read
108
109 If there is nothing waiting in the buffer during a non-blocked
110 read, then undef will be returned. Note that if the OS does not
111 provide any known mechanism for non-blocking reads, then a
112 "ReadLine 1" can die with a fatal error. This will hopefully
113 not be common. Note that a non-blocking test is only performed
114 for the first character in the line, not the entire line. This
115 call will probably not do what you assume, especially with
116 ReadMode's higher then 1. For example, pressing Space and then
117 Backspace would appear to leave you where you started, but any
118 timeouts would now be suspended.
119
120 This call is currently not available under Windows.
121
122 GetTerminalSize [Filehandle]
123 Returns either an empty array if this operation is unsupported,
124 or a four element array containing: the width of the terminal
125 in characters, the height of the terminal in character, the
126 width in pixels, and the height in pixels. (The pixel size will
127 only be valid in some environments.)
128
129 Under Windows, this function must be called with an "output"
130 filehandle, such as STDOUT, or a handle opened to CONOUT$.
131
132 SetTerminalSize WIDTH,HEIGHT,XPIX,YPIX [, Filehandle]
133 Return -1 on failure, 0 otherwise. Note that this terminal size
134 is only for informative value, and changing the size via this
135 mechanism will not change the size of the screen. For example,
136 XTerm uses a call like this when it resizes the screen. If any
137 of the new measurements vary from the old, the OS will probably
138 send a SIGWINCH signal to anything reading that tty or pty.
139
140 This call does not work under Windows.
141
142 GetSpeeds [, Filehandle]
143 Returns either an empty array if the operation is unsupported,
144 or a two value array containing the terminal in and out speeds,
145 in decimal. E.g, an in speed of 9600 baud and an out speed of
146 4800 baud would be returned as (9600,4800). Note that currently
147 the in and out speeds will always be identical in some OS's. No
148 speeds are reported under Windows.
149
150 GetControlChars [, Filehandle]
151 Returns an array containing key/value pairs suitable for a
152 hash. The pairs consist of a key, the name of the control char‐
153 acter/signal, and the value of that character, as a single
154 character. This call does nothing under Windows.
155
156 Each key will be an entry from the following list:
157
158 DISCARD
159 DSUSPEND
160 EOF
161 EOL
162 EOL2
163 ERASE
164 ERASEWORD
165 INTERRUPT
166 KILL
167 MIN
168 QUIT
169 QUOTENEXT
170 REPRINT
171 START
172 STATUS
173 STOP
174 SUSPEND
175 SWITCH
176 TIME
177
178 Thus, the following will always return the current interrupt
179 character, regardless of platform.
180
181 %keys = GetControlChars;
182 $int = $keys{INTERRUPT};
183
184 SetControlChars [, Filehandle]
185 Takes an array containing key/value pairs, as a hash will pro‐
186 duce. The pairs should consist of a key that is the name of a
187 legal control character/signal, and the value should be either
188 a single character, or a number in the range 0-255. SetCon‐
189 trolChars will die with a runtime error if an invalid character
190 name is passed or there is an error changing the settings. The
191 list of valid names is easily available via
192
193 %cchars = GetControlChars();
194 @cnames = keys %cchars;
195
196 This call does nothing under Windows.
197
199 Kenneth Albanowski <kjahds@kjahds.com>
200
201 Currently maintained by Jonathan Stowe <jns@gellyfish.com>
202
203
204
205perl v5.8.8 2005-01-11 ReadKey(3)