TI-Basic Z80 Programming/List of Commands/GetKey
Intro and Syntax
Getkey is an important feature of user interaction in TI-Basic. getkey could be found in PGRM...Right...7... and it will display the following:
Yes, and it will get the number of the key when that code block is excuted. However, that getkey is very short and therefore, it is hard to get an accurate key number. Because if you don't press anything during its being excuted, it will result in 0.
Keep in mind that with out doing some sort of,
the getkey will store its result in the Ans variable.
Getkey with While and Goto Loops
How do you get an accurate recording?
well, there is a simple while loop that you could do. It's fairly easy so I'll explain it right here...
:While 1 :getkey :If ans : ''Things to do'' : :End
That works because first, you set up a loop. And if there is nothing pressed, it will skip the if condition, then it will excute anything else below it in order. See, if getkey returned a 0, then the calculator skips the if condition, goes right back to the top which now again, checks for a getkey. so this continues on till you press a key!
This example will display "BUTTON PRESSED" when a button is pressed.
:0→K :While K=0 :getKey→K :End :Disp "BUTTON PRESSED"
Now, I'll explain Line by Line...
that Lbl K is used so that could always loop back when you need to get the number of key being pressed. It will make possible for a loop.
This, is the most important part. You need the getkey to get the key number! the '→K' will store the returned value of getkey to the variable 'K'.
this while loop is also essential because you want certain things to happen ONLY when a key is pressed. Thus, the key value is in K, it will check if K is actually greater than 0. If it is, it will excute the code block 'Disp "BUTTON PRESSED"'.
:Disp "BUTTON PRESSED"
You also need this in order to actually see that the button is being pressed. When ever the While loop is excuted, the Disp command will be excuted, displaying a notice...
This is the most important part of this code block. If there isn't an End, this is the result...
:Lbl K :getkey->K :While K>0 :Disp "BUTTON PRESSED" : :Goto K
Now do you notice any flaw? YES!
Do you think that this code will repeat itself? NO!
Do you know why it won't repeat it self? It won't repeat it self BECAUSE the 'End' isn't there! Even though it has a 'Goto K' statement, its still in the While loop, thus, if there is not keypress, that 'Goto K' won't be excuted, resulting in quitting the program. It's essential that you include the 'End' whenever you need to exit from a while loop.
Displaying the corresponding key number with the pressed key
For example, this will display the key number being pressed... You could use this as a little tool in your calculators. Just press the button you want, and it'll display its corresponding key number. Here is goes;
:Lbl 1 :getKey :If Ans > 0 :Disp Ans :Goto 1
Corresponding to specific keys
Now, if you want to respond to specific keys, such as the 2nd or Enter, you change '>0' to '=N' where N is the key number. the chart is shown here:
Do you notice anything weird? Yes! The On is not "registered". Instead of returning an integer for this key, this key will return an interrupt, meaning that it will interrupt the program. There are ASM utilities to block this On key. They are widely available at TI-Calc.Org. Anyway, look at other keys. Do you see how they are organized?
Key organization and numbering
Yes, each row starts with 11...21...31...41...51...61...71...81...91...101... and that's because then, it's easy to remember.
Here, let's use some example...
:Lbl 1 :getkey :If Ans = 11 :Disp "Y= was PRESSED" :If Ans = 21 :Disp "2ND WAS PRESSED" :Goto 1
the code should be self-explanatory. It checks for keys 11 or 21 being pressed and displays a corresponding message.
Moving Pixels with Up/Down/Left/Right Arrows
Is the Disp thingy boring for you? Well, let's move on and move things!
For now, we'll just move a pixel, but as you go on, practice and get complicated!
:ClrDraw :47→X:31→Y :Lbl 1 :getKey→K :If K=25 :ClrDraw :Y-1→Y :Pxl-On(Y,X) :End :If K=34 :ClrDraw :Y+1→Y :Pxl-On(Y,X) :End :If K=24 :X-1→X :Pxl-On (Y,X) :End :If K=26 :X+1→X :Pxl-On (Y,X) :End :Goto 1
Now, the preceding code just moved a single pixel on the screen.
If you just pressed the buttons like crazy, and you got a error message saying: ERR-MEMORY then you're out of luck. The (dreaded) memory error is the worst feature about BASIC compared to Asm, because any BASIC program will eventually slow down and stop with a memory error if run for long enough. Main cause of memory errors: INEFFICIENT CODE and long program running time. Note that with efficient code, the running time will be much longer... here is an example of inefficient code:
:0->A :While A<10 :A+1->A :If A=5:Goto X :End :Lbl X
Why? The Goto label quits the While loop, but it sits in memory because it never reached the 'End' command (simply placing 'End' after Lbl X in the code above does NOT work! So mainly, to optimize your code get rid of Goto's and Lbl's they are bad for you and your program! instead replace them with more efficient Repeat, While, and For loops. hey sorry but i made an equation set that does the samething but is faster and ahorter 1->B 1->C Lbl0 Output(B,C,"A getkey->A B-(A=25)+(B=34->B C-(A=24)=(A=26->C Goto0 storing values in B and C Label 0 Outputs A in the coordinates getkeys and stores in A the equations are self explanatory if you know the key codes and finally goto label0 hope that helps!