Python Forum

Full Version: Can I Control loop with Keyboad key (start/stop)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I want to defining a loop (i.e if) that can start/stop when I press a key like 1,2 on keyboard. For instance, when i press the "1" key without enter, loop' should begin at that time. I'll run this loop with Raspberry Pi. Confused
How can we codded as suitable? Which module can be useful for my problem? Huh
I am not positive but there should be something like getch() function. So you could say:

char = getch()

if (char == '1'):
  io.output(motor1_in1_pin, True)

if (char == '2'):
  io.output(motor1_in1_pin, False)
Then again I have never used a Raspberry Pi before.
Thank you for your helping. I could solve my probem myself Shy as following these steps.
import mscvrt
while True:
key=mscvrt.getch().decode('utf-8)
if key=='1'
print('System is active.')
elif key=='0'
print('System is inactive.')