Skip to content

Commit

Permalink
refactor: let errors propagate as booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
winstxnhdw committed Sep 21, 2023
1 parent f09d3c3 commit ba5a161
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 68 deletions.
182 changes: 135 additions & 47 deletions keywin/mouse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ def create_event(flags: int, x: int = 0, y: int = 0, mouse_data: int = 0) -> lis
x (int?) : generic x-axis value
y (int?) : generic y-axis value
mouse_data (int?) : data to use for the mouse event
Return
--------
event (list[int]) : the mouse event
"""
return [x, y, mouse_data, flags]


def send_events(*inputs: list[int]):
def send_events(*inputs: list[int]) -> bool:
"""
Summary
-------
Expand All @@ -27,11 +31,15 @@ def send_events(*inputs: list[int]):
Parameters
----------
*inputs (list[int]) : mouse event(s)
Return
--------
success (bool) : the success of the event
"""
send_mouse_event(inputs)
return send_mouse_event(inputs)


def click(flags: int, mouse_data: int = 0):
def click(flags: int, mouse_data: int = 0) -> bool:
"""
Summary
-------
Expand All @@ -41,145 +49,209 @@ def click(flags: int, mouse_data: int = 0):
----------
flags (int) : flag to use for the mouse event
mouse_data (int?) : data to use for the mouse event
Return
--------
success (bool) : the success of the event
"""
send_events([0, 0, mouse_data, flags])
return send_events([0, 0, mouse_data, flags])


def left_click():
def left_click() -> bool:
"""
Summary
-------
click the left mouse button
return click the left mouse button
Return
--------
success (bool) : the success of the event
"""
click(MouseCodes.MOUSEEVENTF_LEFTDOWN | MouseCodes.MOUSEEVENTF_LEFTUP)
return click(MouseCodes.MOUSEEVENTF_LEFTDOWN | MouseCodes.MOUSEEVENTF_LEFTUP)


def right_click():
def right_click() -> bool:
"""
Summary
-------
click the right mouse button
return click the right mouse button
Return
--------
success (bool) : the success of the event
"""
click(MouseCodes.MOUSEEVENTF_RIGHTDOWN | MouseCodes.MOUSEEVENTF_RIGHTUP)
return click(MouseCodes.MOUSEEVENTF_RIGHTDOWN | MouseCodes.MOUSEEVENTF_RIGHTUP)


def middle_click():
def middle_click() -> bool:
"""
Summary
-------
click the middle mouse button
return click the middle mouse button
Return
--------
success (bool) : the success of the event
"""
click(MouseCodes.MOUSEEVENTF_MIDDLEDOWN | MouseCodes.MOUSEEVENTF_MIDDLEUP)
return click(MouseCodes.MOUSEEVENTF_MIDDLEDOWN | MouseCodes.MOUSEEVENTF_MIDDLEUP)


def xbutton1_click():
def xbutton1_click() -> bool:
"""
Summary
-------
click `xbutton1`
return click `xbutton1`
Return
--------
success (bool) : the success of the event
"""
click(MouseCodes.MOUSEEVENTF_XDOWN | MouseCodes.MOUSEEVENTF_XUP, MouseCodes.XBUTTON1)
return click(MouseCodes.MOUSEEVENTF_XDOWN | MouseCodes.MOUSEEVENTF_XUP, MouseCodes.XBUTTON1)


def xbutton2_click():
def xbutton2_click() -> bool:
"""
Summary
-------
click `xbutton2`
return click `xbutton2`
Return
--------
success (bool) : the success of the event
"""
click(MouseCodes.MOUSEEVENTF_XDOWN | MouseCodes.MOUSEEVENTF_XUP, MouseCodes.XBUTTON2)
return click(MouseCodes.MOUSEEVENTF_XDOWN | MouseCodes.MOUSEEVENTF_XUP, MouseCodes.XBUTTON2)


def left_press():
def left_press() -> bool:
"""
Summary
-------
press the left mouse button
Return
--------
success (bool) : the success of the event
"""
click(MouseCodes.MOUSEEVENTF_LEFTDOWN)
return click(MouseCodes.MOUSEEVENTF_LEFTDOWN)


def left_release():
def left_release() -> bool:
"""
Summary
-------
release the left mouse button
Return
--------
success (bool) : the success of the event
"""
click(MouseCodes.MOUSEEVENTF_LEFTUP)
return click(MouseCodes.MOUSEEVENTF_LEFTUP)

def right_press():
def right_press() -> bool:
"""
Summary
-------
press the right mouse button
Return
--------
success (bool) : the success of the event
"""
click(MouseCodes.MOUSEEVENTF_RIGHTDOWN)
return click(MouseCodes.MOUSEEVENTF_RIGHTDOWN)


def right_release():
def right_release() -> bool:
"""
Summary
-------
release the right mouse button
Return
--------
success (bool) : the success of the event
"""
click(MouseCodes.MOUSEEVENTF_RIGHTUP)
return click(MouseCodes.MOUSEEVENTF_RIGHTUP)


def middle_press():
def middle_press() -> bool:
"""
Summary
-------
press the middle mouse button
Return
--------
success (bool) : the success of the event
"""
click(MouseCodes.MOUSEEVENTF_MIDDLEDOWN)
return click(MouseCodes.MOUSEEVENTF_MIDDLEDOWN)


def middle_release():
def middle_release() -> bool:
"""
Summary
-------
release the middle mouse button
Return
--------
success (bool) : the success of the event
"""
click(MouseCodes.MOUSEEVENTF_MIDDLEUP)
return click(MouseCodes.MOUSEEVENTF_MIDDLEUP)


def xbutton1_press():
def xbutton1_press() -> bool:
"""
Summary
-------
press `xbutton1`
Return
--------
success (bool) : the success of the event
"""
click(MouseCodes.MOUSEEVENTF_XDOWN, MouseCodes.XBUTTON1)
return click(MouseCodes.MOUSEEVENTF_XDOWN, MouseCodes.XBUTTON1)


def xbutton1_release():
def xbutton1_release() -> bool:
"""
Summary
-------
release `xbutton1`
Return
--------
success (bool) : the success of the event
"""
click(MouseCodes.MOUSEEVENTF_XUP, MouseCodes.XBUTTON1)
return click(MouseCodes.MOUSEEVENTF_XUP, MouseCodes.XBUTTON1)


def xbutton2_press():
def xbutton2_press() -> bool:
"""
Summary
-------
press `xbutton2`
Return
--------
success (bool) : the success of the event
"""
click(MouseCodes.MOUSEEVENTF_XDOWN, MouseCodes.XBUTTON2)
return click(MouseCodes.MOUSEEVENTF_XDOWN, MouseCodes.XBUTTON2)


def xbutton2_release():
def xbutton2_release() -> bool:
"""
Summary
-------
release `xbutton2`
Return
--------
success (bool) : the success of the event
"""
click(MouseCodes.MOUSEEVENTF_XUP, MouseCodes.XBUTTON2)
return click(MouseCodes.MOUSEEVENTF_XUP, MouseCodes.XBUTTON2)


def move_relative(x: int, y: int, flag: int = 0):
def move_relative(x: int, y: int, flag: int = 0) -> bool:
"""
Summary
-------
Expand All @@ -190,11 +262,15 @@ def move_relative(x: int, y: int, flag: int = 0):
x (int) : amount of pixels to move the mouse on the x-axis
y (int) : amount of pixels to move the mouse on the y-axis
flag (int?) : flag to use for the mouse event
Return
--------
success (bool) : the success of the event
"""
send_events([x, y, 0, MouseCodes.MOUSEEVENTF_MOVE | flag])
return send_events([x, y, 0, MouseCodes.MOUSEEVENTF_MOVE | flag])


def move(x: int, y: int):
def move(x: int, y: int) -> bool:
"""
Summary
-------
Expand All @@ -204,11 +280,15 @@ def move(x: int, y: int):
----------
x (int) : coordinate on the x-axis to move the mouse to
y (int) : coordinate on the y-axis to move the mouse to
Return
--------
success (bool) : the success of the event
"""
move_relative(x, y, MouseCodes.MOUSEEVENTF_ABSOLUTE)
return move_relative(x, y, MouseCodes.MOUSEEVENTF_ABSOLUTE)


def scroll(scroll_delta: int, flags: int = MouseCodes.MOUSEEVENTF_WHEEL):
def scroll(scroll_delta: int, flags: int = MouseCodes.MOUSEEVENTF_WHEEL) -> bool:
"""
Summary
-------
Expand All @@ -218,11 +298,15 @@ def scroll(scroll_delta: int, flags: int = MouseCodes.MOUSEEVENTF_WHEEL):
----------
scroll_delta (int) : amount to scroll the mouse wheel
flag (int?) : flag to use for the mouse event
Return
--------
success (bool) : the success of the event
"""
send_events([0, 0, scroll_delta, flags])
return send_events([0, 0, scroll_delta, flags])


def scroll_horizontal(scroll_delta: int):
def scroll_horizontal(scroll_delta: int) -> bool:
"""
Summary
-------
Expand All @@ -231,5 +315,9 @@ def scroll_horizontal(scroll_delta: int):
Parameters
----------
scroll_delta (int) : amount to scroll the mouse wheel
Return
--------
success (bool) : the success of the event
"""
scroll(scroll_delta, MouseCodes.MOUSEEVENTF_HWHEEL)
return scroll(scroll_delta, MouseCodes.MOUSEEVENTF_HWHEEL)
4 changes: 2 additions & 2 deletions keywin/send_input/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Iterable

def send_mouse_event(inputs: Iterable[list[int]]) -> None:
def send_mouse_event(inputs: Iterable[list[int]]) -> bool:
"""
Summary
-------
Expand All @@ -15,7 +15,7 @@ def send_mouse_event(inputs: Iterable[list[int]]) -> None:
...


def press_keyboard(keys: Iterable[int]) -> None:
def press_keyboard(keys: Iterable[int]) -> bool:
"""
Summary
-------
Expand Down
Loading

0 comments on commit ba5a161

Please sign in to comment.