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