TI-Basic Z80 Programming/GetKey

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

[edit] getKey

getKey stores the numerical value of a key press, at the instance of the instruction execution, in a variable. Its general form is :getKey → variable. This command is very useful in conditional statements.

Key Value Diagram:

Ti-83 plus keys.JPG

ON doesn't have an accessable numerical value (theoretically it should be 101; however, it acts as the program break button).

The reason for the numbering is as follows. Each key has a two (or three) digit number assigned to it. The first digit (or two 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. Left, right and up are all considered part of the second row and are numbered as such.

An example of usage is as follows:

[...]
:Lbl X
[...]
:getKey→A
:If A=105
:Then
:End
:Else
:Goto X
[...]

In this example, the program will terminate when ENTER (id 105) is pressed; otherwise it will continue to the GOTO statement and loop back to a predefined point.

:0→A
:while A=0
:getkey→A
:end
:if A=105
:then
:end

In this example, the program will wait for user input before moving past this point in the code. This is a very useful technique. It uses the same number of lines as the previous example, but sometimes it can be easier to understand because it doesn't contain any labels.

The following program will display the id of the key pressed (useful if you do not have a chart handy):

PROGRAM:GETKEY
:Repeat Ans
:getKey
:End
:Disp Ans

Run this program and hit any key. The pressed key's ID will show up.


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