Debian Codegraph @

docs/mouse.rst

raw
1 .. default-role:: code
2
3 =======================
4 Mouse Control Functions
5 =======================
6
7 The Screen and Mouse Position
8 =============================
9
10 Locations on your screen are referred to by X and Y Cartesian coordinates. The X coordinate starts at 0 on the left side and increases going right. Unlike in mathematics, the Y coordinate starts at 0 on the top and increases going down.
11
12 .. code::
13
14 0,0 X increases -->
15 +---------------------------+
16 | | Y increases
17 | | |
18 | 1920 x 1080 screen | |
19 | | V
20 | |
21 | |
22 +---------------------------+ 1919, 1079
23
24 The pixel at the top-left corner is at coordinates 0, 0. If your screen's resolution is 1920 x 1080, the pixel in the lower right corner will be 1919, 1079 (since the coordinates begin at 0, not 1).
25
26 The screen resolution size is returned by the ``size()`` function as a tuple of two integers. The current X and Y coordinates of the mouse cursor are returned by the ``position()`` function.
27
28 For example:
29
30 .. code:: python
31
32 >>> pyautogui.size()
33 (1920, 1080)
34 >>> pyautogui.position()
35 (187, 567)
36
37 Here is a short Python 3 program that will constantly print out the position of the mouse cursor:
38
39 .. code:: python
40
41 #! python3
42 import pyautogui, sys
43 print('Press Ctrl-C to quit.')
44 try:
45 while True:
46 x, y = pyautogui.position()
47 positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
48 print(positionStr, end='')
49 print('\b' * len(positionStr), end='', flush=True)
50 except KeyboardInterrupt:
51 print('\n')
52
53 Here is the Python 2 version:
54
55 .. code:: python
56
57 #! python
58 import pyautogui, sys
59 print('Press Ctrl-C to quit.')
60 try:
61 while True:
62 x, y = pyautogui.position()
63 positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
64 print positionStr,
65 print '\b' * (len(positionStr) + 2),
66 sys.stdout.flush()
67 except KeyboardInterrupt:
68 print '\n'
69
70 To check if XY coordinates are on the screen, pass them (either as two integer arguments or a single tuple/list arguments with two integers) to the ``onScreen()`` function, which will return ``True`` if they are within the screen's boundaries and ``False`` if not. For example:
71
72 .. code:: python
73
74 >>> pyautogui.onScreen(0, 0)
75 True
76 >>> pyautogui.onScreen(0, -1)
77 False
78 >>> pyautogui.onScreen(0, 99999999)
79 False
80 >>> pyautogui.size()
81 (1920, 1080)
82 >>> pyautogui.onScreen(1920, 1080)
83 False
84 >>> pyautogui.onScreen(1919, 1079)
85 True
86
87 Mouse Movement
88 ==============
89
90 The ``moveTo()`` function will move the mouse cursor to the X and Y integer coordinates you pass it. The ``None`` value can be passed for a coordinate to mean "the current mouse cursor position". For example:
91
92 .. code:: python
93
94 >>> pyautogui.moveTo(100, 200) # moves mouse to X of 100, Y of 200.
95 >>> pyautogui.moveTo(None, 500) # moves mouse to X of 100, Y of 500.
96 >>> pyautogui.moveTo(600, None) # moves mouse to X of 600, Y of 500.
97
98 Normally the mouse cursor will instantly move to the new coordinates. If you want the mouse to gradually move to the new location, pass a third argument for the duration (in seconds) the movement should take. For example:
99
100 .. code:: python
101
102 >>> pyautogui.moveTo(100, 200, 2) # moves mouse to X of 100, Y of 200 over 2 seconds
103
104 (If the duration is less than ``pyautogui.MINIMUM_DURATION`` the movement will be instant. By default, ``pyautogui.MINIMUM_DURATION`` is 0.1.)
105
106 If you want to move the mouse cursor over a few pixels *relative* to its current position, use the ``move()`` function. This function has similar parameters as ``moveTo()``. For example:
107
108 .. code:: python
109
110 >>> pyautogui.moveTo(100, 200) # moves mouse to X of 100, Y of 200.
111 >>> pyautogui.move(0, 50) # move the mouse down 50 pixels.
112 >>> pyautogui.move(-30, 0) # move the mouse left 30 pixels.
113 >>> pyautogui.move(-30, None) # move the mouse left 30 pixels.
114
115 Mouse Drags
116 ===========
117
118 PyAutoGUI's ``dragTo()`` and ``drag()`` functions have similar parameters as the ``moveTo()`` and ``move()`` functions. In addition, they have a ``button`` keyword which can be set to ``'left'``, ``'middle'``, and ``'right'`` for which mouse button to hold down while dragging. For example:
119
120 .. code:: python
121
122 >>> pyautogui.dragTo(100, 200, button='left') # drag mouse to X of 100, Y of 200 while holding down left mouse button
123 >>> pyautogui.dragTo(300, 400, 2, button='left') # drag mouse to X of 300, Y of 400 over 2 seconds while holding down left mouse button
124 >>> pyautogui.drag(30, 0, 2, button='right') # drag the mouse right 30 pixels over 2 seconds while holding down the right mouse button
125
126
127 Tween / Easing Functions
128 ========================
129
130 Tweening is an extra feature to make the mouse movements fancy. You can probably skip this section if you don't care about this.
131
132 A tween or easing function dictates the progress of the mouse as it moves to its destination. Normally when moving the mouse over a duration of time, the mouse moves directly towards the destination in a straight line at a constant speed. This is known as a *linear tween* or *linear easing* function.
133
134 PyAutoGUI has other tweening functions available in the ``pyautogui`` module. The ``pyautogui.easeInQuad`` function can be passed for the 4th argument to ``moveTo()``, ``move()``, ``dragTo()``, and ``drag()`` functions to have the mouse cursor start off moving slowly and then speeding up towards the destination. The total duration is still the same as the argument passed to the function. The ``pyautogui.easeOutQuad`` is the reverse: the mouse cursor starts moving fast but slows down as it approaches the destination. The ``pyautogui.easeOutElastic`` will overshoot the destination and "rubber band" back and forth until it settles at the destination.
135
136 For example:
137
138 .. code:: python
139
140 >>> pyautogui.moveTo(100, 100, 2, pyautogui.easeInQuad) # start slow, end fast
141 >>> pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad) # start fast, end slow
142 >>> pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad) # start and end fast, slow in middle
143 >>> pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce) # bounce at the end
144 >>> pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic) # rubber band at the end
145
146 These tweening functions are copied from Al Sweigart's PyTweening module: https://pypi.python.org/pypi/PyTweening https://github.com/asweigart/pytweening This module does not have to be installed to use the tweening functions.
147
148 If you want to create your own tweening function, define a function that takes a single float argument between ``0.0`` (representing the start of the mouse travelling) and ``1.0`` (representing the end of the mouse travelling) and returns a float value between ``0.0`` and ``1.0``.
149
150 Mouse Clicks
151 ============
152
153 The ``click()`` function simulates a single, left-button mouse click at the mouse's current position. A "click" is defined as pushing the button down and then releasing it up. For example:
154
155 .. code:: python
156
157 >>> pyautogui.click() # click the mouse
158
159 To combine a ``moveTo()`` call before the click, pass integers for the ``x`` and ``y`` keyword argument:
160
161 .. code:: python
162
163 >>> pyautogui.click(x=100, y=200) # move to 100, 200, then click the left mouse button.
164
165 To specify a different mouse button to click, pass ``'left'``, ``'middle'``, or ``'right'`` for the ``button`` keyword argument:
166
167 .. code:: python
168
169 >>> pyautogui.click(button='right') # right-click the mouse
170
171 To do multiple clicks, pass an integer to the ``clicks`` keyword argument. Optionally, you can pass a float or integer to the ``interval`` keyword argument to specify the amount of pause between the clicks in seconds. For example:
172
173 .. code:: python
174
175 >>> pyautogui.click(clicks=2) # double-click the left mouse button
176 >>> pyautogui.click(clicks=2, interval=0.25) # double-click the left mouse button, but with a quarter second pause in between clicks
177 >>> pyautogui.click(button='right', clicks=3, interval=0.25) ## triple-click the right mouse button with a quarter second pause in between clicks
178
179 As a convenient shortcut, the ``doubleClick()`` function will perform a double click of the left mouse button. It also has the optional ``x``, ``y``, ``interval``, and ``button`` keyword arguments. For example:
180
181 .. code:: python
182
183 >>> pyautogui.doubleClick() # perform a left-button double click
184
185 There is also a ``tripleClick()`` function with similar optional keyword arguments.
186
187 The ``rightClick()`` function has optional ``x`` and ``y`` keyword arguments.
188
189 The mouseDown() and mouseUp() Functions
190 =======================================
191
192 Mouse clicks and drags are composed of both pressing the mouse button down and releasing it back up. If you want to perform these actions separately, call the ``mouseDown()`` and ``mouseUp()`` functions. They have the same ``x``, ``y``, and ``button``. For example:
193
194 .. code:: python
195
196 >>> pyautogui.mouseDown(); pyautogui.mouseUp() # does the same thing as a left-button mouse click
197 >>> pyautogui.mouseDown(button='right') # press the right button down
198 >>> pyautogui.mouseUp(button='right', x=100, y=200) # move the mouse to 100, 200, then release the right button up.
199
200
201 Mouse Scrolling
202 ===============
203
204 The mouse scroll wheel can be simulated by calling the ``scroll()`` function and passing an integer number of "clicks" to scroll. The amount of scrolling in a "click" varies between platforms. Optionally, integers can be passed for the the ``x`` and ``y`` keyword arguments to move the mouse cursor before performing the scroll. For example:
205
206 .. code:: python
207
208 >>> pyautogui.scroll(10) # scroll up 10 "clicks"
209 >>> pyautogui.scroll(-10) # scroll down 10 "clicks"
210 >>> pyautogui.scroll(10, x=100, y=100) # move mouse cursor to 100, 200, then scroll up 10 "clicks"
211
212 On OS X and Linux platforms, PyAutoGUI can also perform horizontal scrolling by calling the hscroll() function. For example:
213
214 .. code:: python
215
216 >>> pyautogui.hscroll(10) # scroll right 10 "clicks"
217 >>> pyautogui.hscroll(-10) # scroll left 10 "clicks"
218
219 The ``scroll()`` function is a wrapper for ``vscroll()``, which performs vertical scrolling.