using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
namespace Ashwild.Player
{
///
/// 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).
///
public enum KeyboardLayout
{
Unknown,
Qwerty,
Azerty
}
///
/// Classifies the current OS keyboard layout by probing . 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.
///
public static class KeyboardLayoutDetector
{
#region Public API
///
/// Returns the detected layout, or when no keyboard is
/// connected or the probes disagree with nothing conclusive.
///
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
///
/// The lower-cased, trimmed display name of a key control, or empty when unavailable.
///
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();
}
///
/// Last-resort classification from the OS layout name (e.g. "French" / "AZERTY").
///
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
}
}