Win32 API模拟鼠标点击在Unreal Engine应用中无响应问题排查
Unreal Engine 模拟鼠标点击无响应问题解决
场景:通过UDP接收HID鼠标数据,用Python的
win32api.mouse_event模拟鼠标移动和点击,其他Windows应用正常,但Unreal Engine应用中光标可移动,点击无响应;UE能正常响应硬件鼠标。
问题原因
Unreal Engine默认启用**Raw Input(原始输入)**模式,该模式优先处理来自HID硬件的原生输入事件,而win32api.mouse_event生成的是合成虚拟输入事件,会被UE的输入系统过滤,导致点击无响应。光标移动生效是因为SetCursorPos直接修改系统光标位置,不受UE输入过滤影响。
解决方案
方案1:关闭Raw Input(简便配置,无需改代码)
- 打开UE项目的
Project Settings - 进入
Input选项卡,找到Mouse Settings区域 - 取消勾选
Use Raw Input选项 - 重启Unreal Editor后,模拟的鼠标点击即可正常响应
方案2:使用SendInput替代mouse_event(保留Raw Input的同时生效)
如果需要保留Raw Input的高精度输入特性,可以用Windows原生的SendInputAPI模拟鼠标事件,它生成的事件更接近原生HID输入,能被UE的Raw Input识别。替换原有Python代码中的鼠标点击逻辑:
import win32api import ctypes from ctypes import wintypes # 定义SendInput所需的结构体 INPUT_MOUSE = 0 MOUSEEVENTF_LEFTDOWN = 0x0002 MOUSEEVENTF_LEFTUP = 0x0004 class MOUSEINPUT(ctypes.Structure): _fields_ = [("dx", wintypes.LONG), ("dy", wintypes.LONG), ("mouseData", wintypes.DWORD), ("dwFlags", wintypes.DWORD), ("time", wintypes.DWORD), ("dwExtraInfo", wintypes.ULONG_PTR)] class INPUT(ctypes.Structure): _fields_ = [("type", wintypes.DWORD), ("mi", MOUSEINPUT)] def simulate_mouse_click(x, y, is_pressed): # 先设置光标位置 win32api.SetCursorPos((x, y)) # 构造鼠标输入事件 input_event = INPUT() input_event.type = INPUT_MOUSE input_event.mi.dwFlags = MOUSEEVENTF_LEFTDOWN if is_pressed else MOUSEEVENTF_LEFTUP input_event.mi.dx = 0 input_event.mi.dy = 0 input_event.mi.mouseData = 0 input_event.mi.time = 0 input_event.mi.dwExtraInfo = 0 # 发送事件 ctypes.windll.user32.SendInput(1, ctypes.byref(input_event), ctypes.sizeof(input_event)) # 调用示例(替换原有mouse_event逻辑) simulate_mouse_click(data["x"], data["y"], data["mouseDown"])
内容的提问来源于stack exchange,提问作者nathan lachenmyer
相关产品推荐
相关产品推荐

