def size(): """Returns the width and height of the screen as a two-integer tuple. Returns: (width, height) tuple of the screen size, in pixels. """ return platformModule._size()
def dragMouse(x, y, button, duration): width, height = platformModule._size() startx, starty = platformModule._position() # None values means "use current position". Convert x and y to ints. x = startx if x is None else int(x) y = starty if y is None else int(y) # Make sure x and y are within the screen bounds. if x < 0: x = 0 elif x >= width: x = width - 1 if y < 0: y = 0 elif y >= height: y = height - 1 segments = max(width, height) timeSegment = float(duration) / segments while timeSegment < 0.05: # if timeSegment is too short, let's decrease the amount we divide it by. Otherwise the time.sleep() will be a no-op and the mouse cursor moves there instantly. segments = int(segments * 0.9) # decrease segments by 90%. timeSegment = float(duration) / segments for n in range(segments): time.sleep(timeSegment) pointOnLine = linear(float(n) / segments) tweenX, tweenY = getPointOnLine(startx, starty, x, y, pointOnLine) tweenX, tweenY = int(tweenX), int(tweenY) # only OS X needs the drag event specifically platformModule._dragTo(tweenX, tweenY, button) # Ensure that no matter what the tween function returns, the mouse ends up # at the final destination. platformModule._dragTo(x, y, button)
def onScreen(*args): """Returns whether the given xy coordinates are on the screen or not. Args: Either the arguments are two separate values, first arg for x and second for y, or there is a single argument of a sequence with two values, the first x and the second y. Example: onScreen(x, y) or onScreen([x, y]) Returns: bool: True if the xy coordinates are on the screen at its current resolution, otherwise False. """ if len(args) == 2: # args passed as onScreen(x, y) x = int(args[0]) y = int(args[1]) else: # args pass as onScreen([x, y]) x = int(args[0][0]) y = int(args[0][1]) width, height = platformModule._size() return x >= 0 and y >= 0 and x < width and y < height
def _mouseMoveDragTo(moveOrDrag, x, y, duration, tween, button=None): """Handles the actual move or drag event, since different platforms implement them differently. On Windows & Linux, a drag is a normal mouse move while a mouse button is held down. On OS X, a distinct "drag" event must be used instead. The code for moving and dragging the mouse is similar, so this function handles both. Users should call the moveTo() or dragTo() functions instead of calling _mouseMoveDragTo(). Args: moveOrDrag (str): Either 'move' or 'drag', for the type of action this is. x (int, float, None, optional): How far left (for negative values) or right (for positive values) to move the cursor. 0 by default. y (int, float, None, optional): How far up (for negative values) or down (for positive values) to move the cursor. 0 by default. duration (float, optional): The amount of time it takes to move the mouse cursor to the new xy coordinates. If 0, then the mouse cursor is moved instantaneously. 0.0 by default. tween (func, optional): The tweening function used if the duration is not 0. A linear tween is used by default. See the tweens.py file for details. button (str, int, optional): The mouse button clicked. Must be one of 'left', 'middle', 'right' (or 1, 2, or 3) respectively. 'left' by default. Returns: None """ # The move and drag code is similar, but OS X requires a special drag event instead of just a move event when dragging. # See https://stackoverflow.com/a/2696107/1893164 assert moveOrDrag in ('move', 'drag'), "moveOrDrag must be in ('move', 'drag'), not %s" % (moveOrDrag) if sys.platform != 'darwin': moveOrDrag = 'move' # only OS X needs to use the drag if x is None and y is None: return # special case for no mouse movement at all x, y = position(x, y) width, height = platformModule._size() startx, starty = platformModule._position() # None values means "use current position". Convert x and y to ints. x = startx if x is None else int(x) y = starty if y is None else int(y) # Make sure x and y are within the screen bounds. if x < 0: x = 0 elif x >= width: x = width - 1 if y < 0: y = 0 elif y >= height: y = height - 1 _failSafeCheck() # If the duration is small enough, just move the cursor there instantly. if duration <= MINIMUM_DURATION: if moveOrDrag == 'move': platformModule._moveTo(x, y) else: platformModule._dragTo(x, y, button) return # Non-instant moving/dragging involves tweening: segments = max(width, height) timeSegment = duration / segments while timeSegment < 0.05: # if timeSegment is too short, let's decrease the amount we divide it by. Otherwise the time.sleep() will be a no-op and the mouse cursor moves there instantly. segments = int(segments * 0.9) # decrease segments by 90%. timeSegment = duration / segments for n in range(segments): time.sleep(timeSegment) _failSafeCheck() pointOnLine = tween(n / segments) tweenX, tweenY = getPointOnLine(startx, starty, x, y, pointOnLine) tweenX, tweenY = int(tweenX), int(tweenY) if moveOrDrag == 'move': platformModule._moveTo(tweenX, tweenY) else: # only OS X needs the drag event specifically platformModule._dragTo(tweenX, tweenY, button) # Ensure that no matter what the tween function returns, the mouse ends up # at the final destination. if moveOrDrag == 'move': platformModule._moveTo(x, y) else: platformModule._dragTo(x, y, button) _failSafeCheck()
def _mouseMoveDragTo(moveOrDrag, x, y, duration, tween, button=None): """Handles the actual move or drag event, since different platforms implement them differently. On Windows & Linux, a drag is a normal mouse move while a mouse button is held down. On OS X, a distinct "drag" event must be used instead. The code for moving and dragging the mouse is similar, so this function handles both. Users should call the moveTo() or dragTo() functions instead of calling _mouseMoveDragTo(). Args: moveOrDrag (str): Either 'move' or 'drag', for the type of action this is. x (int, float, None, optional): How far left (for negative values) or right (for positive values) to move the cursor. 0 by default. y (int, float, None, optional): How far up (for negative values) or down (for positive values) to move the cursor. 0 by default. duration (float, optional): The amount of time it takes to move the mouse cursor to the new xy coordinates. If 0, then the mouse cursor is moved instantaneously. 0.0 by default. tween (func, optional): The tweening function used if the duration is not 0. A linear tween is used by default. See the tweens.py file for details. button (str, int, optional): The mouse button clicked. Must be one of 'left', 'middle', 'right' (or 1, 2, or 3) respectively. 'left' by default. Returns: None """ # The move and drag code is similar, but OS X requires a special drag event instead of just a move event when dragging. # See https://stackoverflow.com/a/2696107/1893164 assert moveOrDrag in ( 'move', 'drag' ), "moveOrDrag must be in ('move', 'drag'), not %s" % (moveOrDrag) if sys.platform != 'darwin': moveOrDrag = 'move' # only OS X needs to use the drag if x is None and y is None: return # special case for no mouse movement at all x, y = position(x, y) width, height = platformModule._size() startx, starty = platformModule._position() # None values means "use current position". Convert x and y to ints. x = startx if x is None else int(x) y = starty if y is None else int(y) # Make sure x and y are within the screen bounds. if x < 0: x = 0 elif x >= width: x = width - 1 if y < 0: y = 0 elif y >= height: y = height - 1 _failSafeCheck() # If the duration is small enough, just move the cursor there instantly. if duration <= MINIMUM_DURATION: if moveOrDrag == 'move': platformModule._moveTo(x, y) else: platformModule._dragTo(x, y, button) return # Non-instant moving/dragging involves tweening: segments = max(width, height) timeSegment = float(duration) / segments while timeSegment < 0.05: # if timeSegment is too short, let's decrease the amount we divide it by. Otherwise the time.sleep() will be a no-op and the mouse cursor moves there instantly. segments = int(segments * 0.9) # decrease segments by 90%. timeSegment = float(duration) / segments for n in range(segments): time.sleep(timeSegment) _failSafeCheck() pointOnLine = tween(float(n) / segments) tweenX, tweenY = getPointOnLine(startx, starty, x, y, pointOnLine) tweenX, tweenY = int(tweenX), int(tweenY) if moveOrDrag == 'move': platformModule._moveTo(tweenX, tweenY) else: # only OS X needs the drag event specifically platformModule._dragTo(tweenX, tweenY, button) # Ensure that no matter what the tween function returns, the mouse ends up # at the final destination. if moveOrDrag == 'move': platformModule._moveTo(x, y) else: platformModule._dragTo(x, y, button) _failSafeCheck()