Here is the code I use to convert a keypress to an indexed text (the only appropriate I7 type, unfortunately) under Glulx:
[code]To decide which indexed text is (N - a number) resolved to an/-- indexed text:
if (N > 31 and N < 127) or (N > 160 and N < 384):[i.e., we have received printable input]
decide on “[char-code (N)]”;
otherwise:
decide on “”.
To say char-code (N - a number):
(- print (char) {N}; -)[/code]
The decide phrase takes a number (presumably a character code returned by VM_KeyChar) and returns that code as an indexed text if it’s found in the ASCII or Latin Extended A character sets. If the player has entered something else, whether an exotic character or a control key (e.g. return or arrow key), it returns an empty string. The same code works under z-code, though maybe there are interpreters that won’t allow character codes from the higher range (161 - 383)?
Anyway, you could build on Basic Screen Effects’s undocumented capabilities by adding something like this:
[code]Keychar is a number variable.
Keypress is an indexed text variable.
To get char input:
now keychar is the chosen letter;
now keypress is keychar resolved to an indexed text.[/code]
This gives the author access to the input in two forms. If we are interested in control key input (e.g., arrow keys), we can use the keychar variable to check against those codes, e.g.:
To decide whether (keychar - a number) is an arrow key:
if keychar > -6 and keychar < -1, decide yes;
decide no.
For printable input, we can just compare textually:
When play begins:
while keypress is "":
say "Press any key and I'll tell you what you pressed.";
get char input;
say "You typed: [keypress] (character code [keychar]).";
if keypress is "":
say "You pressed a control key. That's cheating. Try again![paragraph break]";
if keypress is "a" or keypress is "A":
say "Good work, 'a' is the first letter of the alphabet!"
So, the basics of an author-friendly approach to char input are pretty simple. A full solution would probably have a number of decision phrases for different types of key, as well as a table of nonprintable input so that the author wouldn’t have to refer directly to character code numbers.
Anybody feel like carrying it the rest of the way?
–Erik