Created
October 14, 2012 04:15
-
-
Save franzwong/3887273 to your computer and use it in GitHub Desktop.
Auto click button (Windows)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import ctypes | |
import time | |
EnumWindows = ctypes.windll.user32.EnumWindows | |
EnumChildWindows = ctypes.windll.user32.EnumChildWindows | |
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)) | |
GetWindowText = ctypes.windll.user32.GetWindowTextW | |
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW | |
IsWindowVisible = ctypes.windll.user32.IsWindowVisible | |
SendMessage = ctypes.windll.user32.SendMessageW | |
def getWindowText(hwnd): | |
length = GetWindowTextLength(hwnd) | |
buffer = ctypes.create_unicode_buffer(length + 1) | |
GetWindowText(hwnd, buffer, length + 1) | |
return buffer.value | |
def match(title, text, exactMatch): | |
match = False | |
if exactMatch: | |
if text == title: | |
match = True | |
else: | |
if 0 < title.find(text): | |
match = True | |
return match | |
def search(text, exactMatch, parentHwnd=None): | |
resultHwnd = [] | |
def enumProc(hwnd, lParam, text=text, exactMatch=exactMatch, resultHwnd=resultHwnd): | |
title = getWindowText(hwnd) | |
if match(title, text, exactMatch): | |
resultHwnd.append(hwnd) | |
return False | |
return True | |
if None == parentHwnd: | |
EnumWindows(EnumWindowsProc(enumProc), 0) | |
return resultHwnd | |
else: | |
EnumChildWindows(parentHwnd, EnumWindowsProc(enumProc), 0) | |
return resultHwnd | |
def clickButtonByHwnd(buttonHwnd): | |
SendMessage(buttonHwnd, 0x00F5, 0, 0) # 0x00F5 - BM_CLICK | |
def searchButton(windowTitle, buttonText): | |
exactMatch = True | |
for hwnd in search(windowTitle, exactMatch): | |
for buttonHwnd in search(buttonText, exactMatch, hwnd): | |
return buttonHwnd | |
return None | |
# interval - in terms of second | |
def timeIntervalCall(fn, interval): | |
import time | |
while True: | |
fn() | |
time.sleep(interval) | |
def clickButton(windowTitle, buttonText): | |
print 'attempt to find button...' | |
buttonHwnd = searchButton(windowTitle, buttonText) | |
if None != buttonHwnd: | |
print 'found' | |
clickButtonByHwnd(buttonHwnd) | |
else: | |
print 'not found' | |
if __name__ == '__main__': | |
if 4 > len(sys.argv): | |
print 'not enough parameter' | |
sys.exit() | |
windowTitle = sys.argv[1] | |
buttonText = sys.argv[2] | |
# interval - in term of second | |
interval = int(sys.argv[3]) | |
print 'title : %s' % windowTitle | |
print 'button text : %s' % buttonText | |
print 'time interval : %s' % interval | |
timeIntervalCall(lambda : clickButton(windowTitle, buttonText), interval) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment