예제 #1
0
def _click_pattern(pattern,
                   clicks=None,
                   duration=None,
                   in_region=None,
                   button=None):
    """Click on center or offset of a Pattern.

    :param pattern: Input Pattern.
    :param clicks: Number of mouse clicks.
    :param duration: Speed of hovering from current location to target.
    :param in_region: Region object in order to minimize the area.
    :param button: Mouse button clicked (can be left, right, middle, 1, 2, 3).
    :return: None.
    """

    if duration is None:
        duration = Settings.move_mouse_delay

    width, height = pattern.get_size()

    p_top = positive_image_search(pattern=pattern, region=in_region)

    if p_top is None:
        raise FindError('Unable to click on: %s' % pattern.get_file_path())

    possible_offset = pattern.get_target_offset()

    if possible_offset is not None:
        _click_at(
            Location(p_top.x + possible_offset.x, p_top.y + possible_offset.y),
            clicks, duration, button)
    else:
        _click_at(Location(p_top.x + width / 2, p_top.y + height / 2), clicks,
                  duration, button)
예제 #2
0
def _mouse_press_release(where=None, action=None, button=None, in_region=None):
    """Mouse press/release.

    :param where: Location , image name or Pattern.
    :param action: 'press' or 'release'.
    :param button: 'left','right' or 'middle'.
    :param in_region: Region object in order to minimize the area.
    :return: None.
    """
    if isinstance(where, Pattern):
        needle = cv2.imread(where.get_file_path())
        height, width, channels = needle.shape
        p_top = positive_image_search(pattern=where, region=in_region)
        if p_top is None:
            raise FindError('Unable to click on: %s' % where.get_file_path())
        possible_offset = where.get_target_offset()
        if possible_offset is not None:
            location = Location(p_top.x + possible_offset.x,
                                p_top.y + possible_offset.y)
            pyautogui.moveTo(location.x, location.y)
            if action == 'press':
                pyautogui.mouseDown(location.x, location.y, button)
            elif action == 'release':
                pyautogui.mouseUp(location.x, location.y, button)
        else:
            location = Location(p_top.x + width / 2, p_top.y + height / 2)
            pyautogui.moveTo(location.x, location.y)
            if action == 'press':
                pyautogui.mouseDown(location.x, location.y, button)
            elif action == 'release':
                pyautogui.mouseUp(location.x, location.y, button)
    elif isinstance(where, str):
        mouse = Controller()
        ocr_search = OCRSearch()
        a_match = ocr_search.text_search_by(where, True, in_region)
        if a_match is not None:
            location = Location(a_match['x'] + a_match['width'] / 2,
                                a_match['y'] + a_match['height'] / 2)
            mouse.move(location.x, location.y)
            if action == 'press':
                mouse.press(button)
            elif mouse == 'release':
                mouse.release(button)