< TI-Basic Z80 Programming

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 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:

TI-84s
1112131415
212223242526
31323334
4142434445
5152535455
6162636465
7172737475
8182838485
9192939495
102103104105
TI-83s
1112131415
212223242526
31323334
4142434445
5152535455
6162636465
7172737475
8182838485
9192939495
102103104105

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

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!

Try these examples to practice using getKey.

Player Movement

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.


This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.