Python Forum
Can I Control loop with Keyboad key (start/stop)
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can I Control loop with Keyboad key (start/stop)
#1
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
Reply
#2
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.
Reply
#3
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.')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Button to stop while loop from another script Absolutewind 5 900 Sep-25-2023, 11:20 PM
Last Post: deanhystad
  ''.join and start:stop:step notation for lists ringgeest11 2 2,434 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  get out of while loop and stop repeat Frankduc 11 2,984 Apr-26-2022, 10:09 PM
Last Post: deanhystad
  tkinter control break a while loop samtal 0 2,396 Apr-29-2021, 08:26 AM
Last Post: samtal
  Start loop from different points TheHolyPyGrenade 3 2,070 Apr-11-2021, 07:57 PM
Last Post: TheHolyPyGrenade
  Stop/continue While loop block Moris526 68 25,503 Mar-28-2021, 09:21 PM
Last Post: Larz60+
  while loop will not stop looping TheTechRobo 5 3,733 Apr-20-2020, 01:47 PM
Last Post: TheTechRobo
  Use Python to start/stop a server service via a webform? oakleaf2001 0 1,756 Apr-04-2020, 06:14 AM
Last Post: oakleaf2001
  start and stop omxlayer playback dezmob 2 4,767 Jan-27-2020, 09:43 AM
Last Post: dezmob
  how to stop and start a script for 30 seconds laspaul 9 7,655 Jan-16-2020, 02:13 PM
Last Post: laspaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020