Problem with looping in python with pyautogui and keyboard

Question:

Hello my first time posting, I have a problem with python. So I have this code and it’s a keybind like when I press the number 1 i need to execute1() but after this run I don’t want the program to exit but just wait and run in the background and wait for another key to be pressed. With this program it keeps looping the execute(). How I can solve this?

 import threading
 import pyautogui 
 import time
 import pyscreeze
 from pynput import keyboard

 cmb = [{keyboard.Key.alt_l, keyboard.Key.page_up}] #keybind 
 current = set()

def execute1():
   pyautogui.moveTo(2459,122)
   pyautogui.leftClick()
   pyautogui.moveTo(2368,392)
   pyautogui.leftClick()
   pyautogui.moveTo(1542,447)
   pyautogui.leftClick()
   pyautogui.moveTo(1546,518)
   pyautogui.leftClick()
   pyautogui.moveTo(1649,635)
   pyautogui.leftClick()
   pyautogui.moveTo(2018,320)
   time.sleep(1.35)
   pyautogui.leftClick()
   pyautogui.typewrite("111")
   pyautogui.moveTo(2006,379)
   pyautogui.leftClick()

def execute2():
   pyautogui.moveTo(2459,122)
   pyautogui.leftClick()
   pyautogui.moveTo(2271,505)
   pyautogui.leftClick()
   pyautogui.moveTo(1542,447)
   pyautogui.leftClick()
   pyautogui.moveTo(1546,518)
   pyautogui.leftClick()
   pyautogui.moveTo(1649,635)
   pyautogui.leftClick()
   pyautogui.moveTo(2018,320)
   time.sleep(1.35)
   pyautogui.leftClick()
   pyautogui.typewrite("111")
   pyautogui.moveTo(2006,379)
   pyautogui.leftClick()

def execute3():
   pyautogui.moveTo(2459,122)
   pyautogui.leftClick()
   pyautogui.moveTo(2292,603)
   pyautogui.leftClick()
   pyautogui.moveTo(1542,447)
   pyautogui.leftClick()
   pyautogui.moveTo(1546,518)
   pyautogui.leftClick()
   pyautogui.moveTo(1649,635)
   pyautogui.leftClick()
   pyautogui.moveTo(2018,320)
   time.sleep(1.35)
   pyautogui.leftClick()
   pyautogui.typewrite("111")
   pyautogui.moveTo(2006,379)
   pyautogui.leftClick()

def on_press(key): #check when key presses
    if key == keyboard.KeyCode(char='1'): # check if '1' key is pressed
       execute1()
       current.add(key)
    elif key == keyboard.KeyCode(char='2'): # check if '2' key is pressed
        execute2()
        current.add(key)
    elif key == keyboard.KeyCode(char='3'): # check if '3' key is pressed
        execute3()
        current.add(key)

def on_release(key): #check when key releases
    if any([key in z for z in cmb]):
      current.remove(key)

with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
        listener.join()

I tried to do this:

def on_press(key): #check when key presses
    if key == keyboard.KeyCode(char='1'): # check if '1' key is pressed
      t = threading.Thread(target=execute1)
      t.start()
      t.join()
      current.add(key)

for all the executes but there was another problem, when it reached the pyautogui.typewrite("111") it just typed one of the characters but I wanted to be fast.

This program worked when I had only 1 execution but I want 6 of them now and I can’t find a solution

Answers:

You need to create a listener, but you are missing the correct way.

Here are a couple of examples…

Center the mouse in the middle of the screen when you press F7:

import pyautogui
import pynput.mouse
import pynput.keyboard

def center_mouse_on_press(key):
    if key == pynput.keyboard.Key.f7:
        screen_width, screen_height = pyautogui.size()
        center_x, center_y = screen_width / 2, screen_height / 2
        pyautogui.moveTo(center_x, center_y)

keyboard_listener = pynput.keyboard.Listener(on_press=center_mouse_on_press)
keyboard_listener.start()
keyboard_listener.join()

Move the mouse to different points in the screen depending on whether you press F7, F8 or F9:

import pyautogui
import pynput.mouse
import pynput.keyboard

def move_mouse_on_press(key):
    screen_width, screen_height = pyautogui.size()
    center_x, center_y = screen_width / 2, screen_height / 2
    if key == pynput.keyboard.Key.f7:
        pyautogui.moveTo(center_x, center_y/2)
    if key == pynput.keyboard.Key.f8:
        pyautogui.moveTo(center_x, center_y)
    if key == pynput.keyboard.Key.f9:
        pyautogui.moveTo(center_x, center_y*2)

keyboard_listener = pynput.keyboard.Listener(on_press=move_mouse_on_press)
keyboard_listener.start()
keyboard_listener.join()
Answered By: Pedro Rocha
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.