using Avalonia.Controls; using Avalonia.Input; namespace SlamCross.Views; public partial class SelectKeyWindow : Window { public string? ChosenKey { get; private set; } public SelectKeyWindow() => InitializeComponent(); private void KeyBox_KeyDown(object? sender, KeyEventArgs e) { ChosenKey = MapKey(e.Key); if (this.FindControl("KeyBox") is TextBox box) box.Text = ChosenKey; e.Handled = true; } private void OK_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e) { if (this.FindControl("KeyBox") is TextBox box && !string.IsNullOrWhiteSpace(box.Text)) ChosenKey = box.Text.Trim().ToUpper(); Close(); } private void Cancel_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e) { ChosenKey = null; Close(); } private static string MapKey(Key key) => key switch { >= Key.D0 and <= Key.D9 => ((char)('0' + (key - Key.D0))).ToString(), Key.F1 => "F1", Key.F2 => "F2", Key.F3 => "F3", Key.F4 => "F4", Key.F5 => "F5", Key.F6 => "F6", Key.F7 => "F7", Key.F8 => "F8", Key.F9 => "F9", Key.F10 => "F10", Key.F11 => "F11", Key.F12 => "F12", Key.Insert => "INS", Key.Delete => "DEL", Key.Home => "HOME", Key.End => "END", Key.PageUp => "PGUP", Key.PageDown => "PGDN", Key.NumPad0 => "KP_INS", Key.NumPad1 => "KP_END", Key.NumPad2 => "KP_DOWNARROW", Key.NumPad3 => "KP_PGDN", Key.NumPad4 => "KP_LEFTARROW", Key.NumPad5 => "KP_5", Key.NumPad6 => "KP_RIGHTARROW", Key.NumPad7 => "KP_HOME", Key.NumPad8 => "KP_UPARROW", Key.NumPad9 => "KP_PGUP", Key.Multiply => "KP_MULTIPLY", Key.Add => "KP_PLUS", Key.Subtract => "KP_MINUS", Key.Divide => "KP_SLASH", Key.OemMinus => "MINUS", Key.OemPlus => "EQUALS", Key.OemOpenBrackets => "LBRACKET", Key.OemCloseBrackets => "RBRACKET", Key.OemSemicolon => "SEMICOLON", Key.OemQuotes => "APOSTROPHE", Key.OemBackslash => "BACKSLASH", Key.OemComma => "COMMA", Key.OemPeriod => "PERIOD", _ => key.ToString().ToUpper() }; }