6.24.6 Single-key input

If you want to get a single byte, you can use key; to check whether a character is available for key, you can use key?.

key ( – c  ) core

Receive (but do not display) one byte c.

key-ior ( – c|ior  ) gforth-1.0

Receive (but do not display) one byte c. In case of an error or interrupt, return the negative ior instead.

key? ( – flag  ) facility “key-question”

If a byte is available for receiving with key, return true, otherwise false.

xkey ( – xc  ) xchar “x-key”

Reads an extended character xc xchar from the terminal without printing it. This will discard all input events until all bytes of xc have been received.

xkey? ( – flag  ) xchar “x-key-query”

Ideally this word would return true if a complete extended char is available for input, otherwise false. Unfortunately, currently also a partial extended character results in returning true.

If you want to process a mix of printable and non-printable characters, you can do that with ekey and friends. Ekey produces a keyboard event that you have to convert into a character with ekey>char or into a key identifier with ekey>fkey.

Typical code for using EKEY looks like this:

ekey ekey>xchar if ( xc )
  ... \ do something with the character
else ekey>fkey if ( key-id )
  case
    k-up                                  of ... endof
    k-f1                                  of ... endof
    k-left k-shift-mask or k-ctrl-mask or of ... endof
    ...
  endcase
else ( keyboard-event )
  drop \ just ignore an unknown keyboard event type
then then
ekey ( – u  ) facility-ext “e-key”

Receive a keyboard event u (encoding implementation-defined).

ekey>xchar ( u – u false | xc true  ) xchar-ext “e-key-to-x-char”

Convert keyboard event u into extended char xc. If that is possible, return xt and true, otherise u and false.

ekey>char ( u – u false | c true  ) facility-ext “e-key-to-char”

Convert keyboard event u into the ASCII char c. If that is possible, return c and true, otherise u and false. Instead of ekey>char, use ekey>xchar if available.

ekey>fkey ( u1 – u2 f  ) facility-ext “e-key-to-f-key”

If u1 is a keyboard event in the special key set, convert keyboard event u1 into key id u2 and return true; otherwise return u1 and false.

ekey? ( – flag  ) facility-ext “e-key-question”

If a keyboard event is available for receiving with ekey, return true, otherwise false.

The key identifiers for cursor keys are:

k-left ( – u  ) facility-ext
k-right ( – u  ) facility-ext
k-up ( – u  ) facility-ext
k-down ( – u  ) facility-ext
k-home ( – u  ) facility-ext

aka Pos1

k-end ( – u  ) facility-ext
k-prior ( – u  ) facility-ext

aka PgUp

k-next ( – u  ) facility-ext

aka PgDn

k-insert ( – u  ) facility-ext
k-delete ( – u  ) facility-ext

the DEL key on my xterm, not backspace

The key identifiers for function keys (aka keypad keys) are:

k-f1 ( – u  ) facility-ext “k-f-1”
k-f2 ( – u  ) facility-ext “k-f-2”
k-f3 ( – u  ) facility-ext “k-f-3”
k-f4 ( – u  ) facility-ext “k-f-4”
k-f5 ( – u  ) facility-ext “k-f-5”
k-f6 ( – u  ) facility-ext “k-f-6”
k-f7 ( – u  ) facility-ext “k-f-7”
k-f8 ( – u  ) facility-ext “k-f-8”
k-f9 ( – u  ) facility-ext “k-f-9”
k-f10 ( – u  ) facility-ext “k-f-10”
k-f11 ( – u  ) facility-ext “k-f-11”
k-f12 ( – u  ) facility-ext “k-f-12”

Note that k-f11 and k-f12 are not as widely available.

You can combine these key identifiers with masks for various shift keys:

k-shift-mask ( – u  ) facility-ext
k-ctrl-mask ( – u  ) facility-ext
k-alt-mask ( – u  ) facility-ext

There are a number of keys that have ASCII values, and therefore are unlikely to be reported as special keys, but the combination of these keys with shift keys may be reported as a special key:

k-enter ( – u  ) gforth-1.0
k-backspace ( – u  ) gforth-1.0
k-tab ( – u  ) gforth-1.0

Moreover, there are the following key codes for keys and other events:

k-winch ( – u  ) gforth-1.0

This key code may be generated when the user changes the window size; if you have cached the value returned by form, this indicates that you should update your cache.

k-pause ( – u  ) gforth-1.0
k-mute ( – u  ) gforth-1.0
k-volup ( – u  ) gforth-1.0
k-voldown ( – u  ) gforth-1.0
k-sel ( – u  ) gforth-1.0
k-eof ( – u  ) gforth-1.0

Note that, even if a Forth system has ekey>fkey and the key identifier words, the keys are not necessarily available or it may not necessarily be able to report all the keys and all the possible combinations with shift masks. Therefore, write your programs in such a way that they are still useful even if the keys and key combinations cannot be pressed or are not recognized.

Examples: Older keyboards often do not have an F11 and F12 key. If you run Gforth in an xterm, the xterm catches a number of combinations (e.g., Shift-Up), and never passes it to Gforth. Finally, Gforth currently does not recognize and report combinations with multiple shift keys (so the shift-ctrl-left case in the example above would never be entered).

Gforth recognizes various keys available on ANSI terminals (in MS-DOS you need the ANSI.SYS driver to get that behaviour); it works by recognizing the escape sequences that ANSI terminals send when such a key is pressed. If you have a terminal that sends other escape sequences, you will not get useful results on Gforth. Other Forth systems may work in a different way.

Gforth also provides a few words for outputting names of function keys:

fkey. ( u –  ) gforth-1.0 “fkey-dot”

Print a string representation for the function key u. U must be a function key (possibly with modifier masks), otherwise there may be an exception.

simple-fkey-string ( u1 – c-addr u  ) gforth-1.0

c-addr u is the string name of the function key u1. Only works for simple function keys without modifier masks. Any u1 that does not correspond to a simple function key currently produces an exception.