Skip to content

Commit

Permalink
Custom Windows Double Click Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinyzu committed Aug 18, 2024
1 parent 1c9798c commit 9db497b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 47 deletions.
27 changes: 9 additions & 18 deletions cdp_patches/input/async_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down
35 changes: 25 additions & 10 deletions cdp_patches/input/os_base/windows.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import ctypes
import re
import time
import warnings
from typing import List, Literal, Union

Expand Down Expand Up @@ -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()
Expand Down
27 changes: 8 additions & 19 deletions cdp_patches/input/sync_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 9db497b

Please sign in to comment.