73 lines
2.5 KiB
C#
73 lines
2.5 KiB
C#
using UnityEngine.InputSystem;
|
|
using UnityEngine.InputSystem.Controls;
|
|
|
|
namespace Ashwild.Player
|
|
{
|
|
/// <summary>
|
|
/// The physical keyboard layouts we tell apart at first launch. Unknown means no keyboard was
|
|
/// present or probing was inconclusive — callers treat it like QWERTY (apply no profile).
|
|
/// </summary>
|
|
public enum KeyboardLayout
|
|
{
|
|
Unknown,
|
|
Qwerty,
|
|
Azerty
|
|
}
|
|
|
|
/// <summary>
|
|
/// Classifies the current OS keyboard layout by probing <see cref="Keyboard.current"/>. The
|
|
/// primary signal is the layout-aware display name of the physical Q/W keys (AZERTY labels them
|
|
/// A/Z); the OS-reported layout string is a fallback. Pure and side-effect-free — the caller
|
|
/// decides what to do with the result.
|
|
/// </summary>
|
|
public static class KeyboardLayoutDetector
|
|
{
|
|
#region Public API
|
|
|
|
/// <summary>
|
|
/// Returns the detected layout, or <see cref="KeyboardLayout.Unknown"/> when no keyboard is
|
|
/// connected or the probes disagree with nothing conclusive.
|
|
/// </summary>
|
|
public static KeyboardLayout Detect()
|
|
{
|
|
Keyboard keyboard = Keyboard.current;
|
|
if (keyboard == null) return KeyboardLayout.Unknown;
|
|
|
|
string q = Label(keyboard[Key.Q]);
|
|
string w = Label(keyboard[Key.W]);
|
|
|
|
if (q == "a" || w == "z") return KeyboardLayout.Azerty;
|
|
if (q == "q" || w == "w") return KeyboardLayout.Qwerty;
|
|
|
|
return FromLayoutString(keyboard.keyboardLayout);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Internal Helpers
|
|
|
|
/// <summary>
|
|
/// The lower-cased, trimmed display name of a key control, or empty when unavailable.
|
|
/// </summary>
|
|
private static string Label(KeyControl key)
|
|
{
|
|
if (key == null) return string.Empty;
|
|
string display = key.displayName;
|
|
return string.IsNullOrEmpty(display) ? string.Empty : display.Trim().ToLowerInvariant();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Last-resort classification from the OS layout name (e.g. "French" / "AZERTY").
|
|
/// </summary>
|
|
private static KeyboardLayout FromLayoutString(string layout)
|
|
{
|
|
if (string.IsNullOrEmpty(layout)) return KeyboardLayout.Unknown;
|
|
string l = layout.ToLowerInvariant();
|
|
if (l.Contains("azerty") || l.Contains("french") || l.Contains("belg")) return KeyboardLayout.Azerty;
|
|
return KeyboardLayout.Unknown;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|