import pyautogui
import keyboard
import time
running = True
print("Script will start in 5 seconds.")
time.sleep(5) # Wait for 5 seconds before starting
while running:
keyboard.press_and_release('x') # Press 'x' before every color detection
# Capture the screen and get the color at the specified position
screenshot = pyautogui.screenshot()
target_color = (255, 0, 0) # Replace with the RGB values of your target color
target_position = (100, 100) # Replace with the coordinates of the position to monitor
# The pixel_color format is (red, green, blue, alpha)
pixel_color = screenshot.getpixel(target_position)
# Check if the pixel color matches the target color
if pixel_color == target_color:
time.sleep(1) # Adjust this delay as needed to avoid rapid repeated key presses
if keyboard.is_pressed('F10'):
running = False
print("Script stopped by user.")
I want it to press x, then wait until the colour is detected, and then press x and repeat those 2 actions, but it would either spam x or just press x once, and that's it.
I tried doing everything I could and researching, but I'm still fairly new, and I don't know how.
A required package for
pyautoguiispyscreeze.pyscreezecan get the color of the pixel for you.With this you don't need the 5 second delay for getting the pixel at the coordinates because
pynput.keyboard.listenerruns in it's own thread.if you decide you want to change the hotkeys for getting the pixel you can use this to get the key codes that aren't constants in
pynput.keyboard.Key.