TI-Basic Z80 Programming/List of Commands/GetKey
Intro and Syntax
[edit | edit source]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:
:getkey
Yes, and it will get the number of the key when that code block is executed. 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 executed, it will result in 0.
Keep in mind that with out doing some sort of,
:getkey->X
the getkey will store its result in the Ans variable.
Getkey with While and Goto Loops
[edit | edit source]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 execute 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!
Notifying a pressed button
[edit | edit source]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...
:Lbl K
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.
:getkey→K
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'.
:While K>0
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 execute 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 executed, the Disp command will be executed, displaying a notice...
:End
Possible mistake
[edit | edit source]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 executed, 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
[edit | edit source]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
[edit | edit source]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
[edit | edit source]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
[edit | edit source]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!
Example:
: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.
ERR-MEMORY
[edit | edit source]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!