Have.key
The have.key command returns TRUE if the the value of reserved variable v19 > 0.
Syntax
have.key()
Remarks
Test commands are only valid in an if
statement.
While this command is often described as detecting the presence of key presses, it is more accurate to state that the have.key command returns TRUE if the value of reserved variable v19 > 0.
AGI first checks v19; if it is greater than 0, it immediately returns TRUE. If not, AGI then checks the keyboard buffer; if an ASCII value is in the buffer, that value is copied to v19, and then has.key returns TRUE. If no ASCII value is found in the buffer, have.key returns FALSE. Only ASCII values will trigger the have.key command; scan codes (such as function keys or ALT key combos) will not. Also, AGI does not reset the value of v19 after calling have.key; it will continue to return TRUE until v19 is reset to zero at the beginning of the next interpreter cycle (or if reset manually by logic code).
If you manually assign a non-zero value to v19 prior to calling the have.key command, it will return TRUE even though no key was actually pressed.
This statement can be combined with the NOT
operator to test for no keys having been pressed.
Possible Errors
When calling have.key as part of a loop to wait for a key-press, you should first reset the value of v19 to zero, in case there is already a key-press recorded in the current interpreter cycle.
Example
<syntaxhighlight lang="agi"> [ a simple loop to check for keyboard activity v19 = 0; [ clear the key value before starting the loop StartLoop: if(!have.key()) {
goto(StartLoop);
} </syntaxhighlight>
Technical Information
Required Interpreter Version: | Available in all AGI versions. |
Byte-Code Value: | 13 (0x0D hex) |