def main():
    if auto.IsNT6orHigher:
        oriTitle = auto.GetConsoleOriginalTitle()
    else:
        oriTitle = auto.GetConsoleTitle()
    auto.SetConsoleTitle(auto.GetConsoleTitle() + ' | Ctrl+1,Ctrl+2, stop Ctrl+4')
    auto.Logger.ColorfullyWriteLine('Press <Color=Cyan>Ctrl+1 or Ctrl+2</Color> to start, press <Color=Cyan>ctrl+4</Color> to stop')
    auto.Logger.Write('\nUse UIAutomation in main thread:\n', auto.ConsoleColor.Yellow)
    auto.LogControl(auto.GetRootControl())
    auto.Logger.Write('\n')
    auto.RunByHotKey({(auto.ModifierKey.Control, auto.Keys.VK_1): test1,
                              (auto.ModifierKey.Control, auto.Keys.VK_2): test2},
                             (auto.ModifierKey.Control, auto.Keys.VK_4))
    auto.SetConsoleTitle(oriTitle)
def test1(stopEvent):
    """
    This function runs in a new thread triggered by hotkey.
    """
    auto.InitializeUIAutomationInCurrentThread()
    n = 0
    child = None
    auto.Logger.WriteLine('Use UIAutomation in another thread:', auto.ConsoleColor.Yellow)
    while True:
        if stopEvent.is_set():
            break
        if not child:
            n = 1
            child = auto.GetRootControl().GetFirstChildControl()
        auto.Logger.WriteLine(n, auto.ConsoleColor.Cyan)
        auto.LogControl(child)
        child = child.GetNextSiblingControl()
        n += 1
        stopEvent.wait(1)
    auto.UninitializeUIAutomationInCurrentThread()
    print('test1 exits')
예제 #3
0
def main():
    import getopt
    auto.Logger.Write('UIAutomation {} (Python {}.{}.{}, {} bit)\n'.format(
        auto.VERSION, sys.version_info.major, sys.version_info.minor,
        sys.version_info.micro, 64 if sys.maxsize > 0xFFFFFFFF else 32))
    options, args = getopt.getopt(sys.argv[1:], 'hrfcand:t:', [
        'help', 'root', 'focus', 'cursor', 'ancestor', 'showAllName', 'depth=',
        'time='
    ])
    root = False
    focus = False
    cursor = False
    ancestor = False
    foreground = True
    showAllName = False
    depth = 0xFFFFFFFF
    seconds = 3
    for (o, v) in options:
        if o in ('-h', '-help'):
            usage()
            exit(0)
        elif o in ('-r', '-root'):
            root = True
            foreground = False
        elif o in ('-f', '-focus'):
            focus = True
            foreground = False
        elif o in ('-c', '-cursor'):
            cursor = True
            foreground = False
        elif o in ('-a', '-ancestor'):
            ancestor = True
            foreground = False
        elif o in ('-n', '-showAllName'):
            showAllName = True
        elif o in ('-d', '-depth'):
            depth = int(v)
        elif o in ('-t', '-time'):
            seconds = int(v)
    if seconds > 0:
        auto.Logger.Write('please wait for {0} seconds\n\n'.format(seconds),
                          writeToFile=False)
        time.sleep(seconds)
    auto.Logger.Log('Starts, Current Cursor Position: {}'.format(
        auto.GetCursorPos()))
    control = None
    if root:
        control = auto.GetRootControl()
    if focus:
        control = auto.GetFocusedControl()
    if cursor:
        control = auto.ControlFromCursor()
        if depth < 0:
            while depth < 0 and control:
                control = control.GetParentControl()
                depth += 1
            depth = 0xFFFFFFFF
    if ancestor:
        control = auto.ControlFromCursor()
        if control:
            auto.EnumAndLogControlAncestors(control, showAllName)
        else:
            auto.Logger.Write(
                'IUIAutomation returns null element under cursor\n',
                auto.ConsoleColor.Yellow)
    else:
        indent = 0
        if not control:
            control = auto.GetFocusedControl()
            controlList = []
            while control:
                controlList.insert(0, control)
                control = control.GetParentControl()
            if len(controlList) == 1:
                control = controlList[0]
            else:
                control = controlList[1]
                if foreground:
                    indent = 1
                    auto.LogControl(controlList[0], 0, showAllName)
        auto.EnumAndLogControl(control, depth, showAllName, startDepth=indent)
    auto.Logger.Log('Ends\n')