Skip to content

Commit 83b3b63

Browse files
authored
Merge pull request #2 from dhalbert/master
Mouse: fix examples. Add keyword args to Mouse.move()
2 parents 189a1c7 + 0aedf70 commit 83b3b63

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ __pycache__
22
_build
33
*.pyc
44
.vscode
5+
*~
6+

README.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,19 @@ The ``Mouse`` class simulates a three-button mouse with a scroll wheel.
9393
m.click(Mouse.LEFT_BUTTON)
9494
9595
# Move the mouse diagonally to the upper left.
96-
m.move(-100, 100, 0)
96+
m.move(-100, -100, 0)
9797
98-
# Move the mouse while holding down the left button.
98+
# Roll the mouse wheel away from the user one unit.
99+
# Amount scrolled depends on the host.
100+
m.move(0, 0, -1)
101+
102+
# Keyword arguments may also be used. Omitted arguments default to 0.
103+
m.move(x=-100, y=-100)
104+
m.move(wheel=-1)
105+
106+
# Move the mouse while holding down the left button. (click-drag).
99107
m.press(Mouse.LEFT_BUTTON)
100-
m.move(50, 20, 0)
108+
m.move(x=50, y=20)
101109
m.release_all() # or m.release(Mouse.LEFT_BUTTON)
102110
103111

adafruit_hid/mouse.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def release(self, buttons):
7777
"""Release the given mouse buttons.
7878
7979
:param buttons: a bitwise-or'd combination of ``LEFT_BUTTON``, ``MIDDLE_BUTTON``, and ``RIGHT_BUTTON``.
80-
"""
80+
"""
8181
self.report[0] &= ~buttons
8282
self.move(0, 0, 0)
8383

@@ -104,31 +104,35 @@ def click(self, buttons):
104104
self.press(buttons)
105105
self.release(buttons)
106106

107-
def move(self, x_distance, y_distance, wheel_turn):
107+
def move(self, x=0, y=0, wheel=0):
108108
"""Move the mouse and turn the wheel as directed.
109109
110-
:param x_distance: Move the mouse along the x axis. Negative is to the left, positive is to the right.
111-
:param y_distance: Move the mouse along the y axis. Negative is toward the user, positive is away from the user.
112-
:param wheel turn: Rotate the wheel this amount. Negative is toward the user, positive is away from the user.
110+
:param x: Move the mouse along the x axis. Negative is to the left, positive is to the right.
111+
:param y: Move the mouse along the y axis. Negative is upwards on the display, positive is downwards.
112+
:param wheel: Rotate the wheel this amount. Negative is toward the user, positive is away from the user. The scrolling effect depends on the host.
113113
:raises ValueError: if any argument is not in the range -127 to 127 inclusive.
114114
115115
Examples::
116116
117-
# Move 100 to the left.
117+
# Move 100 to the left. Do not move up and down. Do not roll the scroll wheel.
118118
m.move(-100, 0, 0)
119+
# Same, with keyword arguments.
120+
m.move(x=-100)
119121
120122
# Move diagonally to the upper right.
121-
m.move(50, 20, 0)
123+
m.move(50, 20)
124+
# Same.
125+
m.move(x=50, y=-20)
122126
123127
# Roll the mouse wheel away from the user.
124-
m.move(0, 0, 5)
128+
m.move(wheel=1)
125129
"""
126-
if (self._distance_ok(x_distance)
127-
and self._distance_ok(y_distance)
128-
and self._distance_ok(wheel_turn)):
129-
self.report[1] = x_distance
130-
self.report[2] = y_distance
131-
self.report[3] = wheel_turn
130+
if (self._distance_ok(x)
131+
and self._distance_ok(y)
132+
and self._distance_ok(wheel)):
133+
self.report[1] = x
134+
self.report[2] = y
135+
self.report[3] = wheel
132136
self.hid_mouse.send_report(self.report)
133137
else:
134138
raise ValueError('All arguments must be >= -127 and <= 127')

0 commit comments

Comments
 (0)