PhP
PhP

Reputation: 82

getKey from Ti-Basic in Python?

I was wondering how the getKey command works and how to use it in Python, as there is no command like that (right?).

Somehow, StackOverflow does not like short questions. I was writing a program on my calculator and then I thought, why not put it to my computer using Python? But then I realized I could not because I don't know what to do with the getKey.

Upvotes: 0

Views: 999

Answers (2)

Jme
Jme

Reputation: 3

You can use get_key(key)! However, you need to first import the System Module (named ti_system).

get_key can be run without a parameter to output a string corresponding with the key pressed (pressing 1 outputs "1", pressing escape outputs "esc", etc.).

If you use a parameter in get_key, it will delay program execution until that key is pressed. For example, get_key("5") will wait until the 5 key is pressed before running the program.

Note! When using get_key, the keypress is discarded. If you wish to use the keypress multiple times, store it to a variable! For example, instead of writing

while get_key() != "esc":
  if get_key() == "left":
    go_left()
  if get_key() == "right":
    go_right()

instead write

key = ""
while key != "esc":
  key = get_key()
  if key == "left":
    go_left()
  if key == "right":
    go_right()

For example, if you wanted to create an infinite loop that runs until the user presses escape, you would run

from ti_system import *

key = ""

while (key != "esc"):
  key = get_key()
  # run code here

You can also get mouse coordinates using get_mouse().

You can see the full documentation for the ti_system module here https://education.ti.com/html/webhelp/EG_TINspire/EN/Subsystems/EG_Python/Content/m_menumap/mm_tisystem.HTML

Yeah... Unfortunately TI doesn't have particularly good documentation. As an aside, I was not able to get print("key: "+get_key()) to work properly, despite get_key working fine in comparisons such as get_key() == "esc", for some reason? This might just be poor code on my part, but this does mean I cannot provide a list of the key strings to specific keys like TIBasic has. If anyone reading this managed to get this to work properly, please let me know!

Upvotes: 0

Roundhouse
Roundhouse

Reputation: 98

Not sure about your question, but this is the use of the getKey command in Ti-Basic:

:While 1         #loop
:Repeat X        #make sure a key is presssed
:getKey→X        #store that key in a variable
:End             #end loop
:Disp X
:End

getKey returns a value which corresponds to a key on the Ti-"Keyboard". You can look up the key values in the picture below.

Ti-"keyboard"

To get this functionality in Python search this site for it. You could use .char and .keysym for this. More info:

Keypress detection Python

Upvotes: 2

Related Questions