TI-BASIC Wiki
Advertisement


==(84) :getKey (89) :GetKey()==

At the time this command is executed, the program will check to see if the user has pressed a key. If so, this returns the Keystroke Values associated with the pressed key. While by itself fairly useless, getKey can be used with other commands to be very helpful.

Getkey

The GetKey function on both TI-83 and TI-84 uses a map exactly like this to detect which button was pressed. It is much like a graph, starting with (1,1) and ending with (10,5)


Location[]

  • PRGM
  • I/O
  • 7:getKey

Example[]

This program will wait for user keypresses. Once a user presses a key it will give the keycode (11-105) of the key pressed

:While 1
:Repeat X
:getKey→X
:End
:Disp X
:End

This program will start with the first While loop.

The Repeat command makes the program repeat the getKey command until it reads a keypress. The program will then display the value of the key pressed.


A more suitable version of this would be..

(Same size and function, cleaner code, slightly faster.)

:While 1
:Repeat Ans
:getKey
:End
:ClrHome
:Disp Ans
:End

Why this works:


We will start with the 2nd loop first so that it will make more sense.

:Repeat Ans
:getKey
:End

The Repeat (wait for keypress) loop works like this..

Repeat (until) [Condition] (is true)

Each time the program passes the Repeat loop the Ans variable = 0 (false)

Using Boolean Logic 0 = false anything that isnt 0 = true

So.. Repeat (until) [0] (is true) is obviously false causing the program to loop until...

getKey is then saved to Ans, an internal variable.

End; When the loop reaches the End instruction it will jump back to the top and again check if..

Repeat (until) [Condition] (is true).


Now the main loop.

:While 1
:[...Code]
:[...Code]
:ClrHome
:Disp Ans
:End

The Main loop uses While 1 to initiate an infinite loop.

While [Condition] (is true).

Using Boolean Logic 0 = false; Anything that is not 0 = true. {1 = True, 30 = True, 5 Billion = True, 0 = False}

Because condition 1 (true) is true it will loop until the on key is pressed.

ClrHome is used to clear the screen right before displaying Keypress to prevent overlapping.

Disp Ans displays the answer on the Homescreen. (Plain and simple.)

Disp is placed after the repeat loop so it will display only after keypress has been made.

End; When the loop reaches the End instruction it will jump back to the top and again check if..

While [Condition] (is true)

Advertisement