Most applications and popdowns provide a system of menus and commands that the user can access at any time the application is expecting keyboard input. The way the menus themselves are generated will be discussed later; for now, we need to decide how to react to the user selecting a particular command.
When the user selects a command, a command code is generated. CamelForth generally ignores these codes, as they are not proper key codes, but you can intercept them if you intend to support menus and commands.
The method is very simple: when a command code is received, CamelForth executes one of the deferred words (KEY) or (ACC_EVT) (depending upon the input word currently in use) with an event code on top of the stack. This event code is simply 256 plus the command code. Therefore, you can define a word to deal with all the commands you are implementing, and assign it to the two deferred words.
For our example application, I have decided to provide two commands: Quit (to which I will assign command code 128) and Restart (to which I will assign command code 129). Any other codes that are received will be rejected with a beep.
The code we need to implement these commands is as follows:
: COMMAND-KEYS ( eventcode -- ) 256 - \ convert to command code CASE 128 OF BYE ENDOF \ the Quit command 129 OF COLD ENDOF \ the Restart command 7 EMIT \ beep on other codes ENDCASE ; ' COMMAND-KEYS IS (ACC_EVT) \ assign to the deferred words ' COMMAND-KEYS IS (KEY)
Next section: Creating the application DOR
Previous section: Dealing with OZ errors