From 9db497bcae026824e4a240c5cdc281ff27f5163b Mon Sep 17 00:00:00 2001 From: Vinyzu <50874994+Vinyzu@users.noreply.github.com> Date: Sun, 18 Aug 2024 21:47:00 +0200 Subject: [PATCH] Custom Windows Double Click Implementation --- cdp_patches/input/async_input.py | 27 +++++++-------------- cdp_patches/input/os_base/windows.py | 35 ++++++++++++++++++++-------- cdp_patches/input/sync_input.py | 27 +++++++-------------- 3 files changed, 42 insertions(+), 47 deletions(-) diff --git a/cdp_patches/input/async_input.py b/cdp_patches/input/async_input.py index 86cd2a2..655d32c 100644 --- a/cdp_patches/input/async_input.py +++ b/cdp_patches/input/async_input.py @@ -8,6 +8,8 @@ import time from typing import Any, Generator, Literal, Optional, Union +from pywinauto.mouse import press + if sys.version_info.minor >= 10: from typing import TypeAlias else: @@ -115,13 +117,7 @@ async def _sleep_timeout(self, timeout: Optional[float] = None) -> None: await asyncio.sleep(timeout) async def click( - self, - button: Literal["left", "right", "middle"], - x: Union[int, float], - y: Union[int, float], - pressed: str = "", - emulate_behaviour: Optional[bool] = True, - timeout: Optional[float] = None + self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], pressed: str = "", emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None ) -> None: x, y = int(x), int(y) @@ -136,22 +132,17 @@ async def double_click( ) -> None: x, y = int(x), int(y) - await self.click(button=button, x=x, y=y, timeout=timeout, emulate_behaviour=emulate_behaviour, pressed=pressed) + press_timeout = click_timeout = timeout or self.sleep_timeout if self.emulate_behaviour and emulate_behaviour: - # await self._sleep_timeout(random.uniform(0.14, 0.21)) - await self._sleep_timeout(timeout=timeout) - await self.click(button=button, x=x, y=y, emulate_behaviour=False, timeout=timeout, pressed=pressed) + click_timeout = random.uniform(0.14, 0.21) + self._base.move(x=x, y=y) + kwargs = _mk_kwargs(pressed) + self._base.double_click(button=button, x=x, y=y, press_timeout=press_timeout, click_timeout=click_timeout, **kwargs) self.last_x, self.last_y = x, y async def down( - self, - button: Literal["left", "right", "middle"], - x: Union[int, float], - y: Union[int, float], - pressed: str = "", - emulate_behaviour: Optional[bool] = True, - timeout: Optional[float] = None + self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], pressed: str = "", emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None ) -> None: x, y = int(x), int(y) diff --git a/cdp_patches/input/os_base/windows.py b/cdp_patches/input/os_base/windows.py index b167f02..bc03449 100644 --- a/cdp_patches/input/os_base/windows.py +++ b/cdp_patches/input/os_base/windows.py @@ -1,6 +1,7 @@ import asyncio import ctypes import re +import time import warnings from typing import List, Literal, Union @@ -133,23 +134,37 @@ def down(self, button: Literal["left", "right", "middle"], x: int, y: int, press self.ensure_window() self.browser_window.press_mouse(button=button, pressed=pressed, coords=(int(x * self.scale_factor), int(y * self.scale_factor))) - def double_down(self, button: Literal["left", "right", "middle"], x: int, y: int, pressed: str = ""): + def double_click(self, button: Literal["left", "right", "middle"], x: int, y: int, pressed: str = "", press_timeout: float = 0.01, click_timeout: float = 0.05) -> None: if not pressed: pressed = button + self.ensure_window() - if button == "left": - msg = win32defines.WM_LBUTTONDBLCLK - elif button == "right": - msg = win32defines.WM_RBUTTONDBLCLK + if button.lower() == "left": + first_click = (win32defines.WM_LBUTTONDOWN, win32defines.WM_LBUTTONUP) + second_click = (win32defines.WM_LBUTTONDBLCLK, win32defines.WM_LBUTTONUP) + elif button.lower() == "right": + first_click = (win32defines.WM_RBUTTONDOWN, win32defines.WM_RBUTTONUP) + second_click = (win32defines.WM_RBUTTONDBLCLK, win32defines.WM_RBUTTONUP) else: - msg = win32defines.WM_MBUTTONDBLCLK + first_click = (win32defines.WM_MBUTTONDOWN, win32defines.WM_MBUTTONUP) + second_click = (win32defines.WM_MBUTTONDBLCLK, win32defines.WM_MBUTTONUP) + # figure out the flags and pack coordinates flags, click_point = _calc_flags_and_coords(pressed, [x, y]) - win32functions.PostMessage(self.browser_window, msg, win32structures.WPARAM(flags), win32structures.LPARAM(click_point)) - - # wait until the thread can accept another message - win32functions.WaitGuiThreadIdle(self.browser_window.handle) + for msg in first_click: + win32functions.PostMessage(self.browser_window, msg, win32structures.WPARAM(flags), win32structures.LPARAM(click_point)) + time.sleep(press_timeout) + # wait until the thread can accept another message + win32functions.WaitGuiThreadIdle(self.browser_window.handle) + + time.sleep(click_timeout) + + for msg in second_click: + win32functions.PostMessage(self.browser_window, msg, win32structures.WPARAM(flags), win32structures.LPARAM(click_point)) + time.sleep(press_timeout) + # wait until the thread can accept another message + win32functions.WaitGuiThreadIdle(self.browser_window.handle) def up(self, button: Literal["left", "right", "middle"], x: int, y: int, pressed: str = "") -> None: self.ensure_window() diff --git a/cdp_patches/input/sync_input.py b/cdp_patches/input/sync_input.py index 6b28172..e9625c5 100644 --- a/cdp_patches/input/sync_input.py +++ b/cdp_patches/input/sync_input.py @@ -113,13 +113,7 @@ def _sleep_timeout(self, timeout: Optional[float] = None) -> None: # pass def click( - self, - button: Literal["left", "right", "middle"], - x: Union[int, float], - y: Union[int, float], - pressed: str = "", - emulate_behaviour: Optional[bool] = True, - timeout: Optional[float] = 0.07 + self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], pressed: str = "", emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = 0.07 ) -> None: x, y = int(x), int(y) @@ -134,22 +128,17 @@ def double_click( ) -> None: x, y = int(x), int(y) - self.click(button=button, x=x, y=y, timeout=timeout, emulate_behaviour=emulate_behaviour, pressed=pressed) - if emulate_behaviour and self.emulate_behaviour: - # self._sleep_timeout(random.uniform(0.14, 0.21)) - self._sleep_timeout(timeout=timeout) - self.click(button=button, x=x, y=y, emulate_behaviour=False, timeout=timeout, pressed=pressed) + press_timeout = click_timeout = timeout or self.sleep_timeout + if self.emulate_behaviour and emulate_behaviour: + click_timeout = random.uniform(0.14, 0.21) + self._base.move(x=x, y=y) + kwargs = _mk_kwargs(pressed) + self._base.double_click(button=button, x=x, y=y, press_timeout=press_timeout, click_timeout=click_timeout, **kwargs) self.last_x, self.last_y = x, y def down( - self, - button: Literal["left", "right", "middle"], - x: Union[int, float], - y: Union[int, float], - pressed: str = "", - emulate_behaviour: Optional[bool] = True, - timeout: Optional[float] = None + self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], pressed: str = "", emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None ) -> None: x, y = int(x), int(y)