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
21 generalized handy functions for working with terminals. One of the main
22 goals is to have the functions as portable as possible, so you can just
23 plug in "use Term::ReadKey" on any architecture and have a good
24 likelihood of it working.
25
26 Version 2.30.01: Added handling of arrows, page up/down, home/end,
27 insert/delete keys under Win32. These keys emit xterm-compatible
28 sequences. Works with Term::ReadLine::Perl.
29
30 ReadMode MODE [, Filehandle]
31 Takes an integer argument or a string synonym (case insensitive),
32 which can currently be one of the following values:
33
34 INT SYNONYM DESCRIPTION
35
36 0 'restore' Restore original settings.
37
38 1 'normal' Change to what is commonly the default mode,
39 echo on, buffered, signals enabled, Xon/Xoff
40 possibly enabled, and 8-bit mode possibly disabled.
41
42 2 'noecho' Same as 1, just with echo off. Nice for
43 reading passwords.
44
45 3 'cbreak' Echo off, unbuffered, signals enabled, Xon/Xoff
46 possibly enabled, and 8-bit mode possibly enabled.
47
48 4 'raw' Echo off, unbuffered, signals disabled, Xon/Xoff
49 disabled, and 8-bit mode possibly disabled.
50
51 5 'ultra-raw' Echo off, unbuffered, signals disabled, Xon/Xoff
52 disabled, 8-bit mode enabled if parity permits,
53 and CR to CR/LF translation turned off.
54
55 These functions are automatically applied to the STDIN handle if no
56 other handle is supplied. Modes 0 and 5 have some special
57 properties worth mentioning: not only will mode 0 restore original
58 settings, but it cause the next ReadMode call to save a new set of
59 default settings. Mode 5 is similar to mode 4, except no CR/LF
60 translation is performed, and if possible, parity will be disabled
61 (only if not being used by the terminal, however. It is no
62 different from mode 4 under Windows.)
63
64 If you just need to read a key at a time, then modes 3 or 4 are
65 probably sufficient. Mode 4 is a tad more flexible, but needs a bit
66 more work to control. If you use ReadMode 3, then you should
67 install a SIGINT or END handler to reset the terminal (via ReadMode
68 0) if the user aborts the program via "^C". (For any mode, an END
69 handler consisting of "ReadMode 0" is actually a good idea.)
70
71 If you are executing another program that may be changing the
72 terminal mode, you will either want to say
73
74 ReadMode 1; # same as ReadMode 'normal'
75 system('someprogram');
76 ReadMode 1;
77
78 which resets the settings after the program has run, or:
79
80 $somemode=1;
81 ReadMode 0; # same as ReadMode 'restore'
82 system('someprogram');
83 ReadMode 1;
84
85 which records any changes the program may have made, before
86 resetting the mode.
87
88 ReadKey MODE [, Filehandle]
89 Takes an integer argument, which can currently be one of the
90 following values:
91
92 0 Perform a normal read using getc
93 -1 Perform a non-blocked read
94 >0 Perform a timed read
95
96 If the filehandle is not supplied, it will default to STDIN. If
97 there is nothing waiting in the buffer during a non-blocked read,
98 then undef will be returned. In most situations, you will probably
99 want to use "ReadKey -1".
100
101 NOTE that if the OS does not provide any known mechanism for non-
102 blocking reads, then a "ReadKey -1" can die with a fatal error.
103 This will hopefully not be common.
104
105 If MODE is greater then zero, then ReadKey will use it as a timeout
106 value in seconds (fractional seconds are allowed), and won't return
107 "undef" until that time expires.
108
109 NOTE, again, that some OS's may not support this timeout behaviour.
110
111 If MODE is less then zero, then this is treated as a timeout of
112 zero, and thus will return immediately if no character is waiting.
113 A MODE of zero, however, will act like a normal getc.
114
115 NOTE, there are currently some limitations with this call under
116 Windows. It may be possible that non-blocking reads will fail when
117 reading repeating keys from more then one console.
118
119 ReadLine MODE [, Filehandle]
120 Takes an integer argument, which can currently be one of the
121 following values:
122
123 0 Perform a normal read using scalar(<FileHandle>)
124 -1 Perform a non-blocked read
125 >0 Perform a timed read
126
127 If there is nothing waiting in the buffer during a non-blocked
128 read, then undef will be returned.
129
130 NOTE, that if the OS does not provide any known mechanism for non-
131 blocking reads, then a "ReadLine 1" can die with a fatal error.
132 This will hopefully not be common.
133
134 NOTE that a non-blocking test is only performed for the first
135 character in the line, not the entire line. This call will
136 probably not do what you assume, especially with "ReadMode" MODE
137 values higher then 1. For example, pressing Space and then
138 Backspace would appear to leave you where you started, but any
139 timeouts would now be suspended.
140
141 This call is currently not available under Windows.
142
143 GetTerminalSize [Filehandle]
144 Returns either an empty array if this operation is unsupported, or
145 a four element array containing: the width of the terminal in
146 characters, the height of the terminal in character, the width in
147 pixels, and the height in pixels. (The pixel size will only be
148 valid in some environments.)
149
150 NOTE, under Windows, this function must be called with an output
151 filehandle, such as "STDOUT", or a handle opened to "CONOUT$".
152
153 SetTerminalSize WIDTH,HEIGHT,XPIX,YPIX [, Filehandle]
154 Return -1 on failure, 0 otherwise.
155
156 NOTE that this terminal size is only for informative value, and
157 changing the size via this mechanism will not change the size of
158 the screen. For example, XTerm uses a call like this when it
159 resizes the screen. If any of the new measurements vary from the
160 old, the OS will probably send a SIGWINCH signal to anything
161 reading that tty or pty.
162
163 This call does not work under Windows.
164
165 GetSpeed [, Filehandle]
166 Returns either an empty array if the operation is unsupported, or a
167 two value array containing the terminal in and out speeds, in
168 decimal. E.g, an in speed of 9600 baud and an out speed of 4800
169 baud would be returned as (9600,4800). Note that currently the in
170 and out speeds will always be identical in some OS's.
171
172 No speeds are reported under Windows.
173
174 GetControlChars [, Filehandle]
175 Returns an array containing key/value pairs suitable for a hash.
176 The pairs consist of a key, the name of the control
177 character/signal, and the value of that character, as a single
178 character.
179
180 This call does nothing under Windows.
181
182 Each key will be an entry from the following list:
183
184 DISCARD
185 DSUSPEND
186 EOF
187 EOL
188 EOL2
189 ERASE
190 ERASEWORD
191 INTERRUPT
192 KILL
193 MIN
194 QUIT
195 QUOTENEXT
196 REPRINT
197 START
198 STATUS
199 STOP
200 SUSPEND
201 SWITCH
202 TIME
203
204 Thus, the following will always return the current interrupt
205 character, regardless of platform.
206
207 %keys = GetControlChars;
208 $int = $keys{INTERRUPT};
209
210 SetControlChars [, Filehandle]
211 Takes an array containing key/value pairs, as a hash will produce.
212 The pairs should consist of a key that is the name of a legal
213 control character/signal, and the value should be either a single
214 character, or a number in the range 0-255. SetControlChars will die
215 with a runtime error if an invalid character name is passed or
216 there is an error changing the settings. The list of valid names is
217 easily available via
218
219 %cchars = GetControlChars();
220 @cnames = keys %cchars;
221
222 This call does nothing under Windows.
223
225 Kenneth Albanowski <kjahds@kjahds.com>
226
227 Currently maintained by Jonathan Stowe <jns@gellyfish.co.uk>
228
230 The code is maintained at
231
232 https://github.com/jonathanstowe/TermReadKey
233
234 Please feel free to fork and suggest patches.
235
237 Prior to the 2.31 release the license statement was:
238
239 Copyright (C) 1994-1999 Kenneth Albanowski.
240 2001-2005 Jonathan Stowe and others
241
242 Unlimited distribution and/or modification is allowed as long as this
243 copyright notice remains intact.
244
245 And was only stated in the README file.
246
247 Because I believe the original author's intent was to be more open than
248 the other commonly used licenses I would like to leave that in place.
249 However if you or your lawyers require something with some more words
250 you can optionally choose to license this under the standard Perl
251 license:
252
253 This module is free software; you can redistribute it and/or modify it
254 under the terms of the Artistic License. For details, see the full
255 text of the license in the file "Artistic" that should have been provided
256 with the version of perl you are using.
257
258 This program is distributed in the hope that it will be useful, but
259 without any warranty; without even the implied warranty of merchantability
260 or fitness for a particular purpose.
261
262
263
264perl v5.36.0 2023-01-20 ReadKey(3)