How to exit a function because a key was pressed? - python

Im trying to move my Cursor every three seconds and if it went too far, reset it to its original location. I want the program to stop after I press any key on the keyboard.
It doesnt seem to work though... what could I be missing? This is what I came up with:
import win32api, win32con, time, sys, msvcrt
global z
z=10
def kbfunc():
#this is boolean for whether the keyboard has bene hit
x = msvcrt.kbhit()
if x:
sys.exit
else:
ret = False
return ret
def move(x,y):
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE | win32con.MOUSEEVENTF_ABSOLUTE, int(x/1920*65535.0), int(y/1080*65535.0))
while True:
move(960,540+z)
time.sleep(3)
if z>100:
move(960,540)
z += -100
z + 10
kbfunc()

First, you need to install the keyboard module:
pip3 install keyboard
And than add it to your loop:
import keyboard
while True:
move(960,540+z)
time.sleep(3)
if z>100:
move(960,540)
z += -100
z + 10
if keyboard.is_pressed('q'): # if key 'q' is pressed
print('You Pressed A Key!')
break # finishing the loop

I would guess that msvcrt.kbhit() does not store previous hits. It only tells you if a key is hit at the current moment in time. You can loop checking for keyboard hits. If no hits before 3 seconds, return:
import time
import msvcrt
import sys
def kbfunc(max_time=3):
start = time.time()
while not msvcrt.kbhit():
end = time.time()
if end - start >= max_time:
break
else:
sys.exit()

After trying around with the given answers, the finished solution looks like this:
global z
z=10
def move():
mouse.move(10, 10, absolute=False, duration=1)
def moveback():
mouse.move(-10, -10, absolute=False, duration=1)
while True:
move()
time.sleep(3)
moveback()
if keyboard.is_pressed('q'): # if key 'q' is pressed
break # finishing the loop

Related

How to make it so that when pressed hotkey + anything else the hotkey worked?

I'm writing a script for game, i need to bind some key for pressing right click for three times. I can do it, but if i will use it in game i can press WASD or something else when i using hotkey and it doesn't work.
My code here :
import keyboard
import mouse
import time
iswork = False
def work():
global iswork
if iswork:
iswork = False
print("Script deactivated")
else:
iswork = True
print("Script activated")
keyboard.add_hotkey("F4",work)
def RRR():
if iswork :
for i in range(3):
mouse.right_click()
time.sleep(0.05)
bind = input("Bind for Right - Right - Right :")
#keyboard.add_hotkey(bind,RRR)
while iswork:
if keyboard.is_pressed("x") and len(keyboard._pressed_events) >= 1:
RRR()
elif keyboard.is_pressed("x"):
RRR()
while True :
count = 0

How to stop or pause pyautogui at any moment that I want?

