Python Forum
Can I Control loop with Keyboad key (start/stop) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Can I Control loop with Keyboad key (start/stop) (/thread-11832.html)



Can I Control loop with Keyboad key (start/stop) - Lyperion - Jul-27-2018

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


RE: Can I Control loop with Keyboad key (start/stop) - Vysero - Jul-27-2018

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.


RE: Can I Control loop with Keyboad key (start/stop) - Lyperion - Jul-28-2018

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.')