Skip to content

Instantly share code, notes, and snippets.

@xiada
Forked from franzwong/autoclick.py
Last active August 29, 2015 14:27
Show Gist options
  • Save xiada/9e364ab4c9d946f5e10c to your computer and use it in GitHub Desktop.
Save xiada/9e364ab4c9d946f5e10c to your computer and use it in GitHub Desktop.

Revisions

  1. @franzwong franzwong revised this gist Oct 14, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion autoclick.py
    Original file line number Diff line number Diff line change
    @@ -85,4 +85,4 @@ def clickButton(windowTitle, buttonText):
    print 'time interval : %s' % interval
    print

    timeIntervalCall(lambda : clickButton(windowTitle, buttonText), interval)
    timeIntervalCall(lambda : clickButton(windowTitle, buttonText), interval)
  2. @franzwong franzwong revised this gist Oct 14, 2012. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion autoclick.py
    Original file line number Diff line number Diff line change
    @@ -54,7 +54,7 @@ def searchButton(windowTitle, buttonText):
    return buttonHwnd
    return None

    # interval - in term of second
    # interval - in terms of second
    def timeIntervalCall(fn, interval):
    import time
    while True:
    @@ -77,6 +77,7 @@ def clickButton(windowTitle, buttonText):

    windowTitle = sys.argv[1]
    buttonText = sys.argv[2]
    # interval - in term of second
    interval = int(sys.argv[3])

    print 'title : %s' % windowTitle
  3. @franzwong franzwong revised this gist Oct 14, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions autoclick.py
    Original file line number Diff line number Diff line change
    @@ -49,8 +49,8 @@ def clickButtonByHwnd(buttonHwnd):

    def searchButton(windowTitle, buttonText):
    exactMatch = True
    for hwnd in search(windowTitle, exactMatch ):
    for buttonHwnd in search(buttonText, exactMatch , hwnd):
    for hwnd in search(windowTitle, exactMatch):
    for buttonHwnd in search(buttonText, exactMatch, hwnd):
    return buttonHwnd
    return None

  4. @franzwong franzwong revised this gist Oct 14, 2012. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions autoclick.py
    Original file line number Diff line number Diff line change
    @@ -48,8 +48,9 @@ def clickButtonByHwnd(buttonHwnd):
    SendMessage(buttonHwnd, 0x00F5, 0, 0) # 0x00F5 - BM_CLICK

    def searchButton(windowTitle, buttonText):
    for hwnd in search(windowTitle, True):
    for buttonHwnd in search(buttonText, True, hwnd):
    exactMatch = True
    for hwnd in search(windowTitle, exactMatch ):
    for buttonHwnd in search(buttonText, exactMatch , hwnd):
    return buttonHwnd
    return None

    @@ -83,4 +84,4 @@ def clickButton(windowTitle, buttonText):
    print 'time interval : %s' % interval
    print

    timeIntervalCall(lambda : clickButton(windowTitle, buttonText), interval)
    timeIntervalCall(lambda : clickButton(windowTitle, buttonText), interval)
  5. @franzwong franzwong created this gist Oct 14, 2012.
    86 changes: 86 additions & 0 deletions autoclick.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    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):
    for hwnd in search(windowTitle, True):
    for buttonHwnd in search(buttonText, True, hwnd):
    return buttonHwnd
    return None

    # interval - in term 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 = int(sys.argv[3])

    print 'title : %s' % windowTitle
    print 'button text : %s' % buttonText
    print 'time interval : %s' % interval
    print

    timeIntervalCall(lambda : clickButton(windowTitle, buttonText), interval)