I am building some macro program using pyautogui.
Unfortunately I am not able to stop for-loop so sometimes it takes too much time until for-loop end.
Is there any way to stop the program at any moment I want? or I just wait until program end.
below is my code
CURRENT_DIR = os.getcwd()
list = os.path.join(CURRENT_DIR,'BOM_list.xlsx')
df = pd.read_excel(list)
for i in df['MLFB'].index:
MLFB = df.loc[i, 'MLFB']
print(MLFB)
a = pyautogui.locateCenterOnScreen('a_material.png')
print(a)
pyautogui.moveTo(a)
pyautogui.moveRel(350, 0)
pyautogui.click()
pyautogui.keyDown('ctrl')
pyautogui.press('a')
pyautogui.keyUp('ctrl')
pyautogui.typewrite(MLFB)
b = pyautogui.locateCenterOnScreen('b_execute.png')
print(b)
pyautogui.moveTo(b)
pyautogui.click()
time.sleep(2.5)
pyautogui.keyDown('alt')
pyautogui.press('y')
pyautogui.press('t')
pyautogui.press('a')
pyautogui.press('i')
pyautogui.keyUp('alt')
time.sleep(2)
pyautogui.press('down')
pyautogui.typewrite(['enter'])
time.sleep(2)
c = pyautogui.locateCenterOnScreen('c_Directory.png')
pyautogui.moveTo(c)
pyautogui.moveRel(350, 0)
pyautogui.click()
pyautogui.keyDown('ctrl')
pyautogui.press('a')
pyautogui.keyUp('ctrl')
pyautogui.typewrite(CURRENT_DIR)
pyautogui.click()
time.sleep(1.5)
d = pyautogui.locateCenterOnScreen('d_Filename.png')
pyautogui.moveTo(d)
pyautogui.moveRel(350, 0)
pyautogui.click()
pyautogui.keyDown('ctrl')
pyautogui.press('left')
pyautogui.keyUp('ctrl')
pyautogui.typewrite(MLFB)
time.sleep(0.5)
pyautogui.typewrite(['enter'])
time.sleep(2)
e = pyautogui.locateCenterOnScreen('e_go_back.png')
pyautogui.moveTo(e)
pyautogui.click()
time.sleep(2)
PyAutoGUI has a built in failsafe to terminate the program at any time. Just move your mouse to the top left corner of your main monitor where your x, y values would be 0, 0.
Typing print(pyautogui.FAILSAFE) should return True, telling us the failsafe is on. You could also disable it if it's getting in the way of your program by setting it to pyautogui.FAILSAFE = False
Looking through your code, you could save some space by using hotkey() when you want to press more than one key at a time:
pyautogui.keyDown('ctrl')
pyautogui.press('a')
pyautogui.keyUp('ctrl')
Is the same as:
pyautogui.hotkey('ctrl', 'a')
You could also check out threading which allows you to run more than one process at a time.
The following code will have an example main program running and when the Esc key is pressed, the main program will pause and the user is prompted if they want to continue or not.
import time
from threading import Thread
from pynput import keyboard
def exit_program():
def on_press(key):
if str(key) == 'Key.esc':
main.status = 'pause'
user_input = input('Program paused, would you like to continue? (y/n) ')
while user_input != 'y' and user_input != 'n':
user_input = input('Incorrect input, try either "y" or "n" ')
if user_input == 'y':
main.status = 'run'
elif user_input == 'n':
main.status = 'exit'
exit()
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
def main():
main.status = 'run'
while True:
print('running')
time.sleep(1)
while main.status == 'pause':
time.sleep(1)
if main.status == 'exit':
print('Main program closing')
break
Thread(target=main).start()
Thread(target=exit_program).start()

python - can't get audio player working

