import pyautogui import numpy as np import cv2 from PIL import Image import time from pynput import mouse, keyboard class ScreenController: def __init__(self): # 初始化 PyAutoGUI 安全设置 pyautogui.FAILSAFE = True pyautogui.PAUSE = 0.5 def capture_screen(self): """捕获当前屏幕""" screenshot = pyautogui.screenshot() return np.array(screenshot) def find_element_on_screen(self, template_image, confidence=0.8): """在屏幕上查找特定元素""" try: location = pyautogui.locateOnScreen(template_image, confidence=confidence) if location: return location return None except Exception as e: print(f"Error finding element: {e}") return None def click_position(self, x, y): """点击指定位置""" try: pyautogui.click(x, y) return True except Exception as e: print(f"Error clicking position: {e}") return False def type_text(self, text): """输入文本""" try: pyautogui.write(text) return True except Exception as e: print(f"Error typing text: {e}") return False def press_key(self, key): """按下特定按键""" try: pyautogui.press(key) return True except Exception as e: print(f"Error pressing key: {e}") return False def move_to(self, x, y, duration=0.5): """移动鼠标到指定位置""" try: pyautogui.moveTo(x, y, duration=duration) return True except Exception as e: print(f"Error moving mouse: {e}") return False def drag_to(self, x, y, duration=0.5): """拖拽到指定位置""" try: pyautogui.dragTo(x, y, duration=duration) return True except Exception as e: print(f"Error dragging: {e}") return False def scroll(self, clicks): """滚动屏幕""" try: pyautogui.scroll(clicks) return True except Exception as e: print(f"Error scrolling: {e}") return False