TI-Basic Z80 Programming/GetKey

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

getKey[edit]

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 accessible 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
:Stop
:Goto X

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

Labels are NOT recommended in advanced coding.. Instead, try nested loops.

For Example.

[...]            // Program code.
:Delvar Z        // You don't need a new line here.  
:Repeat Z=1      // Ex. :DelvarZRepeat Z=1
[...]            // Program code to repeat (Like the previous label as stated above.)
:Repeat Ans
:getKey          // This will wait until a keypress is made. 
:End             // End of :Repeat Ans loop.
:Z+(Ans=105)→Z   // Test for keypress 105 (Enter) adds 1 to Z if true 0 if false.
:End             // End of :Repeat Z=1 loop.


In this example there are no labels. The loop waits for key to continue and will end if enter key is pressed. If any other key is pressed it will loop back just as the label program would.


:While Ans≠105
:Repeat Ans
:getkey
:End
:End

In this example, the program will wait for user input 105 (Enter) before ending the continuous loop. This is a very useful technique. The following program will display the id of the key pressed (useful if you do not have a chart handy):

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

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