This program will demonstrate how to use key presses in TI-Basic(83,84)
First the program:
CODE
PROGRAM: CURSOR
:0→C
:0→G
:8→X
:4→Y
:Input (“YOUR CURSOR: “,Str0)
:While not(C)
:ClrHome
:getKey→G
:Output(Y,X,Str0)
:X+(G=26)-(G=24) →X
:Y+(G=34)-(G=25) →Y
:If G=45
:Goto Z9
:If X≤0
:1→X
:If X≥17
:16→X
:If Y≤0
:1→Y
:If Y≥9
:8→Y
:END
:Lbl Z9
:ClrHome
:Output(1,1,”
So, to break it down.
CODE
:0→C
:0→G
:8→X
:4→Y
Sets C and G variables to 0.
Sets X and Y to 8 and 4 respectively.
This will put the cursor in the middle of the Home screen when you start.
C is used to keep the while loop running.
G is where we will store the keypress.
X and Y are Horizontal and Vertical position, respectively.
CODE
:Input (“YOUR CURSOR: “,Str0)
Lets you choose any character to use as a cursor.
CODE
:While not(C)
Loops the program until the user ends it.
CODE
:ClrHome
Refreshes the screen every time the program loops
CODE
:getKey→G
If a key is pressed, it stores the number of the key into the variable G
CODE
:Output(Y,X,Str0)
outputs the position of the cursor (Str0) to the coordinates Y, and X Respectively. This changes every time the program loops.
CODE
:X+(G=26)-(G=24) →X
The key number for left is 24 and the key number for right is 26.
This line says “take X +(1 or 0 :evaluate if the right key is pressed or not and return a value of 1 if yes or zero if no) - (1 or 0 :evaluate if the right key is pressed or not and return a value of 1 if yes or zero if no)”
Example:
If the right key is pressed:
X+(1)-0→X //moving 1 forward on the x axis
If the left key is pressed:
X+(0)-(1) →X //moving 1 backward on the x axis
CODE
:Y+(G=34)-(G=25) →Y
The key number for up is 34 and the key number for down is 25.
This line says “take X +(1 or 0 :evaluate if the up key is pressed or not and return a value of 1 if yes or zero if no) - (1 or 0 :evaluate if the down key is pressed or not and return a value of 1 if yes or zero if no)”
Example:
If the right key is pressed:
X+(1)-0→X //moving 1 forward on the y axis
If the left key is pressed:
X+(0)-(1) →X //moving 1 backward on the y axis
CODE
:If G=45
:Goto Z9
45 is the clear button. This goes to label Z9 if the clear button is pressed, exiting the program
CODE
:If X≤0
:1→X
:If X≥17
:16→X
:If Y≤0
:1→Y
:If Y≥9
:8→Y
The edges of the home screen is 0 to 16 on the x axis, and 0 to 8 on the y axis.
This series of
if statements says that if the cursor goes over the edge of the screen, put it back at the edge.
CODE
:END
Ends the while loop instance
CODE
:Lbl Z9
:ClrHome
:Output(1,1,”
If the clear key is pressed, the program goes to label Z9, and clears the home screen.
Output(1,1,” stops
Done from being displayed when the program is done running.