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?.
Receive (but do not display) one byte c.
Receive (but do not display) one byte c. In case of an error or interrupt, return the negative ior instead.
If a byte is available for receiving with key, return true,
otherwise false.
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.
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
Receive a keyboard event u (encoding implementation-defined).
Convert keyboard event u into extended char xc. If that is possible, return xt and true, otherise u and false.
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.
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.
If a keyboard event is available for receiving with ekey,
return true, otherwise false.
The key identifiers for cursor keys are:
aka Pos1
aka PgUp
aka PgDn
the DEL key on my xterm, not backspace
The key identifiers for function keys (aka keypad keys) are:
Note that k-f11 and k-f12 are not as widely available.
You can combine these key identifiers with masks for various shift keys:
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:
Moreover, there are the following key codes for keys and other events:
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.
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:
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.
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.