I want this code to remain open in the background and affect the game. However, this code only works on the desktop and not in the game.
<code>import time
import threading
import pyautogui
from pynput import mouse
press_time = None
hold_thread = None
stop_movement = False
def check_hold():
global press_time, stop_movement
while press_time:
elapsed_time = time.time() - press_time
if elapsed_time >= 0.2:
print("Hold")
stop_movement = False
# Move the mouse downwards for 1 second
pyautogui.moveRel(0, 100, duration=1)
if stop_movement:
return
# Move the mouse left for 1 second
pyautogui.moveRel(-100, 0, duration=1)
if stop_movement:
return
press_time = None
time.sleep(0.01)
def on_click(x, y, button, pressed):
global press_time, hold_thread, stop_movement
if button == mouse.Button.left:
if pressed:
press_time = time.time()
print("Pressed")
hold_thread = threading.Thread(target=check_hold)
hold_thread.start()
else:
press_time = None
stop_movement = True
if hold_thread:
hold_thread.join()
hold_thread = None
# Set up the listener
with mouse.Listener(on_click=on_click) as listener:
listener.join()
</code>
<code>import time
import threading
import pyautogui
from pynput import mouse
press_time = None
hold_thread = None
stop_movement = False
def check_hold():
global press_time, stop_movement
while press_time:
elapsed_time = time.time() - press_time
if elapsed_time >= 0.2:
print("Hold")
stop_movement = False
# Move the mouse downwards for 1 second
pyautogui.moveRel(0, 100, duration=1)
if stop_movement:
return
# Move the mouse left for 1 second
pyautogui.moveRel(-100, 0, duration=1)
if stop_movement:
return
press_time = None
time.sleep(0.01)
def on_click(x, y, button, pressed):
global press_time, hold_thread, stop_movement
if button == mouse.Button.left:
if pressed:
press_time = time.time()
print("Pressed")
hold_thread = threading.Thread(target=check_hold)
hold_thread.start()
else:
press_time = None
stop_movement = True
if hold_thread:
hold_thread.join()
hold_thread = None
# Set up the listener
with mouse.Listener(on_click=on_click) as listener:
listener.join()
</code>
import time
import threading
import pyautogui
from pynput import mouse
press_time = None
hold_thread = None
stop_movement = False
def check_hold():
global press_time, stop_movement
while press_time:
elapsed_time = time.time() - press_time
if elapsed_time >= 0.2:
print("Hold")
stop_movement = False
# Move the mouse downwards for 1 second
pyautogui.moveRel(0, 100, duration=1)
if stop_movement:
return
# Move the mouse left for 1 second
pyautogui.moveRel(-100, 0, duration=1)
if stop_movement:
return
press_time = None
time.sleep(0.01)
def on_click(x, y, button, pressed):
global press_time, hold_thread, stop_movement
if button == mouse.Button.left:
if pressed:
press_time = time.time()
print("Pressed")
hold_thread = threading.Thread(target=check_hold)
hold_thread.start()
else:
press_time = None
stop_movement = True
if hold_thread:
hold_thread.join()
hold_thread = None
# Set up the listener
with mouse.Listener(on_click=on_click) as listener:
listener.join()
I am not a python expert and I wrote this code by combining codes from here and there. This code works without any problems on the desktop, but it does not work when I switch to the game. How can I make this code work while playing the game?
New contributor
Lublu Emre Kele Otdihat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2