diff --git a/botcity/core/bot.py b/botcity/core/bot.py index dbcdfda..10ce7f6 100644 --- a/botcity/core/bot.py +++ b/botcity/core/bot.py @@ -761,7 +761,7 @@ def get_element_coords_centered( height: Optional[int] = None, matching: float = 0.9, best: bool = True, - ): + ) -> Union[Tuple[int, int], Tuple[None, None]]: """ Find an element defined by label on screen and returns its centered coordinates. @@ -807,7 +807,7 @@ def browse(self, url, location=0): # Mouse ####### - def click_on(self, label): + def click_on(self, label: str) -> None: """ Click on the element. @@ -819,7 +819,7 @@ def click_on(self, label): raise ValueError(f"Element not available. Cannot find {label}.") _mouse_click(self._mouse_controller, x, y) - def get_last_x(self): + def get_last_x(self) -> int: """ Get the last X position for the mouse. @@ -828,7 +828,7 @@ def get_last_x(self): """ return self._mouse_controller.position[0] - def get_last_y(self): + def get_last_y(self) -> int: """ Get the last Y position for the mouse. @@ -837,7 +837,7 @@ def get_last_y(self): """ return self._mouse_controller.position[1] - def mouse_move(self, x, y): + def mouse_move(self, x: int, y: int) -> None: """ Move the mouse to the coordinate defined by x and y @@ -849,7 +849,7 @@ def mouse_move(self, x, y): self._mouse_controller.position = (x, y) self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION) - def click_at(self, x, y): + def click_at(self, x: int, y: int) -> None: """ Click at the coordinate defined by x and y @@ -862,12 +862,12 @@ def click_at(self, x, y): @only_if_element def click( self, - wait_after=config.DEFAULT_SLEEP_AFTER_ACTION, + wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION, *, - clicks=1, - interval_between_clicks=0, - button="left", - ): + clicks: int = 1, + interval_between_clicks: int = 0, + button: str = "left", + ) -> None: """ Click on the last found element. @@ -878,6 +878,7 @@ def click( button (str, optional): One of 'left', 'right', 'middle'. Defaults to 'left' """ x, y = self.state.center() + _mouse_click( self._mouse_controller, x, y, clicks, interval_between_clicks, button ) @@ -886,14 +887,14 @@ def click( @only_if_element def click_relative( self, - x, - y, - wait_after=config.DEFAULT_SLEEP_AFTER_ACTION, + x: int, + y: int, + wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION, *, - clicks=1, - interval_between_clicks=0, - button="left", - ): + clicks: int = 1, + interval_between_clicks: int = 0, + button: str = "left", + ) -> None: """ Click Relative on the last found element. @@ -907,13 +908,14 @@ def click_relative( """ x = self.state.x() + x y = self.state.y() + y + _mouse_click( self._mouse_controller, x, y, clicks, interval_between_clicks, button ) self.sleep(wait_after) @only_if_element - def double_click(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION): + def double_click(self, wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION) -> None: """ Double Click on the last found element. @@ -925,11 +927,11 @@ def double_click(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION): @only_if_element def double_click_relative( self, - x, - y, - interval_between_clicks=0, - wait_after=config.DEFAULT_SLEEP_AFTER_ACTION, - ): + x: int, + y: int, + interval_between_clicks: int = 0, + wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION, + ) -> None: """ Double Click Relative on the last found element. @@ -949,7 +951,7 @@ def double_click_relative( ) @only_if_element - def triple_click(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION): + def triple_click(self, wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION) -> None: """ Triple Click on the last found element. @@ -961,11 +963,11 @@ def triple_click(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION): @only_if_element def triple_click_relative( self, - x, - y, - interval_between_clicks=0, - wait_after=config.DEFAULT_SLEEP_AFTER_ACTION, - ): + x: int, + y: int, + interval_between_clicks: int = 0, + wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION, + ) -> None: """ Triple Click Relative on the last found element. @@ -985,8 +987,11 @@ def triple_click_relative( ) def mouse_down( - self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION, *, button="left" - ): + self, + wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION, + *, + button: str = "left", + ) -> None: """ Holds down the requested mouse button. @@ -998,7 +1003,12 @@ def mouse_down( self._mouse_controller.press(mouse_button) self.sleep(wait_after) - def mouse_up(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION, *, button="left"): + def mouse_up( + self, + wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION, + *, + button: str = "left", + ) -> None: """ Releases the requested mouse button. @@ -1010,7 +1020,7 @@ def mouse_up(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION, *, button="left self._mouse_controller.release(mouse_button) self.sleep(wait_after) - def scroll_down(self, clicks): + def scroll_down(self, clicks: int) -> None: """ Scroll Down n clicks @@ -1020,7 +1030,7 @@ def scroll_down(self, clicks): self._mouse_controller.scroll(0, -1 * clicks) self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION) - def scroll_up(self, clicks): + def scroll_up(self, clicks: int) -> None: """ Scroll Up n clicks @@ -1031,7 +1041,7 @@ def scroll_up(self, clicks): self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION) @only_if_element - def move(self): + def move(self) -> None: """ Move to the center position of last found item. """ @@ -1039,7 +1049,7 @@ def move(self): self._mouse_controller.position = (x, y) self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION) - def move_relative(self, x, y): + def move_relative(self, x: int, y: int) -> None: """ Move the mouse relative to its current position. @@ -1053,7 +1063,7 @@ def move_relative(self, x, y): self._mouse_controller.position = (x, y) self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION) - def move_random(self, range_x, range_y): + def move_random(self, range_x: int, range_y: int) -> None: """ Move randomly along the given x, y range. @@ -1070,11 +1080,11 @@ def move_random(self, range_x, range_y): @only_if_element def right_click( self, - wait_after=config.DEFAULT_SLEEP_AFTER_ACTION, + wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION, *, - clicks=1, - interval_between_clicks=0, - ): + clicks: int = 1, + interval_between_clicks: int = 0, + ) -> None: """ Right click on the last found element. @@ -1094,7 +1104,7 @@ def right_click( ) self.sleep(wait_after) - def right_click_at(self, x, y): + def right_click_at(self, x: int, y: int) -> None: """ Right click at the coordinate defined by x and y @@ -1107,11 +1117,11 @@ def right_click_at(self, x, y): @only_if_element def right_click_relative( self, - x, - y, - interval_between_clicks=0, - wait_after=config.DEFAULT_SLEEP_AFTER_ACTION, - ): + x: int, + y: int, + interval_between_clicks: int = 0, + wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION, + ) -> None: """ Right Click Relative on the last found element. diff --git a/botcity/core/input_utils.py b/botcity/core/input_utils.py index c4821ae..f95c380 100644 --- a/botcity/core/input_utils.py +++ b/botcity/core/input_utils.py @@ -5,22 +5,22 @@ from pynput.mouse import Button, Controller keys_map = { - "num0": '0', - "num1": '1', - "num2": '2', - "num3": '3', - "num4": '4', - "num5": '5', - "num6": '6', - "num7": '7', - "num8": '8', - "num9": '9', - "add": '+', - "decimal": ',', - "subtract": '-', - "multiply": '*', - "divide": '/', - "separator": '|', + "num0": "0", + "num1": "1", + "num2": "2", + "num3": "3", + "num4": "4", + "num5": "5", + "num6": "6", + "num7": "7", + "num8": "8", + "num9": "9", + "add": "+", + "decimal": ",", + "subtract": "-", + "multiply": "*", + "divide": "/", + "separator": "|", "altleft": Key.alt_l, "altright": Key.alt_r, "capslock": Key.caps_lock, @@ -43,14 +43,14 @@ "volumeup": Key.media_volume_up, "prevtrack": Key.media_previous, "nexttrack": Key.media_next, - "return": Key.enter + "return": Key.enter, } if platform.system() != "Darwin": keys_map.update( { "numlock": Key.num_lock, - "prtsc": Key.print_screen, + "prtsc": Key.print_screen, "prtscr": Key.print_screen, "printscreen": Key.print_screen, "prntscrn": Key.print_screen, @@ -59,25 +59,33 @@ } ) -mouse_map = { - "left": Button.left, - "right": Button.right, - "middle": Button.middle -} +mouse_map = {"left": Button.left, "right": Button.right, "middle": Button.middle} -def _mouse_click(mouse_controller: Controller, x: int, y: int, clicks=1, interval_between_clicks=0, button='left'): +def _mouse_click( + mouse_controller: Controller, + x: int, + y: int, + clicks: int = 1, + interval_between_clicks: int = 0, + button: str = "left", +) -> None: """ Moves the mouse and clicks at the coordinate defined by x and y. """ if platform.system() == "Darwin": from . import os_compat - os_compat.osx_click(x=x, y=y, clicks=clicks, interval=interval_between_clicks, button=button) + + os_compat.osx_click( + x=x, y=y, clicks=clicks, interval=interval_between_clicks, button=button + ) else: mouse_button = mouse_map.get(button, None) if not mouse_button: - raise ValueError(f'''Invalid mouse button name. - The mouse button has to be one of these values: {list(mouse_map.keys())}''') + raise ValueError( + f"""Invalid mouse button name. + The mouse button has to be one of these values: {list(mouse_map.keys())}""" + ) mouse_controller.position = (x, y) time.sleep(0.1)