TI-Basic Z80 Programming/GetKey
When writing certain TI-Basic programs, it can be useful to get individual key presses for advanced menu handling or games. This can be achieved via the getKey command (PRGM:I/O:7). getKey returns the numerical value of a key press at the instance of the instruction execution.
Key Code Diagrams
The value that is stored into variable is a special number that represents the key that was pressed. The following diagram shows the value that corresponds to the key:
Each key has a two or three digit number assigned to it. The first digit, or two digits if the number is three digits, is the key's row number (how far down on the calculator the key appears) and the last digit is the key's column number (how far across on the row the key appears). Thus the SIN key is in the 5th row and the 2nd column, so its code is 52. The only slight exception to this is the arrows keys. ←, ↑, and → are all considered part of the second row and are numbered as such. ON doesn't have an accessible numerical value (theoretically it should be 101; however, it acts as the program break button).
Because getKey only returns the key press during the instruction of the command, it will often return 0 since no keys were pressed while command was being executed. However, the getKey command can be run on a loop, allowing for user input to be registered at any time.
To do so, use this simple code:
:0→K :Repeat K :getKey→K :End
This program will wait until the user presses a key on the keypad, then will break out of the loop and continue execution. Since getKey returns 0 if no keys are pressed, the code repeats since Repeat will loop if the condition is 0 or false.
Note that the variable K is the de facto variable to store getKey to.
It is possible to shave off 4 bytes by using Ans:
:Repeat Ans :getKey :End
If getKey is called, but not stored to a variable, the value will be stored into Ans.