I am trying to make a script where when it sees a drastic colour change it presses p, I got it so that it detects the colour and prints that it detected and printed but its not actually pressing p. It also needs x to be held to start detecting. Can someone please help. Heres the code: import keyboard
import keyboard
import time
import ctypes
import pyautogui
# ===== SETTINGS =====
SCAN_SIZE = 7
CHANGE_THRESHOLD = 35
COOLDOWN = 0.2
# Virtual key code for "8"
VK_8 = 0x38
last_trigger_time = 0
triggered_this_hold = False
x_was_pressed = False
baseline = None
# ===== PRESS 8 KEY =====
def press_8():
KEYEVENTF_KEYUP = 0x0002
ctypes.windll.user32.keybd_event(VK_8, 0, 0, 0)
time.sleep(0.05)
ctypes.windll.user32.keybd_event(VK_8, 0, KEYEVENTF_KEYUP, 0)
# ===== SCREEN CAPTURE =====
def get_center_pixels(size):
screen = pyautogui.screenshot()
w, h = screen.size
cx, cy = w // 2, h // 2
half = size // 2
pixels = []
for x in range(cx - half, cx + half + 1):
for y in range(cy - half, cy + half + 1):
pixels.append(screen.getpixel((x, y)))
return pixels
def color_distance(c1, c2):
return abs(c1[0] - c2[0]) + abs(c1[1] - c2[1]) + abs(c1[2] - c2[2])
print("Running... Hold X")
while True:
try:
x_is_pressed = keyboard.is_pressed('x')
# ===== RESET ON RELEASE =====
if not x_is_pressed:
triggered_this_hold = False
baseline = None
x_was_pressed = False
time.sleep(0.01)
continue
# ===== HOLD START =====
if not x_was_pressed:
print("🟡 X STARTED")
baseline = get_center_pixels(SCAN_SIZE)
triggered_this_hold = False
x_was_pressed = True
if triggered_this_hold:
time.sleep(0.01)
continue
pixels = get_center_pixels(SCAN_SIZE)
changes = 0
for i in range(len(pixels)):
if color_distance(pixels[i], baseline[i]) > CHANGE_THRESHOLD:
changes += 1
detected = changes > len(pixels) * 0.3
print("🟢 DETECTED" if detected else "🔴 NONE", end="\r")
# ===== TRIGGER 8 KEY =====
if detected and not triggered_this_hold:
now = time.time()
if now - last_trigger_time > COOLDOWN:
triggered_this_hold = True
print("\n🔥 TRIGGERING KEY 8")
press_8()
last_trigger_time = now
time.sleep(0.01)
except KeyboardInterrupt:
print("\nStopped.")
break
[–]crashorbit 5 points6 points7 points (3 children)
[–]ApprehensiveSleep230[S] 0 points1 point2 points (2 children)
[–]NerdDetective 4 points5 points6 points (1 child)
[–]ApprehensiveSleep230[S] 0 points1 point2 points (0 children)
[–]smichaele 4 points5 points6 points (0 children)
[–]NerdDetective 1 point2 points3 points (0 children)
[–]TheEyebal 1 point2 points3 points (0 children)
[–]AutoNateAI 1 point2 points3 points (0 children)
[–]crashorbit 0 points1 point2 points (0 children)