This commit is contained in:
陈思海 2025-12-16 14:00:30 +08:00
parent 1723b4d893
commit 4e24bd4f5d

View File

@ -24,6 +24,7 @@ public static class InputDeviceWatcher
private static int _lastDeviceId = -1; private static int _lastDeviceId = -1;
private static float _lastInputTime = -Mathf.Infinity; private static float _lastInputTime = -Mathf.Infinity;
private static InputDeviceCategory _lastEmittedCategory = InputDeviceCategory.Keyboard;
public static event Action<InputDeviceCategory> OnDeviceChanged; public static event Action<InputDeviceCategory> OnDeviceChanged;
@ -37,6 +38,7 @@ public static class InputDeviceWatcher
CurrentCategory = InputDeviceCategory.Keyboard; CurrentCategory = InputDeviceCategory.Keyboard;
CurrentDeviceName = ""; CurrentDeviceName = "";
_lastEmittedCategory = CurrentCategory; // 初始化同步
_anyInputAction = new InputAction("AnyDevice", InputActionType.PassThrough); _anyInputAction = new InputAction("AnyDevice", InputActionType.PassThrough);
_anyInputAction.AddBinding("<Keyboard>/anyKey"); _anyInputAction.AddBinding("<Keyboard>/anyKey");
@ -77,6 +79,8 @@ public static class InputDeviceWatcher
OnDeviceChanged = null; OnDeviceChanged = null;
initialized = false; initialized = false;
_lastEmittedCategory = InputDeviceCategory.Keyboard;
} }
private static void OnAnyInputPerformed(InputAction.CallbackContext ctx) private static void OnAnyInputPerformed(InputAction.CallbackContext ctx)
@ -96,7 +100,7 @@ public static class InputDeviceWatcher
CurrentCategory = DetermineCategoryFromDevice(device); CurrentCategory = DetermineCategoryFromDevice(device);
CurrentDeviceName = device.displayName ?? $"Device_{curId}"; CurrentDeviceName = device.displayName ?? $"Device_{curId}";
EmitChangeLog(); EmitChange();
} }
// ------------------ 监听设备变更 -------------------- // ------------------ 监听设备变更 --------------------
@ -110,7 +114,7 @@ public static class InputDeviceWatcher
_lastInputTime = -Mathf.Infinity; _lastInputTime = -Mathf.Infinity;
CurrentDeviceName = ""; CurrentDeviceName = "";
CurrentCategory = InputDeviceCategory.Keyboard; CurrentCategory = InputDeviceCategory.Keyboard;
EmitChangeLog(); EmitChange();
} }
} }
} }
@ -183,8 +187,13 @@ public static class InputDeviceWatcher
} }
// ------------------ 输出 -------------------- // ------------------ 输出 --------------------
private static void EmitChangeLog() private static void EmitChange()
{ {
if (CurrentCategory == _lastEmittedCategory)
{
return;
}
int vid = GetVendorId(); int vid = GetVendorId();
int pid = GetProductId(); int pid = GetProductId();
@ -192,7 +201,9 @@ public static class InputDeviceWatcher
Debug.Log($"输入设备变更 -> {CurrentCategory} 触发设备: {CurrentDeviceName} vid=0x{vid:X} pid=0x{pid:X}"); Debug.Log($"输入设备变更 -> {CurrentCategory} 触发设备: {CurrentDeviceName} vid=0x{vid:X} pid=0x{pid:X}");
#endif #endif
// 触发事件并记录已发射的分类
OnDeviceChanged?.Invoke(CurrentCategory); OnDeviceChanged?.Invoke(CurrentCategory);
_lastEmittedCategory = CurrentCategory;
} }
private static int GetVendorId() private static int GetVendorId()