everything works except the next song doesn't play after the first is finished.
import os, random
from pygame import mixer
from pynput import keyboard
startup = 0
pause = 0
volume = 0.5
def Picker():
global startup
global volume
startup += 1
if startup > 1:
ThisSong = random.choice(os.listdir("C:\\Users\\...\\Music"))
NextSong = random.choice(os.listdir("C:\\Users\\...\\Music"))
ThisSong = NextSong
if ThisSong != NextSong:
mixer.init()
mixer.music.load("C:\\Users\\...\\Music" + ThisSong)
mixer.music.play(0)
mixer.music.set_volume(volume)
while mixer.music.get_busy():
def on_press(key):
global pause
global volume
if key == keyboard.KeyCode(char='-'):
volume -= 0.1
if volume < 0.1:
volume = 0.1
mixer.music.set_volume(volume)
if key == keyboard.KeyCode(char='='):
volume += 0.1
if volume > 1:
volume = 1
mixer.music.set_volume(volume)
if key == keyboard.KeyCode(char='['):
pause += 1
if pause == 1:
mixer.music.pause()
pause = 2
if pause == 3:
mixer.music.unpause()
pause = 0
with keyboard.Listener(on_press=on_press) as listener: listener.join()
else:
Picker()
else:
pass
Picker()
Picker()
screenshot of code
I can't get it to work, i'm very new to python so i'm probably missing something
obvious
Before starting: Thx #JGreenwell for copying the code.
Ok, so first, I’ll help you clean your code.
Things that are wrong
Having all that ThisSong and NextSong things: It won’t get saved when you restart Picker(). Either just have ThisSong, or place the ThisSong and NextSong assignment with the volume and pause variables:
.
pause = 0 # this is a problem! Next point
volume = 0.5
ThisSong = random.choice(...)
NextSong = random.choice(...)
The pause variable should be a boolean (True/False) and the pausing code should be like this:
.
pause = not pause
if pause:
# pause
else:
# unpause
Also, it would ideally be called paused
on_press and the Listener declaration should be outside the while loop, because otherwise they just keep being declared every time it loops. Then you should import time and put time.sleep(500) or something like that in the while loop, so that it doesn’t check every fraction of second.
As for the next song not playing, I don’t really know, but I’d suggest entering Picker() in the shell after the program has run (python -i script.py from CMD, IDLE leaves you in a shell by default). I would also suggest really following recommendation #3 as having them in the loop may break the loop and stop it from finishing. Most importantly, I would ask you to debug your code by adding print in every step of your code:
print(1)
if something:
print(2)
etc...
And seeing where it blocks
P.S.: The random dots are because you can’t have code in lists, so I had to exit the list.
Working solution, incase anyone else has the same problem as me in the future :)
from pygame import mixer
from pynput import keyboard
import threading
import random
import os
paused = 0
def player():
song = random.choice(os.listdir("C:\\users\\...\\desktop\\music"))
mixer.init()
mixer.music.load("C:\\users\\...\\desktop\\music\\" + song)
mixer.music.play(0)
while mixer.music.get_busy():
pass
else:
player()
def main():
t = threading.Thread(target = player, name = 'thread1', args = ())
t.start()
main()
def on_press(key):
global paused
if key == keyboard.KeyCode(char='['):
paused = not paused
if paused:
mixer.music.pause()
else:
mixer.music.unpause()
with keyboard.Listener(on_press=on_press) as listener: listener.join()

python pyautogui working together with pynput

The subject is next, I´m doing some siple clickers with pyautogui but it lacks of control. Basically I want to be able to start and stop different scripts based on pyautogui. My idea was to combine the Listener function from pynput, but it doesn´t work properly . It starts when I press the assigned key, but I cannot stop it, why?
Here is some simle code:
from pynput.keyboard import Key, Controller, Listener
import time
import pyautogui as pg
pg.FAILSAFE = True
kb = Controller()
time.sleep(1)
def on_press(key):
if key == Key.space:
pg.position(500, 500)
x = 20
while key is not Key.enter:
pg.moveRel(x, 0, duration=0.2)
time.sleep(1)
with Listener(on_press=on_press) as listener:
listener.join()
I also have tried this loop:
while True:
if key==Key.enter:
pg.moveRel(x, 0, duration=0.2)
else:
return(False)
time.sleep(1)
but nothing works.
UPD:
Maybe someone can suggest me another module with controlling features, which can be good for clicker?
It's failing to stop because you are in a infinite loop when you do this:
while key is not Key.enter:
due to the fact that your on_press can't be called again and therefore the variable key will never change.
from pynput.keyboard import Key, Controller, Listener
import time
import pyautogui as pg
import threading
pg.FAILSAFE = True
kb = Controller()
time.sleep(1)
threadExitFlag = threading.Event()
threadVar = None
def mouse_move_thread(threadExitFlag):
pg.position(500, 500)
x = 20
while not threadExitFlag.is_set():
pg.moveRel(x, 0, duration=0.2)
time.sleep(1)
def on_press(key):
global threadExitFlag
if key == Key.space:
threadVar = threading.Thread(target=mouse_move_thread, args=[threadExitFlag]).start()
if key == Key.enter:
threadExitFlag.set()
#Turns this macro back on
elif key == Key.esc:
if threadExitFlag.is_set():
threadExitFlag.clear()
with Listener(on_press=on_press) as listener:
listener.join()
To use this, you press space to start your mouse movement, then you can stop it by pressing enter. After this, you need to press esc key to reset the event that stops it which means to do this macro twice in a row you need to press:
space (start the macro)
enter (stop/kill the macro)
esc (reset flag, if you press space after this you can start the macro again)
I've tested it and it works 100%.

