Skip to content

Commit

Permalink
feat: add scroll support
Browse files Browse the repository at this point in the history
  • Loading branch information
thewh1teagle committed Dec 4, 2023
1 parent 37fec13 commit a8d2b77
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
8 changes: 8 additions & 0 deletions examples/scroll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from zero_hid import Mouse
from time import sleep

with Mouse() as m:
m.scroll_y(50)
sleep(1)
m.scroll_x(50)

30 changes: 27 additions & 3 deletions zero_hid/Mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ class RelativeMoveRangeError(Exception):

class Mouse:
def __init__(self, dev = None, absolute = False) -> None:
self.__setup_device(dev, absolute)
self.__setup_move(absolute)
self.__send_mouse_event = absolute_mouse_event if absolute else relative_mouse_event # dynamic mouse event method


def __setup_device(self, dev, absolute: bool):
if dev is None:
dev = defaults.ABSOLUTE_MOUSE_PATH if absolute else defaults.RELATIVE_MOUSE_PATH
self.move = self.__move_absolute if absolute else self.__move_relative # dynamic move method
self.__send_mouse_event = absolute_mouse_event if absolute else relative_mouse_event # dynamic mouse event method
if not hasattr(dev, 'write'): # check if file like object
self.dev = open(dev, 'ab+')
else:
self.dev = dev


def __setup_move(self, absolute: bool):
self.move = self.__move_absolute if absolute else self.__move_relative # dynamic move method

def left_click(self):
self.__send_mouse_event(self.dev, 0x1, 0, 0, 0, 0)
self.__send_mouse_event(self.dev, 0x0, 0, 0, 0, 0)
Expand All @@ -25,6 +31,24 @@ def right_click(self):
self.__send_mouse_event(self.dev, 0x2, 0, 0, 0, 0)
self.__send_mouse_event(self.dev, 0x0, 0, 0, 0, 0)

def scroll_y(self, position: int):
"""
scroll in y axis (vertical)
y should be in range of -127 to 127
"""
if not -127 <= position <= 127:
raise RelativeMoveRangeError(f"Value of y {position} out of range (-127 - 127)")
self.__send_mouse_event(self.dev, 0, 0, 0, position, 0)

def scroll_x(self, position: int):
"""
scroll in x axis (horizontal)
x should be in range of -127 to 127
"""
if not -127 <= position <= 127:
raise RelativeMoveRangeError(f"Value of x: {position} out of range (-127 - 127)")
self.__send_mouse_event(self.dev, 0, 0, 0, 0, position)

def __move_relative(self, x, y):
"""
move the mouse in relative mode
Expand Down

0 comments on commit a8d2b77

Please sign in to comment.