2023-12-29 13:23:17 +05:30
|
|
|
import pyautogui
|
2024-01-05 16:03:46 -08:00
|
|
|
import platform
|
2023-12-29 13:23:17 +05:30
|
|
|
import time
|
|
|
|
|
import math
|
2024-01-05 16:03:46 -08:00
|
|
|
|
2024-01-07 07:06:52 -08:00
|
|
|
from operate.utils.misc import convert_percent_to_decimal
|
2023-12-29 13:23:17 +05:30
|
|
|
|
2024-01-05 16:03:46 -08:00
|
|
|
|
2024-01-12 16:00:34 -08:00
|
|
|
class OperatingSystem:
|
|
|
|
|
def write(self, content):
|
2024-01-13 07:03:31 -08:00
|
|
|
try:
|
|
|
|
|
content = content.replace("\\n", "\n")
|
|
|
|
|
for char in content:
|
|
|
|
|
pyautogui.write(char)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print("[OperatingSystem][write] error:", e)
|
2024-01-12 16:00:34 -08:00
|
|
|
|
|
|
|
|
def press(self, keys):
|
2024-01-13 07:03:31 -08:00
|
|
|
try:
|
|
|
|
|
for key in keys:
|
|
|
|
|
pyautogui.keyDown(key)
|
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
for key in keys:
|
|
|
|
|
pyautogui.keyUp(key)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print("[OperatingSystem][press] error:", e)
|
2024-01-12 16:00:34 -08:00
|
|
|
|
|
|
|
|
def mouse(self, click_detail):
|
|
|
|
|
try:
|
|
|
|
|
x = convert_percent_to_decimal(click_detail.get("x"))
|
|
|
|
|
y = convert_percent_to_decimal(click_detail.get("y"))
|
|
|
|
|
|
|
|
|
|
if click_detail and isinstance(x, float) and isinstance(y, float):
|
2024-01-13 07:03:31 -08:00
|
|
|
self.click_at_percentage(x, y)
|
2024-01-12 16:00:34 -08:00
|
|
|
|
|
|
|
|
except Exception as e:
|
2024-01-13 07:03:31 -08:00
|
|
|
print("[OperatingSystem][mouse] error:", e)
|
2024-01-07 08:44:04 -08:00
|
|
|
|
2024-01-13 07:03:31 -08:00
|
|
|
def click_at_percentage(
|
2024-01-12 16:00:34 -08:00
|
|
|
self,
|
|
|
|
|
x_percentage,
|
|
|
|
|
y_percentage,
|
|
|
|
|
duration=0.2,
|
|
|
|
|
circle_radius=50,
|
|
|
|
|
circle_duration=0.5,
|
|
|
|
|
):
|
2024-01-13 07:03:31 -08:00
|
|
|
try:
|
|
|
|
|
screen_width, screen_height = pyautogui.size()
|
|
|
|
|
x_pixel = int(screen_width * float(x_percentage))
|
|
|
|
|
y_pixel = int(screen_height * float(y_percentage))
|
2024-01-12 16:00:34 -08:00
|
|
|
|
2024-01-13 07:03:31 -08:00
|
|
|
pyautogui.moveTo(x_pixel, y_pixel, duration=duration)
|
2024-01-12 16:00:34 -08:00
|
|
|
|
2024-01-13 07:03:31 -08:00
|
|
|
start_time = time.time()
|
|
|
|
|
while time.time() - start_time < circle_duration:
|
|
|
|
|
angle = ((time.time() - start_time) / circle_duration) * 2 * math.pi
|
|
|
|
|
x = x_pixel + math.cos(angle) * circle_radius
|
|
|
|
|
y = y_pixel + math.sin(angle) * circle_radius
|
|
|
|
|
pyautogui.moveTo(x, y, duration=0.1)
|
2024-01-12 16:00:34 -08:00
|
|
|
|
2024-01-13 07:03:31 -08:00
|
|
|
pyautogui.click(x_pixel, y_pixel)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print("[OperatingSystem][click_at_percentage] error:", e)
|