This commit is contained in:
陈思海 2025-12-17 13:04:58 +08:00
parent f6715dbfae
commit c8bb190c2e

View File

@ -42,7 +42,6 @@ public static class InputDeviceWatcher
_anyInputAction = new InputAction("AnyDevice", InputActionType.PassThrough);
_anyInputAction.AddBinding("<Keyboard>/anyKey");
_anyInputAction.AddBinding("<Mouse>/*");
_anyInputAction.AddBinding("<Gamepad>/*");
_anyInputAction.AddBinding("<Joystick>/*");
@ -88,6 +87,9 @@ public static class InputDeviceWatcher
if (ctx.control == null || ctx.control.device == null) return;
var device = ctx.control.device;
if (!IsRelevantDevice(device)) return;
int curId = device.deviceId;
float now = Time.realtimeSinceStartup;
@ -123,7 +125,9 @@ public static class InputDeviceWatcher
private static InputDeviceCategory DetermineCategoryFromDevice(InputDevice device)
{
if (device == null) return InputDeviceCategory.Keyboard;
if (device is Keyboard || device is Mouse) return InputDeviceCategory.Keyboard;
// 重要:鼠标不再被视为键盘类(避免鼠标触发时回退到 Keyboard
if (device is Keyboard) return InputDeviceCategory.Keyboard;
if (device is Mouse) return InputDeviceCategory.Other; // 明确忽略鼠标
if (IsGamepadLike(device)) return GetGamepadCategory(device);
string combined = $"{device.description.interfaceName} {device.layout} {device.description.product} {device.description.manufacturer} {device.displayName}".ToLower();
@ -140,9 +144,19 @@ public static class InputDeviceWatcher
if (device is Joystick) return true;
var layout = (device.layout ?? "").ToLower();
// 这里保留 controller/gamepad/joystick 的识别,但忽略 mouse/touch 等
if (layout.Contains("mouse") || layout.Contains("touch") || layout.Contains("pen")) return false;
return layout.Contains("gamepad") || layout.Contains("controller") || layout.Contains("joystick");
}
private static bool IsRelevantDevice(InputDevice device)
{
if (device == null) return false;
if (device is Keyboard) return true;
if (IsGamepadLike(device)) return true;
return false;
}
private static InputDeviceCategory GetGamepadCategory(InputDevice device)
{
if (device == null) return InputDeviceCategory.Other;
@ -186,7 +200,6 @@ public static class InputDeviceWatcher
}
}
// ------------------ 输出 --------------------
private static void EmitChange()
{
if (CurrentCategory == _lastEmittedCategory)
@ -201,7 +214,6 @@ public static class InputDeviceWatcher
Debug.Log($"输入设备变更 -> {CurrentCategory} 触发设备: {CurrentDeviceName} vid=0x{vid:X} pid=0x{pid:X}");
#endif
// 触发事件并记录已发射的分类
OnDeviceChanged?.Invoke(CurrentCategory);
_lastEmittedCategory = CurrentCategory;
}