diff --git a/keywin/mouse/__init__.py b/keywin/mouse/__init__.py index 65c9b2f..41407f1 100644 --- a/keywin/mouse/__init__.py +++ b/keywin/mouse/__init__.py @@ -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 ------- @@ -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 ------- @@ -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 ------- @@ -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 ------- @@ -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 ------- @@ -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 ------- @@ -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) diff --git a/keywin/send_input/__init__.pyi b/keywin/send_input/__init__.pyi index b1f77f3..313b1da 100644 --- a/keywin/send_input/__init__.pyi +++ b/keywin/send_input/__init__.pyi @@ -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 ------- @@ -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 ------- diff --git a/keywin/send_input/send_input.c b/keywin/send_input/send_input.c index cee5a2d..af4d816 100644 --- a/keywin/send_input/send_input.c +++ b/keywin/send_input/send_input.c @@ -5,58 +5,67 @@ static PyObject* press_keyboard(PyObject *self, PyObject *args) { PyObject* key_tuple; if (!PyArg_ParseTuple(args, "O", &key_tuple)) { - return NULL; + Py_RETURN_FALSE; } - const UINT key_list_length = (UINT)PyObject_Length(key_tuple); - const UINT inputs_length = key_list_length * 2; + const Py_ssize_t keys_length = PyObject_Length(key_tuple); + + if (keys_length == -1) { + Py_RETURN_FALSE; + } + + const UINT number_of_keys = (UINT)keys_length; + const UINT inputs_length = number_of_keys * 2; const int input_size = sizeof(INPUT); INPUT* inputs = malloc(input_size * inputs_length); if (!inputs) { - PyErr_SetString(PyExc_MemoryError, "Unable to allocate memory for inputs."); - return NULL; + Py_RETURN_FALSE; } - for (UINT i = 0; i < key_list_length; i++) { + for (UINT i = 0; i < number_of_keys; i++) { PyObject* key = PyTuple_GetItem(key_tuple, i); inputs[i].type = INPUT_KEYBOARD; inputs[i].ki.wVk = (WORD)PyLong_AsLong(key); inputs[i].ki.dwFlags = 0; - const UINT release_index = i + key_list_length; + const UINT release_index = i + number_of_keys; inputs[release_index].type = INPUT_KEYBOARD; inputs[release_index].ki.wVk = inputs[i].ki.wVk; inputs[release_index].ki.dwFlags = KEYEVENTF_KEYUP; } if (SendInput(inputs_length, inputs, input_size) != inputs_length) { - PyErr_SetString(PyExc_RuntimeError, "press_keyboard failed."); free(inputs); - return NULL; + Py_RETURN_FALSE; } free(inputs); - Py_RETURN_NONE; + Py_RETURN_TRUE; } static PyObject* send_mouse_event(PyObject *self, PyObject *args) { PyObject* mouse_event_list; if (!PyArg_ParseTuple(args, "O", &mouse_event_list)) { - return NULL; + Py_RETURN_FALSE; } const Py_ssize_t mouse_events_length = PyObject_Length(mouse_event_list); + + if (mouse_events_length == -1) { + Py_RETURN_FALSE; + } + + const UINT number_of_events = (UINT)mouse_events_length; const int input_size = sizeof(INPUT); - INPUT* inputs = malloc(input_size * mouse_events_length); + INPUT* inputs = malloc(input_size * number_of_events); if (!inputs) { - PyErr_SetString(PyExc_MemoryError, "Unable to allocate memory for inputs."); - return NULL; + Py_RETURN_FALSE; } - for (Py_ssize_t i = 0; i < mouse_events_length; i++) { + for (UINT i = 0; i < number_of_events; i++) { PyObject* mouse_event = PyTuple_GetItem(mouse_event_list, i); inputs[i].type = INPUT_MOUSE; @@ -67,14 +76,13 @@ static PyObject* send_mouse_event(PyObject *self, PyObject *args) { inputs[i].mi.time = 0; } - if (SendInput((UINT)mouse_events_length, inputs, input_size) != mouse_events_length) { - PyErr_SetString(PyExc_RuntimeError, "send_mouse_event failed."); + if (SendInput((UINT)number_of_events, inputs, input_size) != number_of_events) { free(inputs); - return NULL; + Py_RETURN_FALSE; } free(inputs); - Py_RETURN_NONE; + Py_RETURN_TRUE; } static PyMethodDef send_input_methods[] = {