The easiest way is to just interrupt it with the usual Ctrl-C (SIGINT).
try:
while True:
do_something()
except KeyboardInterrupt:
pass
Since Ctrl-C causes KeyboardInterrupt to be raised, just catch it outside the loop and ignore it.
The easiest way is to just interrupt it with the usual Ctrl-C (SIGINT).
try:
while True:
do_something()
except KeyboardInterrupt:
pass
Since Ctrl-C causes KeyboardInterrupt to be raised, just catch it outside the loop and ignore it.
There is a solution that requires no non-standard modules and is 100% transportable:
import _thread
def input_thread(a_list):
raw_input() # use input() in Python3
a_list.append(True)
def do_stuff():
a_list = []
_thread.start_new_thread(input_thread, (a_list,))
while not a_list:
stuff()
python - How do I wait for a pressed key? - Stack Overflow
Exiting infinite loop with keypress in python
How so I wait for a keystroke?
python - While statement checking for button press while accepting raw input - Raspberry Pi Stack Exchange
Hey everyone!,
atm i need to repeat some code but i am not to sure how, i think i have to use while loops. And i need it to repeat an infinite amout of times untill i press a button for instance "q"
import time
import pyautogui
import pydirectinput
import time
<While loop?>
time.sleep(5)
pydirectinput.keyDown('d')
time.sleep(3)
pydirectinput.keyUp('d')
time.sleep(31)
pydirectinput.leftClick(982, 876)
<If statment where i need to press q to stop the sript?>
Thank you to anyone who can help me :)
In Python 3, use input():
input("Press Enter to continue...")
In Python 2, use raw_input():
raw_input("Press Enter to continue...")
This only waits for the user to press enter though.
On Windows/DOS, one might want to use msvcrt. The msvcrt module gives you access to a number of functions in the Microsoft Visual C/C++ Runtime Library (MSVCRT):
import msvcrt as m
def wait():
m.getch()
This should wait for a key press.
Notes:
In Python 3, raw_input() does not exist.
In Python 2, input(prompt) is equivalent to eval(raw_input(prompt)).
In Python 3, use input():
input("Press Enter to continue...")
In Python 2, use raw_input():
raw_input("Press Enter to continue...")
I want to make a program that waits for the user to press a key and then acts based on what key was pressed. The only way I found was to make a loop that checks if a key is pressed, but I want the program to halt and wait for the keystroke rather than repeat.
Edit: To clarify, the program doesn't do anything while it waits. It just waits as if I used the input() method.
Edit2: I've been given a solution. Thank you to everyone.
PS.: New here so sorry if I've asked in a strange way or something of the sorts
Depending on how your circuit is wired you should use
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
inputPin = "YOUR_PIN_GOES_HERE"
GPIO.setup(inputPin,GPIO.IN,pull_up_down=GPIO.PUD_UP)
def buttonPress(channel):
#stuff
def buttonRelease(channel):
#stuff
GPIO.add_event_detect(channel,GPIO.RISING,callback=buttonPress)
#OR
GPIO.add_event_detect(channel,GPIO.FALLING,callback=buttonRelease)
while(1):
keypressed = raw_input('Press q to quit: ')
if keypressed == 'q':
break
elif keypressed == 'SOME OTHER KEY':
#code for some other thing
else
print("Unknown input")
These are events that will be trigged where there is a change on your input pin. Your raw_input can still handle whatver code you want because the event loop is running in the background.
Pulled from here
Edits: put the event defs above event declaration because I am teh dumb
While(1)'s are ugly. For the sake of staying a loop this should suffice though. Essentially this will run the keyboard input loop until you hit q. Use the elifs to handle other inputs. The else will capture unknown input and provide feedback. The break statment will boot the program out of the while loop and will reach the end of code and terminate.
Should you feel fancy and want to handle a lot of input I would look to use what other languages have a switch statement to clean up a bunch of elifs
Oh and standard warranty applies, I may have missed a tab or : here and there.
Your Python module will offer GPIO callbacks. Generally you specify
- a GPIO
- whether you are interested in rising edges (0->1), falling edges (1->0), or both edges
- and a function to be called when the event happens
The specified function will be called asynchronously to the main thread, i.e. it will still be called even if the main thread is waiting in a raw_input.
Generally you would use global variables to pass state information between the callback and main thread.
You can use keyboard library. Try this:
import keyboard
while True:
if keyboard.read_key() == "p":
myfunction()
As answered in other questions, responding to a key press involves dealing with events.
Each key press triggers/ emits an event. There are couple of library that deals with events and in particular keyboard events.
modules keyboard and getKey are worth exploring. and to a straightforward answer, refer above answer.(just dont want to repeat, credit to him)