# 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 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,
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...
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

This example will display "BUTTON PRESSED" when a button is 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 execute the code block '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...

## Possible mistake

This is the most important part of this code block. If there isn't an End, this is the result...
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

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;

## 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...
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!
Example:
Now, the preceding code just moved a single pixel on the screen.

## ERR-MEMORY

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:
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!