Skip to content

Repository files navigation

keypress

Read a single key press from the terminal

R-CMD-check CRAN RStudio mirror downloads Codecov test coverage

Installation

install.packages("keypress")

Usage

> has_keypress_support()
[1] TRUE
> x <- keypress()
> cat("You pressed key", x, "\n")
You pressed key h

Support

This is unfortunately impossible to do in

  • RStudio,
  • Positron console,
  • R.app (the builtin macOS GUI),
  • RGui (the builtin Windows GUI),
  • Emacs ESS, or
  • if stdin() is not a terminal.

In other words, it pretty much only works interactively, in a terminal. The terminal can be any Linux, macOS, or other Unix terminal or Windows cmd (also called RTerm in R) or Windows terminal.

Features

  • Blocking or non-blocking operation.
  • Interruptible.
  • Read a key with a timeout.
  • UTF-8 support.

Keys

Note that not all keys are supported, in particular most of the modifiers (alt, shift, etc.) are not. Supported special keys are:

  • Arrow keys: up, down, right, left. Available on most keyboards.
  • Function keys: from f1 to f12. Available on most keyboards.
  • Other special keys: enter, backspace, escape, tab.
  • Some key combinations with the CONTROL key: ctrl-a, ctrl-b, ctrl-c, ctrl-d, ctrl-e, ctrl-f, ctrl-h, ctrl-k, ctrl-l, ctrl-n, ctrl-p, ctrl-t, ctrl-u, ctrl-w.
  • Keys typically not availale on macOS keyboards: home, end, insert, delete, pageup, pagedown.
  • Unicode characters (graphemes) are returned UTF-8 encoded.

License

MIT © Gábor Csárdi

About

Read individual keys in R without waiting for an ENTER

Resources

Stars

23 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages