Can a for loop be timed? - python-3.x

from ast import Break
import pyautogui as pat
import pyautogui
import time
import keyboard
# time.sleep(3)
# print(pat.position())
pat.click(x=712, y=1052, duration=1)
pat.click(x=1108, y=917)
file = open('peanut.txt', 'r').readlines()
for i in file:
pat.typewrite(i, interval=0.1)
pat.press('enter')
How do I time the for loop to stop or command the loop to stop by pressing a key?
I tried using the keyboard module but I could only find a simulation of the 'esc' key but I cannot seem to figure out what module would allow me to physically press a key to quit the program?? Any help would be fantastic. Thank you!

you can bind any key to stop loop
import keyboard
number=1
while True:
number=number+1
print(number)
keyboard.wait("escape")
try this code
this code will print a number and will wait escape, if escape is pressed will print another number infinitely
or you can do this
import keyboard
number=1
while True:
number=number+1
print(number)
if keyboard.is_pressed("escape"):
break
else:
pass
def AfterLoopWhile():
"""This code can't execute if the loop isn't break (Sorry for my
english)"""
print("Stopped the loop")
AfterLoopWhile() "If the loop is break, execute the function AfterLoopWhile
to stop the loop
or you can quit if escape is pressed
import keyboard
number=1
while True:
number=number+1
print(number)
if keyboard.is_pressed("escape"):
exit()
else:
pass

Related

exit loop pynput thread

Hello i'm trying to make a program to play snake using pynput.
When I press space the program runs but even if I press backspace i can't stop it, I know that I have to use threading but I don't figure how it works, thank you
my code :
from pynput import keyboard
from pynput.keyboard import Key, Controller
import threading
import time
kb = Controller()
def droite(key):
kb.press(Key.right)
kb.release(Key.right)
def gauche(key):
kb.press(Key.left)
kb.release(Key.left)
def haut(key):
kb.press(Key.up)
kb.release(Key.up)
def bas(key):
kb.press(Key.down)
kb.release(Key.down)
def on_press(key):
if key == Key.space:
print('yes')
droite(Key.right)
time.sleep(1.7)
haut(Key.up)
time.sleep(0.9)
gauche(Key.left)
time.sleep(2.15)
bas(Key.down)
time.sleep(1.9)
droite(Key.right)
time.sleep(2.15)
for i in range(255):
for i in range(6):
haut(Key.up)
time.sleep(0.1 )
gauche(Key.left)
time.sleep(2.1)
haut(Key.up)
time.sleep(0.1)
droite(Key.right)
time.sleep(2.1)
gauche(Key.left)
time.sleep(2.15)
bas(Key.down)
time.sleep(1.9)
droite(Key.right)
def on_release(key):
if key == Key.backspace:
print('no')
KeyboardInterrupt
return False
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
th1 = threading.Thread(target=on_press)
th1.start()
th2 = threading.Thread(target=on_release)
th2.start()
th1.join()
th2.join()
I'm trying to stop the program even if I'm in a loop when I press a key

pynput.keyboard terminate program when key is pressed not responding

I have this code here and what I want is for the code to type "potato" every 5 seconds. I also want a "quit" command, and I want it so that when I press = it will stop the program. However, not only does the code not work, but it also types "=" after it types/enters "potato". Can someone help? Thanks.
from pynput.keyboard import Key, Controller
import time
keyboard = Controller()
time.sleep(3)
while True:
keyboard.type('potato')
keyboard.press(Key.enter)
keyboard.release(Key.enter)
if keyboard.type('asdf'):
quit()
time.sleep(5)
P.S. no errors or anything
The most obvious answer here would be to change the "asdf" to "=" in the if statement, but if that doesn't fix the problem, you could add a listener to detect keypresses.
from pynput import keyboard, Key, Controller
import time
# set your termination key here
cut_key = "="
# check if key pressed is cut_key, if so, quit
def on_press(key):
try:
if key.char == cut_key:
quit()
except AttributeError:
if key == cut_key:
quit()
# non-blocking listener
listener = keyboard.Listener(
on_press=on_press)
listener.start()
keyboard = Controller()
time.sleep(3)
while True:
keyboard.type('potato')
keyboard.press(Key.enter)
keyboard.release(Key.enter)
time.sleep(5)
I didn't test this code, please comment if it doesn't work.
https://pynput.readthedocs.io/en/latest/keyboard.html

msvcrt.kbhit() always returns false . Unable to detect Escape key

I tried if (msvcrt.getch() == chr(27).encode()), it didn't work
The msrcvt.getch() in my program always prints to b'\xff' in the debug-print statement.
It simply didn't detects the ESC key I am pressing. How to make it work. Please provide any sample code for Python 3.8.x.
Try: if msvcrt.getch()==b'\x1b'
Ex:
import msvcrt
while True:
if msvcrt.kbhit():
key_stroke = msvcrt.getch()
if key_stroke==b'\x1b':
print ("Esc key pressed")
else:
print (str(key_stroke).split("'")[1],"key pressed")

How to Detect 'ESC' keypress while expecting input() from user

I tried using
if msvcrt.kbhit():
key_stroke = msvcrt.getch()
if key_stroke==chr(27).encode(): #b'\x1b'
print ("Esc key pressed")
sys.exit()`
before and after the data=input('Enter a value:') but the Esc key_stroke not getting detected
That is, while expecting an input from user with input() function, if the user press Esc key, I want to do sys.exit()
Try this:
import sys
import msvcrt
def func():
print ('Enter user input:')
while True:
if msvcrt.kbhit():
key_stroke = msvcrt.getche()
if key_stroke==chr(27).encode():
print ("Esc key pressed")
sys.exit()
else:
#print (str(key_stroke).split("'")[1],"key pressed")
i=str(key_stroke).split("'")[1]+input()
print ("User input:",i)
func()
Note: I am using getche instead of getch, it is similar to getch but prints the key that is pressed.
This worked,
import msvcrt
data=input('Enter a value : ')
if(data):
print(data)
else:
print('invalid value')
key=msvcrt.getch()
if(msvcrt.getch() == chr(27).encode()):
print('Escape key pressed')
The main difference/relation between this and the said similar question is that,
The msvcrt.kbhit() couldn't be used with input() function. because it always doesn't wait for the keyboard hit. So directly using the msvcrt.getch() works perfectly.
Both the code provided for the question is mainly based on msvcrt library and that library didn't work as expected in the Python IDLE console, but perfectly in the windows command prompt
Please correct me if I am wrong

How to break a while true with keyboard interruption?

How can I break a while True if I press a key?, and if it is possible, how can I break a while True if a press an x key (e.g. intro)?.
while True:
if something is pressed:
print("STOP")
break
do_something()
I tryied with inputs but you can't put:
while True:
i = input() or None if program wait > 3 seconds. # > = more than
if != None:
print("STOP")
break
do_something()
Input stop all the while waiting for answer, and i don't want that.
PD: I use win 10 64-bits, python 32-bits 3.6, terminal.
PD2: Before post this question I search more about it, i found:
while True:
try:
do_something()
except KeyboardInterrupt:
pass
But this only stop with Ctrl + c, not other keys.
This only works in Windows:
import msvcrt
while True:
if msvcrt.kbhit():
break
The msvcrt provide a list of functions capables of process key inputs. msvcrt.kbhit() check if a key was pressed and it's waiting for been read and return true or false based on that.

Resources