Python 2.7 - How to check if SHIFT-Key or CTRL+Key is pressed?

I wrote a small routine in python 2.7, which waits for some time (say, 10 minutes) but immediately exits if a key is pressed by the the user.
I'm on Windows (7, 64-bit), so I tried using the msvcrt library:
import sys
import msvcrt
from time import sleep
def sleep_sec(secstosleep, allowskip = True):
waitinterval_ms = 0.050
nwaits = int(secstosleep * 1000 / waitinterval)
sys.stdout.write("\n Sleeping for %s seconds. You can press CTRL-F12 to skip.\n\n" % secstosleep)
for sl in range(0, nwaits + 1):
sleep(waitinterval_ms)
if allowskip:
# Check if User has pressed CTRL-F12 to stop waiting
if = msvcrt.kbhit():
kp = msvcrt.getch()
if kp == '\xe0':
print "\a" # Beep once
sys.stdout.write("\nSleep interrupted by User\n")
break
In effect it works quite well, except for the fact that it will break if the user hits either F12, F11 or Cursor Up key: I found out that if I press F12, getch() will return the '\xe0', but it seems the same code is also returned by the other mentioned keys; no way to trap CTRL, SHIFT, ALT, etc.
I would like to force the user pressing CTRL-F12, instead, to avoid quitting the wait by inadvertently hitting one of those keys.
Am I doing something wrong? Is there a way to intercept CTRL, ALT or SHIFT being pressed along with another key?
Thanks in advance,
Max - Italy
I will suggest you to use keyboard module. It has a lot of features and is pretty simple to use. You can also use it to detect keypresses. Examples:
If you want to detect key a:
import keyboard as kb
while True:
if kb.is_pressed("a"):break
You can also use it to detect combinations. For example , if you want to check if a and b are pressed at the same time:
import keyboard as kb
while True:
if kb.is_pressed("a+b"):break
So , you can use to to detect if ctrl and f12 are pressed:
import keyboard as kb
while True:
if kb.is_pressed("ctrl+f12"):break
A sample according to your posted code:
import sys
import keyboard as kb
import time
def sleep_sec(secstosleep,allowskip=True):
t = time.time() #getting time in sec
sys.stdout.write("\n Sleeping for %s seconds. You can press CTRL-F12 to skip.\n\n"% secstosleep)
if allowskip:
while time.time() - t <= secstosleep: #while current time(in sec) - t is less than secstosleep
if kb.is_pressed("ctrl+f11"): #if ctrl and f12 are pressed
print "\a"
sys.stdout.write("\nSleep interrupted by User\n")
quit(0)
sleep_sec(10*60) # 10*60 == 10 min
From the msvcrt manual for msvcrt.getch():
[..] If the pressed key was a special function key, this will return '\000' or '\xe0'; the next call will return the keycode.
Just call getch() again to get the actual keycode
As suggested by Daniel, I've modified my code like this:
import sys
import msvcrt
from time import sleep
def sleep_sec(secstosleep, allowskip = True):
waitinterval_ms = 0.050
nwaits = int(secstosleep * 1000 / waitinterval)
sys.stdout.write("\n Sleeping for %s seconds. You can press CTRL-F12 to skip.\n\n" % secstosleep)
for sl in range(0, nwaits + 1):
sleep(waitinterval_ms)
if allowskip:
# Check if User has pressed CTRL-F12 to stop waiting
if = msvcrt.kbhit():
kp = msvcrt.getch()
if kp == '\xe0':
kp += msvcrt.getch()
if kp == '\xe0\x8a': # \x8a is the keycode for CTRL-F12
print "\a" # Beep once
sys.stdout.write("\nSleep interrupted by User\n")
break
I've also noticed that different keycodes are returned for F12, CTRL-F12 and SHIFT-F12, so keystrokes can be easily recognized.
Thanks again, Daniel!
Max.

Categories

Resources