Files
lc-mcp/backends/sendinput_win.py
T

100 lines
3.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""SendInput 后端(Windows,ctypes user32)。
当前线上 lc_mcp.py 用的机制;作为 interception 不可用时的兜底。
"""
import ctypes
import time
from ctypes import wintypes
INPUT_MOUSE = 0
INPUT_KEYBOARD = 1
KEYEVENTF_KEYUP = 0x0002
KEYEVENTF_UNICODE = 0x0004
MOUSEEVENTF_LEFTDOWN = 0x0002
MOUSEEVENTF_LEFTUP = 0x0004
MOUSEEVENTF_RIGHTDOWN = 0x0008
MOUSEEVENTF_RIGHTUP = 0x0010
class MOUSEINPUT(ctypes.Structure):
_fields_ = [("dx", wintypes.LONG), ("dy", wintypes.LONG),
("mouseData", wintypes.DWORD), ("dwFlags", wintypes.DWORD),
("time", wintypes.DWORD), ("dwExtraInfo", ctypes.POINTER(ctypes.c_ulong))]
class KEYBDINPUT(ctypes.Structure):
_fields_ = [("wVk", wintypes.WORD), ("wScan", wintypes.WORD),
("dwFlags", wintypes.DWORD), ("time", wintypes.DWORD),
("dwExtraInfo", ctypes.POINTER(ctypes.c_ulong))]
class INPUT(ctypes.Structure):
class _I(ctypes.Union):
_fields_ = [("mi", MOUSEINPUT), ("ki", KEYBDINPUT)]
_anonymous_ = ("i",)
_fields_ = [("type", wintypes.DWORD), ("i", _I)]
KEYMAP = {'enter': 0x0D, 'return': 0x0D, 'esc': 0x1B, 'space': 0x20,
'tab': 0x09, 'ctrl': 0x11, 'alt': 0x12, 'shift': 0x10,
'p': 0x50, 'f4': 0x73, 'f5': 0x74, 'f8': 0x77, 'f12': 0x7B,
'1': 0x31, '2': 0x32, '3': 0x33, '4': 0x34, '5': 0x35,
'6': 0x36, '7': 0x37, '8': 0x38, '9': 0x39, '0': 0x30}
class SendInputBackend:
def __init__(self):
self._user32 = ctypes.WinDLL('user32', use_last_error=True)
def _send_input(self, *inputs):
n = len(inputs)
arr = (INPUT * n)(*inputs)
return self._user32.SendInput(n, arr, ctypes.sizeof(INPUT))
def click(self, x, y, button='left', clicks=1):
self._user32.SetCursorPos(int(x), int(y))
time.sleep(0.03)
down_flag = MOUSEEVENTF_LEFTDOWN if button == 'left' else MOUSEEVENTF_RIGHTDOWN
up_flag = MOUSEEVENTF_LEFTUP if button == 'left' else MOUSEEVENTF_RIGHTUP
for _ in range(max(1, int(clicks))):
down = INPUT(); down.type = INPUT_MOUSE; down.mi.dwFlags = down_flag
up = INPUT(); up.type = INPUT_MOUSE; up.mi.dwFlags = up_flag
self._send_input(down, up)
time.sleep(0.05)
return {'ok': True, 'clicked': [int(x), int(y)], 'backend': 'sendinput'}
def move(self, x, y):
self._user32.SetCursorPos(int(x), int(y))
time.sleep(0.01)
return {'ok': True, 'backend': 'sendinput'}
def keypress(self, key):
name = str(key).lower()
vk = KEYMAP.get(name)
if vk is None and len(name) == 1:
vk = ord(name.upper()) if name.isalpha() else ord(name)
if vk is None:
return {'ok': False, 'error': 'unknown key %s' % key}
self._press(vk)
return {'ok': True, 'key': str(key), 'backend': 'sendinput'}
def _press(self, vk):
ki = INPUT(); ki.type = INPUT_KEYBOARD; ki.ki.wVk = vk
self._send_input(ki)
time.sleep(0.04)
ki2 = INPUT(); ki2.type = INPUT_KEYBOARD; ki2.ki.wVk = vk
ki2.ki.dwFlags = KEYEVENTF_KEYUP
self._send_input(ki2)
def type_text(self, text):
for ch in str(text):
ki = INPUT(); ki.type = INPUT_KEYBOARD
ki.ki.wScan = ord(ch); ki.ki.dwFlags = KEYEVENTF_UNICODE
ki2 = INPUT(); ki2.type = INPUT_KEYBOARD
ki2.ki.wScan = ord(ch)
ki2.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP
self._send_input(ki, ki2)
time.sleep(0.01)
return {'ok': True, 'typed': str(text), 'backend': 'sendinput'}