|
|
@ -51,6 +51,9 @@ class WebSocketMessageType: |
|
|
|
MOUSE_MOVE = "mouse_move" |
|
|
|
MOUSE_CLICK = "mouse_click" |
|
|
|
|
|
|
|
# 键盘操作类型 |
|
|
|
SEND_KEYS = "send_keys" |
|
|
|
|
|
|
|
|
|
|
|
class MessageSender: |
|
|
|
"""消息发送器,发送给前端""" |
|
|
@ -97,6 +100,65 @@ class GestureHandler: |
|
|
|
print("👉 正在点击鼠标") |
|
|
|
self.mouse.click(Button.left) |
|
|
|
|
|
|
|
def send_keys(self, key_str: str) -> None: |
|
|
|
""" |
|
|
|
发送按键事件(支持组合键) |
|
|
|
|
|
|
|
Args: |
|
|
|
key_str: 按键字符串(如 'ctrl+r' 或 'F11') |
|
|
|
""" |
|
|
|
try: |
|
|
|
keys = [self.__parse_key(key) for key in key_str.split("+")] |
|
|
|
print("👉 正在点击发送案按键") |
|
|
|
self.__execute_keys(keys) |
|
|
|
except Exception as e: |
|
|
|
print(f"发送按键失败: {e}") |
|
|
|
|
|
|
|
def __parse_key(self, key_str: str) -> Union[str, Key]: |
|
|
|
""" |
|
|
|
解析单个按键 |
|
|
|
|
|
|
|
Args: |
|
|
|
key_str: 按键字符串 |
|
|
|
|
|
|
|
Returns: |
|
|
|
解析后的按键对象 |
|
|
|
""" |
|
|
|
key_str = key_str.strip().lower() |
|
|
|
|
|
|
|
if hasattr(Key, key_str): |
|
|
|
return getattr(Key, key_str) |
|
|
|
elif len(key_str) == 1: |
|
|
|
return key_str |
|
|
|
elif key_str.startswith("f"): |
|
|
|
try: |
|
|
|
return getattr(Key, key_str) |
|
|
|
except AttributeError: |
|
|
|
raise ValueError(f"无效的功能键: {key_str}") |
|
|
|
else: |
|
|
|
raise ValueError(f"无效的按键: {key_str}") |
|
|
|
|
|
|
|
def __execute_keys(self, keys: List[Union[str, Key]]) -> None: |
|
|
|
""" |
|
|
|
执行按键序列 |
|
|
|
|
|
|
|
Args: |
|
|
|
keys: 按键列表 |
|
|
|
""" |
|
|
|
pressed_keys = [] |
|
|
|
try: |
|
|
|
# 按下所有键 |
|
|
|
for key in keys: |
|
|
|
self.keyboard.press(key) |
|
|
|
pressed_keys.append(key) |
|
|
|
|
|
|
|
# 释放所有键(按相反顺序) |
|
|
|
for key in reversed(pressed_keys): |
|
|
|
self.keyboard.release(key) |
|
|
|
except Exception as e: |
|
|
|
print(f"执行按键失败: {e}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.websocket("/ws_wavecontrol") |
|
|
@ -137,6 +199,9 @@ async def _handle_message( |
|
|
|
elif message.type == WebSocketMessageType.MOUSE_CLICK: |
|
|
|
gesture_handler.click_mouse() |
|
|
|
|
|
|
|
# 处理键盘操作 |
|
|
|
elif message.type == WebSocketMessageType.SEND_KEYS: |
|
|
|
gesture_handler.send_keys(data["key_str"]) |
|
|
|
except json.JSONDecodeError: |
|
|
|
print("无效的JSON数据") |
|
|
|
except Exception as e: |
|
|
|