File size: 2,347 Bytes
3508f42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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