| 7 | |
| 8 | |
| 9 | class OperatingSystem: |
| 10 | def write(self, content): |
| 11 | try: |
| 12 | content = content.replace("\\n", "\n") |
| 13 | for char in content: |
| 14 | pyautogui.write(char) |
| 15 | except Exception as e: |
| 16 | print("[OperatingSystem][write] error:", e) |
| 17 | |
| 18 | def press(self, keys): |
| 19 | try: |
| 20 | for key in keys: |
| 21 | pyautogui.keyDown(key) |
| 22 | time.sleep(0.1) |
| 23 | for key in keys: |
| 24 | pyautogui.keyUp(key) |
| 25 | except Exception as e: |
| 26 | print("[OperatingSystem][press] error:", e) |
| 27 | |
| 28 | def mouse(self, click_detail): |
| 29 | try: |
| 30 | x = convert_percent_to_decimal(click_detail.get("x")) |
| 31 | y = convert_percent_to_decimal(click_detail.get("y")) |
| 32 | |
| 33 | if click_detail and isinstance(x, float) and isinstance(y, float): |
| 34 | self.click_at_percentage(x, y) |
| 35 | |
| 36 | except Exception as e: |
| 37 | print("[OperatingSystem][mouse] error:", e) |
| 38 | |
| 39 | def click_at_percentage( |
| 40 | self, |
| 41 | x_percentage, |
| 42 | y_percentage, |
| 43 | duration=0.2, |
| 44 | circle_radius=50, |
| 45 | circle_duration=0.5, |
| 46 | ): |
| 47 | try: |
| 48 | screen_width, screen_height = pyautogui.size() |
| 49 | x_pixel = int(screen_width * float(x_percentage)) |
| 50 | y_pixel = int(screen_height * float(y_percentage)) |
| 51 | |
| 52 | pyautogui.moveTo(x_pixel, y_pixel, duration=duration) |
| 53 | |
| 54 | start_time = time.time() |
| 55 | while time.time() - start_time < circle_duration: |
| 56 | angle = ((time.time() - start_time) / circle_duration) * 2 * math.pi |
| 57 | x = x_pixel + math.cos(angle) * circle_radius |
| 58 | y = y_pixel + math.sin(angle) * circle_radius |
| 59 | pyautogui.moveTo(x, y, duration=0.1) |
| 60 | |
| 61 | pyautogui.click(x_pixel, y_pixel) |
| 62 | except Exception as e: |
| 63 | print("[OperatingSystem][click_at_percentage] error:", e) |