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 ( I/O 7). getKey returns the numerical value of a key press at the instance of the instruction execution.

Key Code Diagram
The value that is stored into variable is a special number that represents the key that was pressed. The diagram to the right 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. doesn't have an accessible numerical value (theoretically it should be 101; however, it acts as the program break button).

Usage
Because <tt>getKey</tt> 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 <tt>getKey</tt> command can be run on a loop, allowing for user input to be registered at any time.

To do so, use this simple code:

This program will wait until the user presses a key on the keypad, then will break out of the loop and continue execution. Since <tt>getKey</tt> returns  if no keys are pressed, the code repeats since <tt>Repeat</tt> will loop if the condition is   or false.

Note that the variable  is the de facto variable to store <tt>getKey</tt> to.

It is possible to shave off 4 bytes by using :

If <tt>getKey</tt> is called, but not stored to a variable, the value will be stored into.

You try it!
Try these examples to practice using <tt>getKey</tt>.

Player Movement
Using <tt>getKey</tt>, draw a player (use a single character like <tt>*</tt>) on the screen and allow the user to push the arrow keys to move the player in X and Y directions. Make sure the player won't go off the screen.