The HTML version of this RfD is
<http://www.forth200x.org/ekeys.html>
Problem
How do I write a portable Forth program that reacts to key presses of cursor
keys (and possibly other keys not represented by ASCII), e.g., an editor?
Proposal
The following words produce the same values that EKEY produces when the user
presses the corresponding key.
K-LEFT ( -- u ) facility ext
The "cursor left" key
K-RIGHT ( -- u ) facility ext
The "cursor right" key
K-UP ( -- u ) facility ext
The "cursor up" key
K-DOWN ( -- u ) facility ext
The "cursor down" key
K-HOME ( -- u ) facility ext
The "home" or "Pos1" key
K-END ( -- u ) facility ext
The "End" key
K-PRIOR ( -- u ) facility ext
The "PgUp" or "Prior" key
K-NEXT ( -- u ) facility ext
The "PgDn" or "Next" key
Typical Use
... ekey case
k-up of ... endof
k-down of ... endof
...
endcase
Remarks
Other Keys
Gforth and PFE also have words K1...K10 for the function/keypad keys;
they also contain S-K1...S-K10 for shifted function/keypad keys, but
they don't work as widely. Moreover, Gforth (but not PFE) also has
K-INSERT K-DELETE K11 K12 S-K11 S-K12. Should any of these words be
added to this proposal?
Experience
These words have been implemented for several years in PFE and
Gforth. They are not used much; however, several editors have been
published in ANS Forth that would have profited from these
words. Also, the original version of the MiniSpreadsheet
<http://wiki.forthfreak.net/index.cgi?MiniSpreadsheet> hardcoded the
values for the keys for one platform; I then fixed it to use the words
proposed above.
Implementation and Tests
The implementation is closely tied to the implementation of EKEY, and
therefore unportable, so I don't provide a reference
implementation. However, you can look at the Gforth implementation of
EKEY and these words (based on ANSI terminal escape sequences)
<http://b2.complang.tuwien.ac.at/cgi-bin/cvsweb/~checkout~/gforth/ekey.fs?rev=HE\
AD;content-type=text%2Fplain;cvsroot=gforth>.
Tests <http://www.forth200x.org/tests/ekeys.fs>
- anton