- Unix Commands Reference
- Unix - Tutorial Home
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
getkey - Unix, Linux Command
NAMEgetkey - wait until a key is pressed
SYNOPSISgetkey [OPTION]... [KEYS]
DESCRIPTIONgetkey waits until one of KEYS is pressed. If KEYS are not specified, any key is accepted. KEYS are matched case-insensitive.
EXIT STATUSgetkey exits with status 0 if one of the expected keys is pressed. If invalid arguments are specified, getkey exits with status 255. If getkey is interrupted or the wait times out, getkey exits with other non-zero status.
|-c, --wait SECONDS|
Wait only for
The default is 0, which means to wait without a time limit.
Dont treat Ctrl+C and Ctrl+D specially.
When this option is not specified, these characters interrupt getkey.
|-m, --message MESSAGE|
MESSAGE while waiting.
The message is used as a format string in
with a single argument, the number of seconds left.
Typical usage is therefore
"Press a key within %d seconds to ...".
MESSAGE contains other format string directives, the behavior is undefined and
getkey may crash.
If there is no time limit specified, the number of seconds left is reported as 0.
1. Without any arguments
$ getkey a$
The command waits until any key pressed and comes out immediately once the key is pressed
2. Wait for key input for certain seconds3>
$ getkey -c 5
$ getkey -c 5
In the first example the command waits for 5 seconds for key press and comes out even if no key is pressed. In the second example the command comes out immediately after key is pressed.
3. Show dynamic message
$ getkey -c 5 -m "Press key within %d seconds" Press key within 5 seconds $ getkey -c 5 -m "Press key within %d seconds" Press key within 4 seconds ... ... $ getkey -c 5 -m "Press key within %d seconds" Press key within 1 seconds $ getkey -c 5 -m "Press key within %d seconds" Press key within 0 seconds$
In the above example the command displays a message where the number of seconds keep decrementing till it reaches 0 or key is pressed.