Jump to content

TI-Basic Z80 Programming/GetKey

From Wikibooks, open books for an open world

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

getKey→variable

Key Code Diagram

[edit | edit source]

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. ON doesn't have an accessible numerical value (theoretically it should be 101; however, it acts as the program break button).

Usage

[edit | edit source]

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.

You try it!

[edit | edit source]

Try these examples to practice using getKey.

Player Movement

[edit | edit source]

Using getKey, draw a player (use a single character like *) 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.

Solution
:1→X :1→Y :0→K :ClrHome :While 1 :Repeat K :getKey→K :End :Output(Y,X," ") :If K=25 and Y>1 :Y-1→Y :If K=34 and Y<7 :Y+1→Y :If K=24 and X>1 :X-1→X :If K=26 and X<16 :X+1→X :Output(Y,X,"*") :End



Previous: Criteria Instructions
Next: Menus
Table of Contents: TI-Basic Z80 Programming