TI-Basic Z80 Programming/GetKey

From Wikibooks, open books for an open world
< TI-Basic Z80 Programming
Jump to navigation Jump to search

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.

getKey→variable

Key Code Diagrams[edit]

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
11 12 13 14 15
21 22 23 24 25 26
31 32 33 34
41 42 43 44 45
51 52 53 54 55
61 62 63 64 65
71 72 73 74 75
81 82 83 84 85
91 92 93 94 95
102 103 104 105
TI-83s
11 12 13 14 15
21 22 23 24 25 26
31 32 33 34
41 42 43 44 45
51 52 53 54 55
61 62 63 64 65
71 72 73 74 75
81 82 83 84 85
91 92 93 94 95
102 103 104 105

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]

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.


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