def drag_and_drop( element_path1: UI_Selector, element_path2: UI_Selector, duration: Optional[float] = None, mode: Enum = MoveMode.linear, button: ButtonLocation = ButtonLocation.left, timeout: Optional[float] = None) -> PYWINAUTO_Wrapper: """ Drags and drops from element_path1 to element_path2. :param element_path1: source element path :param element_path2: destination element path :param duration: duration in seconds of the mouse move (it doesn't take into account the time it takes to find) (if duration is -1 the mouse cursor doesn't move, it just sends WM_CLICK window message, useful for minimized or non-active window). :param mode: move mouse mode: MoveMode.linear, MoveMode.x_first, MoveMode.y_first :param button: mouse button: ButtonLocation.left, ButtonLocation.middle, ButtonLocation.right :param timeout: period of time in seconds that will be allowed to find the element :return: Pywinauto wrapper found with element_path2 """ move(element_path1, duration=duration, mode=mode, timeout=timeout) if button == ButtonLocation.left: event_down = MOUSEEVENTF_LEFTDOWN event_up = MOUSEEVENTF_LEFTUP elif button == ButtonLocation.middle: event_down = MOUSEEVENTF_MIDDLEDOWN event_up = MOUSEEVENTF_MIDDLEUP elif button == ButtonLocation.right: event_down = MOUSEEVENTF_RIGHTDOWN event_up = MOUSEEVENTF_RIGHTUP win32api_mouse_event(event_down, 0, 0) unique_element = move(element_path2, duration=duration, timeout=timeout) win32api_mouse_event(event_up, 0, 0) return unique_element
def _move(x, y, xd, yd, duration=1, refresh_rate=25): """ It moves the mouse from (x, y) to (xd, yd) in a straight line, with a duration of `duration` seconds. :param x: The x-coordinate of the mouse cursor before the move :param y: The y-coordinate of the mouse cursor before the move :param xd: The x-coordinate of the mouse cursor after the move :param yd: The y-coordinate of the mouse cursor after the move :param duration: The time (in seconds) it takes to move the mouse from (x, y) to (xd, yd) :param refresh_rate: 25 Hz is the default refresh rate of the mouse move """ x_max = win32api_GetSystemMetrics(0) - 1 y_max = win32api_GetSystemMetrics(1) - 1 if duration > 0: samples = duration * refresh_rate dt = 1 / refresh_rate step_x = (xd - x) / samples step_y = (yd - y) / samples t0 = time.time() for i in range(int(samples)): x, y = x+step_x, y+step_y t1 = time.time() if t1-t0 > i*dt: continue time.sleep(dt) nx = int(x * 65535 / x_max) ny = int(y * 65535 / y_max) win32api_mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, nx, ny) nx = round(xd * 65535 / x_max) ny = round(yd * 65535 / y_max) win32api_mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, nx, ny)
def drag_and_drop(element_path1, element_path2, duration=0.5, mode=MoveMode.linear): unique_element = move(element_path1, duration=duration, mode=mode) win32api_mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0) move(element_path2, duration=duration, mode=mode) win32api_mouse_event(MOUSEEVENTF_LEFTUP, 0, 0) return unique_element
def _win32api_mouse_click(button: ButtonLocation = ButtonLocation.left, click_count: int = 1): """ Clicks the mouse. :param button: The button to click :param click_count: How many times to click, defaults to 1 """ if button == ButtonLocation.left: event_down = MOUSEEVENTF_LEFTDOWN event_up = MOUSEEVENTF_LEFTUP elif button == ButtonLocation.middle: event_down = MOUSEEVENTF_MIDDLEDOWN event_up = MOUSEEVENTF_MIDDLEUP elif button == ButtonLocation.right: event_down = MOUSEEVENTF_RIGHTDOWN event_up = MOUSEEVENTF_RIGHTUP for _ in range(click_count): win32api_mouse_event(event_down, 0, 0) time.sleep(.01) win32api_mouse_event(event_up, 0, 0) time.sleep(.1)
def right_drag_and_drop( element_path1: UI_Element, element_path2: UI_Element, duration: Optional[float] = None, mode: Enum = MoveMode.linear, timeout: float = 120) -> UI_Element: """ Drags and drop with right button pressed from element_path1 to element_path2. :param element_path1: element path :param element_path2: element path :param duration: duration in seconds of the mouse move :param mode: move mouse mode :param timeout: period of time in seconds that will be allowed to find the element :return: Pywinauto wrapper with element_path2 """ if not duration: duration = PlayerSettings.mouse_move_duration move(element_path1, duration=duration, mode=mode, timeout=timeout) win32api_mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0) unique_element = move(element_path2, duration=duration, mode=mode, timeout=timeout) win32api_mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0) return unique_element
def mouse_wheel(steps: int, pause: float = 0.05) -> None: """ Turns the mouse wheel up or down. :param steps: number of wheel steps, if positive the mouse wheel is turned up else it is turned down :param pause: pause in seconds between each wheel step """ if pause == 0: win32api_mouse_event(MOUSEEVENTF_WHEEL, 0, 0, WHEEL_DELTA * steps, 0) else: for _ in range(abs(steps)): if steps > 0: win32api_mouse_event(MOUSEEVENTF_WHEEL, 0, 0, WHEEL_DELTA, 0) else: win32api_mouse_event(MOUSEEVENTF_WHEEL, 0, 0, -WHEEL_DELTA, 0) time.sleep(pause)
def click( element_path: Optional[UI_Element] = None, duration: Optional[float] = 0.5, mode: Enum = MoveMode.linear, button: str = 'left', timeout: float = 120, wait_ready: bool = True) -> UI_Element: """ Clicks on element :param element_path: element path :param duration: duration in seconds of the mouse move :param mode: move mouse mode :param button: mouse button: 'left','double_left', 'triple_left', 'right' :param timeout: period of time in seconds that will be allowed to find the element :param wait_ready: if True waits until the element is ready :return: Pywinauto wrapper of clicked element """ if element_path: unique_element = move(element_path, duration=duration, mode=mode, timeout=timeout) if wait_ready and isinstance(element_path, str): wait_is_ready_try1(unique_element, timeout=timeout) else: unique_element = None if button == 'left' or button == 'double_left' or button == 'triple_left': win32api_mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0) time.sleep(.01) win32api_mouse_event(MOUSEEVENTF_LEFTUP, 0, 0) time.sleep(.1) if button == 'double_left' or button == 'triple_left': win32api_mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0) time.sleep(.01) win32api_mouse_event(MOUSEEVENTF_LEFTUP, 0, 0) time.sleep(.1) if button == 'triple_left': win32api_mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0) time.sleep(.01) win32api_mouse_event(MOUSEEVENTF_LEFTUP, 0, 0) if button == 'right': win32api_mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0) time.sleep(.01) win32api_mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0) time.sleep(.01) if element_path: return unique_element else: return None
def move( element_path: UI_Element, duration: Optional[float] = 0.5, mode: Enum = MoveMode.linear, timeout: float = 120) -> UI_Element: """ Moves on element :param element_path: element path :param duration: duration in seconds of the mouse move :param mode: move mouse mode :param timeout: period of time in seconds that will be allowed to find the element :return: Pywinauto wrapper of clicked element """ global unique_element_old global element_path_old global w_rOLD x, y = win32api_GetCursorPos() if isinstance(element_path, str): element_path2 = element_path if Region.common_path: if Region.common_path != element_path[0:len(Region.common_path)]: element_path2 = Region.common_path + path_separator + element_path entry_list = get_entry_list(element_path2) if element_path2 == element_path_old: w_r = w_rOLD unique_element = unique_element_old else: unique_element = find(element_path, timeout=timeout) w_r = unique_element.rectangle() control_type = None for entry in entry_list: _, control_type, _, _ = get_entry(entry) if control_type == 'Menu': break if control_type == 'Menu': entry_list_old = get_entry_list(element_path_old) control_type_old = None for entry in entry_list_old: _, control_type_old, _, _ = get_entry(entry) if control_type_old == 'Menu': break if control_type_old == 'Menu': mode = MoveMode.x_first else: mode = MoveMode.y_first xd, yd = w_r.mid_point() else: _, _, _, dx_dy = get_entry(entry_list[-1]) if dx_dy: dx, dy = dx_dy[0], dx_dy[1] else: dx, dy = 0, 0 xd, yd = w_r.mid_point() xd, yd = xd + round(dx/100.0*(w_r.width()/2-1), 0), round(yd + dy/100.0*(w_r.height()/2-1), 0) elif issubclass(type(element_path), pywinauto.base_wrapper.BaseWrapper): unique_element = element_path element_path2 = get_wrapper_path(unique_element) w_r = unique_element.rectangle() xd, yd = w_r.mid_point() else: (xd, yd) = element_path unique_element = None x_max = win32api_GetSystemMetrics(0) - 1 y_max = win32api_GetSystemMetrics(1) - 1 if (x, y) != (xd, yd) and duration > 0: dt = 0.01 samples = duration/dt step_x = (xd-x)/samples step_y = (yd-y)/samples if mode == MoveMode.x_first: for i in range(int(samples)): x = x+step_x time.sleep(0.01) nx = int(x * 65535 / x_max) ny = int(y * 65535 / y_max) win32api_mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, nx, ny) step_x = 0 if mode == MoveMode.y_first: for i in range(int(samples)): y = y+step_y time.sleep(0.01) nx = int(x * 65535 / x_max) ny = int(y * 65535 / y_max) win32api_mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, nx, ny) step_y = 0 for i in range(int(samples)): x, y = x+step_x, y+step_y time.sleep(0.01) nx = int(x * 65535 / x_max) ny = int(y * 65535 / y_max) win32api_mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, nx, ny) nx = round(xd * 65535 / x_max) ny = round(yd * 65535 / y_max) win32api_mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, nx, ny) if unique_element is None: return None unique_element_old = unique_element element_path_old = element_path2 w_rOLD = w_r return unique_element
def mouse_wheel(steps): win32api_mouse_event(MOUSEEVENTF_WHEEL, 0, 0, WHEEL_DELTA * steps, 0)
def click(element_path, duration=0.5, mode=MoveMode.linear, button='left'): unique_element = move(element_path, duration=duration, mode=mode) if isinstance(element_path, string_types): wait_is_ready_try1(unique_element, timeout=60 * 5) else: unique_element = None if button == 'left' or button == 'double_left' or button == 'triple_left': win32api_mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0) time.sleep(.01) win32api_mouse_event(MOUSEEVENTF_LEFTUP, 0, 0) time.sleep(.1) if button == 'double_left' or button == 'triple_left': win32api_mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0) time.sleep(.01) win32api_mouse_event(MOUSEEVENTF_LEFTUP, 0, 0) time.sleep(.1) if button == 'triple_left': win32api_mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0) time.sleep(.01) win32api_mouse_event(MOUSEEVENTF_LEFTUP, 0, 0) if button == 'right': win32api_mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0) time.sleep(.01) win32api_mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0) time.sleep(.01) return unique_element
def move(element_path, duration=0.5, mode=MoveMode.linear): global unique_element_old global element_path_old global w_rOLD x, y = win32api_GetCursorPos() if isinstance(element_path, string_types): element_path2 = element_path if Region.common_path: if Region.common_path != element_path[0:len(Region.common_path)]: element_path2 = Region.common_path + path_separator + element_path entry_list = get_entry_list(element_path2) if element_path2 == element_path_old: w_r = w_rOLD unique_element = unique_element_old else: unique_element = find(element_path) w_r = unique_element.rectangle() control_type = None for entry in entry_list: _, control_type, _, _ = get_entry(entry) if control_type == 'Menu': break if control_type == 'Menu': entry_list_old = get_entry_list(element_path_old) control_type_old = None for entry in entry_list_old: _, control_type_old, _, _ = get_entry(entry) if control_type_old == 'Menu': break if control_type_old == 'Menu': mode = MoveMode.x_first else: mode = MoveMode.y_first xd, yd = w_r.mid_point() else: _, _, _, dx_dy = get_entry(entry_list[-1]) if dx_dy: dx, dy = dx_dy[0], dx_dy[1] else: dx, dy = 0, 0 xd, yd = w_r.mid_point() xd, yd = xd + round(dx / 100.0 * (w_r.width() - 1), 0), round( yd + dy / 100.0 * (w_r.height() - 1), 0) else: (xd, yd) = element_path unique_element = None x_max = win32api_GetSystemMetrics(0) - 1 y_max = win32api_GetSystemMetrics(1) - 1 if (x, y) != (xd, yd) and duration > 0: dt = 0.01 samples = duration / dt step_x = (xd - x) / samples step_y = (yd - y) / samples if mode == MoveMode.x_first: for i in range(int(samples)): x = x + step_x time.sleep(0.01) nx = int(x * 65535 / x_max) ny = int(y * 65535 / y_max) win32api_mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, nx, ny) step_x = 0 if mode == MoveMode.y_first: for i in range(int(samples)): y = y + step_y time.sleep(0.01) nx = int(x * 65535 / x_max) ny = int(y * 65535 / y_max) win32api_mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, nx, ny) step_y = 0 for i in range(int(samples)): x, y = x + step_x, y + step_y time.sleep(0.01) nx = int(x * 65535 / x_max) ny = int(y * 65535 / y_max) win32api_mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, nx, ny) nx = round(xd * 65535 / x_max) ny = round(yd * 65535 / y_max) win32api_mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, nx, ny) if unique_element is None: return None unique_element_old = unique_element element_path_old = element_path2 w_rOLD = w_r return unique_element