Uses of the getKey Function
by Kouri 1999.12.16
For many TI-BASIC game programmers out there, getKey is THE most important function in your program. Without it, there would be almost no way to get input from the user to do things like move a spaceship or a cursor around, or shoot and jump. Whenever the function is called, the calc checks to see if a key has been pressed, and if it has, it returns a value for that key, otherwise it returns the number 0. These key values can be found in the user's manual for your calculator, but for easier reference just combine the row number of the key with the column number. For example, the '5' key is located in row 8, column 3, therefore its getKey value is 83 (thanks Simon). To make a program that will display a key's value when you press it, create a new program and type this in (comments have a ";" in front of them):
:ClrHome
:Repeat 0 ; Repeat indefinitely
:getKey ; Putting it by itself on a line is like saying getKey->Ans
:If Ans:Disp Ans ; If a key was pressed, display its value
:End ; End of the Repeat loop, jump to the top
That should do it. Just press [ON] when you want to stop. Also, the arrow keys and [DEL] are special because you can hold them down, and getKey will still read them (not so for the other keys). Here are some more sample uses of getKey:
Example 1)
:Repeat 0
:If getKey:Goto X ; If a key is pressed, goto label X
:End
For this example, it doesn't matter what key is pressed. As long as any key has been pressed, the If statement will evaluate to true, and it will jump to the label. By the way, a more efficient way of doing this would be:
:Repeat getKey ; Loop until a key is pressed
:End
:Goto X
If it does matter what key you press, simply do something like this:
:Repeat 0
:getKey
:If Ans=21:Goto X ; If [2nd] was pressed, goto X
:If Ans=22:Goto Y ; If [MODE] was pressed, goto Y
:End
Now let's say you want to use getKey to change a variable (like your character's position on the screen) when you press a key. Then using Ans won't always work, because changing a variable will change the Ans, and then subsequent If statements might not work. In this case, you have to store getKey to a variable itself. If all this sounds kind of complicated, don't worry. Just stick with me and look at the examples.
Example 2)
:Repeat 0
:getKey->K ; Stores the value in K
:If Ans=24:X-1->X ; If the left arrow key was pressed decrement X by
; 1. It's okay to use Ans because there is nothing
; before it that might have changed Ans' value.
:If K=26:X+1->X ; If the right arrow key was pressed increase X
; by 1. Now you have to use K because Ans might
; have been changed by the previous line.
:End
Once again, there is a way to improve this. A smaller, faster version would look like this:
:Repeat 0
:getKey
:X-(Ans=24)+(Ans=26->X ; This is kind of tricky. This is called a logic
; statement. If you don't understand why you can do
; this, then I suggest you get your hands on some
; other tutorials, because I'm not gonna explain it.
:End
Here's a fairly advanced sample program that puts an "O" on the screen, lets you move it around with the arrow keys, and [ENTER] quits. If you move off the screen, you will magically (yeah right) warp to the other side.
:8->X:4->Y ; This is the starting position
:ClrHome
:Output(Ans,X,"O ; Without this, nothing would
; appear until you pressed a key.
:Repeat K=105 ; Loop until [ENTER] is pressed
:getKey->K
:If Ans>23 and Ans<35:Then ; Just so that pressing other keys
; doesn't make your guy "blink"
:Output(Y,X," " ; You don't have to use the last quotation
; mark. I just put it there so you could
; see that I only used one blank space.
:X+(Ans=26)-(Ans=24->X
:Ans+16(not Ans)-16(Ans=17->X ; If your X coordinate is not on the
; screen, change it to the other side.
:Y+(K=34)-(K=25->Y
:Ans+8(not Ans)-8(Ans=9->Y ; If your Y coordinate is not on the
; screen, change it to the other side.
:Output(Ans,X,"O ; Y is the last value changed, so Ans is Y
:End ; End If
:End ; End Repeat
Here is a nice, simple, little (153 bytes) program I made that lets you input numbers to the graph screen. You can't input negative numbers but you can backspace by pressing [DEL]. For numbers greater than or equal to 10^10 it will display the number in scientific format, which you may not want. I'll leave that up to you to handle. The program uses variables K and T, but you can change them if you have conflicts with existing variables. K is the getKey value and T is the total number.
:0->T ; Initialize the total
:ClrDraw
:Text(0,0,"HOW MUCH? ; The "prompt"
:Repeat K=105 ; Stops when you press [ENTER]
:getKey->K
:If Ans=23 or Ans=102 or (Ans>91 and Ans<95) or (Ans>81 and Ans<85) or (Ans>71 and Ans<75:Then
:If Ans=23:Then ; If [DEL] was pressed...
:int .1T->T ; Divide the number by 10 and get rid of the remainder
:Else
:10T+(Ans<99)(Ans-65-13(Ans>81)-13(Ans>91->T ; Add the digit pressed to the total
:End
:Text(0,38,Ans," " ; Display the total, plus some spaces to take care of backspacing.
:End
:End
Well, that's it. I hope that was useful to you. If there's anything you'd like to see in this tutorial that I didn't cover, feel free to let me know, and I'll try to update this article.