| Title: | Wait for a Key Press in a Terminal |
|---|---|
| Description: | Wait for a single key press at the 'R' prompt. This works in terminals, but does not currently work in the 'Windows' 'GUI', the 'macOS' 'GUI' ('R.app'), in 'Emacs' 'ESS', in an 'Emacs' shell buffer or in 'R Studio'. In these cases 'keypress' stops with an error message. |
| Authors: | Gábor Csárdi [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-7098-9676>), Jon Griffiths [aut], Posit Software, PBC [cph, fnd] (ROR: <https://ror.org/03wc8by49>) |
| Maintainer: | Gábor Csárdi <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.3.2.9000 |
| Built: | 2026-05-29 21:57:39 UTC |
| Source: | https://github.com/gaborcsardi/keypress |
Check if the current platform/terminal supports reading single keys.
has_keypress_support()has_keypress_support()
Supported platforms:
Terminals in Windows and Unix.
RStudio terminal.
Not supported:
RStudio (if not in the RStudio terminal).
R.app on macOS.
Rgui on Windows.
Emacs ESS.
Others.
Whether there is support for waiting for individual keypressses.
Other keypress function:
keypress()
has_keypress_support()has_keypress_support()
It currently only works at Linux/Unix and macOS terminals,
and at the Windows command line. see has_keypress_support.
keypress(block = TRUE, timeout = Inf)keypress(block = TRUE, timeout = Inf)
block |
Whether to wait for a key press, if there is none available now. |
timeout |
Maximum number of seconds to wait for a key press, if
|
The following special keys are supported:
Arrow keys: 'up', 'down', 'right', 'left'.
Function keys: from 'f1' to 'f12'.
Others: 'home', 'end', 'insert', 'delete', 'pageup', 'pagedown', 'tab', 'enter', 'backspace' (same as 'delete' on macOS keyboards), 'escape'.
Control with one of the following keys: 'a', 'b', 'c', 'd', 'e', 'f', 'h', 'k', 'l', 'n', 'p', 't', 'u', 'w'.
The key pressed, a character scalar. NA is returned if no
key is available: for non-blocking reads, or when a blocking read
times out.
Other keypress function:
has_keypress_support()
x <- keypress() cat("You pressed key", x, "\n") # Wait at most five seconds for a key press x <- keypress(timeout = 5) if (is.na(x)) cat("No key pressed\n") else cat("You pressed key", x, "\n")x <- keypress() cat("You pressed key", x, "\n") # Wait at most five seconds for a key press x <- keypress(timeout = 5) if (is.na(x)) cat("No key pressed\n") else cat("You pressed key", x, "\n")
For Linux/Unix and macOS terminals, suppress key echoes from the terminal.
without_echo(expr)without_echo(expr)
expr |
Expression to evaluate without terminal echo. |
You will need to set echo to FALSE if you want the R script to handle all of the keypress-related behaviour, without any keys being echoed in the terminal by the operating system, when using non-blocking keypress. This is not necessary when running in a Windows command prompt, and will be safely ignored.