Understanding getKey

The getKey routine is one of the most simple and helpful tools in programming ti-basic, and at the same time it is extremely complex. You can understand exactly how it works if you read this page. To understand this page, you should have somewhat good knowledge of how loops work.

the getKey command alone searches to see if any of the keys have been pressed, and if one has been pressed, the key's assigned code is stored to ans. If you want the assigned code to be stored to a variable, then you should put the variable after getkey.
examples:
getKey
getKey A

If you want to search for a key to be pressed over and over again, put the getKey inside a loop.
examples:
While 1
getKey
End

Repeat 0
getKey
End

If you want the loop to keep going until a certain key is pressed, do this.
Repeat A=(keycode of key)
getKey->A
End

While A(isn't equal to)(keycode of key)
getKey->A
End

If you want the loop to keep going until any key is pressed do this
Repeat A
getKey->A
End

While not(A
getKey->A
End

You can find the code of a key with this program.
Repeat Ans
getKey
End
Disp Ans

To check to see if a certain key has been pressed, put this.
getKey->A
if A=(keycode of key to check for)
Then
commands here
End

contrary to what most other sites say, you don't need to have a getKey command inside it's own loop. It just seems a lot faster. In actuality, it is overall faster to have getKey stuck in with the rest of a loop than within it's own, it just seems like that because it responds faster, but then takes much longer to get through the rest of the loop. It is also smaller to not have an extra loop. Here is an example.

While 1
While not(A
getKey->A
End
commands here
End

can be

While 1
getKey->A
commands here
End

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NoDerivs 3.0 License