Skip to content

Instantly share code, notes, and snippets.

@SimonTheCoder
Last active October 13, 2021 01:49
Show Gist options
  • Save SimonTheCoder/b7e7987c694bfdcb858056f3f62a9890 to your computer and use it in GitHub Desktop.
Save SimonTheCoder/b7e7987c694bfdcb858056f3f62a9890 to your computer and use it in GitHub Desktop.
Auto CookieClicker. Press 'ALT' to stop clicker temporarily.
import win32gui
import win32api
import win32con
import re
import time
target_caption_reg = re.compile(".*Cookie Clicker.*")
target_hWnd = None
def enumWindowCallback(hWnd, arg1):
global target_hWnd
text = win32gui.GetWindowText(hWnd)
#print("Got:"+hex(hWnd)+ " text:"+text)
if target_caption_reg.match(text):
target_hWnd = hWnd
#return win32api.MAKEBOOL(True)
return False
else:
pass
#return win32api.MAKEBOOL(False)
return True
def click(hWnd, x, y):
#hWnd = win32gui.FindWindow("Chrome_RenderWidgetHostHWND",None)
#hWnd = win32gui.FindWindows("Chrome_WidgetWin_1",None)
#print(hex(hWnd))
#print("x,y: %d, %d" % (x,y))
#hWnd = 0x006e1516
lParam = win32api.MAKELONG(x, y)
win32api.SendMessage(hWnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
#win32api.SendMessage(hWnd, win32con.WM_PARENTNOTIFY, win32con.MK_LBUTTON, lParam)
win32api.SendMessage(hWnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, lParam)
try:
win32gui.EnumWindows(enumWindowCallback,None)
except BaseException as e:
if target_hWnd is not None:
print("Game window found:"+ hex(target_hWnd))
if target_hWnd is None:
print("Game window not found. Exit.")
exit(1)
print("Auto Click On.")
while True:
foreground_hWnd = win32gui.GetForegroundWindow()
if not win32api.GetAsyncKeyState(win32con.VK_MENU):
(left, top, right, bottom) = win32gui.GetWindowRect(target_hWnd)
if foreground_hWnd == target_hWnd and win32api.GetAsyncKeyState(win32con.VK_LBUTTON):
gx,gy = win32api.GetCursorPos()
# x = gx - left
# y = gy - top
(x, y) = win32gui.ScreenToClient(target_hWnd,(gx,gy))
# print("screentoclient @ %d,%d" % win32gui.ScreenToClient(target_hWnd,(gx,gy)))
print("Manual Click @ %d,%d" % (x,y))
#time.sleep(0.5)
else:
#print("left, top, right, bottom: %d,%d,%d,%d" % (left, top, right, bottom))
x = (right - left) * 385/2576
y = (bottom - top) * 545/1416
click(target_hWnd,int(x),int(y))
else:
x,y = win32api.GetCursorPos()
print("PAUSE mouse@%d,%d" % (x,y))
time.sleep(1/40)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment