Improve hotkey capture and validation
This commit is contained in:
@@ -312,13 +312,78 @@ public partial class MainWindow : Window
|
||||
return;
|
||||
}
|
||||
|
||||
controller.Config.Hotkey = string.IsNullOrWhiteSpace(HotkeyBox.Text) ? "Ctrl+Alt+Space" : HotkeyBox.Text.Trim();
|
||||
controller.Config.Hotkey = string.IsNullOrWhiteSpace(HotkeyBox.Text)
|
||||
? "Ctrl+Alt+Space"
|
||||
: HotkeyService.Normalize(HotkeyBox.Text.Trim());
|
||||
controller.Config.DisplayMode = DisplayModeBox.SelectedItem is LauncherDisplayMode mode
|
||||
? mode
|
||||
: LauncherDisplayMode.LargeIconWithText;
|
||||
ApplyCurrentItem();
|
||||
}
|
||||
|
||||
private void HotkeyBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
|
||||
{
|
||||
Key key = e.Key == Key.System ? e.SystemKey : e.Key;
|
||||
if (key == Key.ImeProcessed)
|
||||
{
|
||||
key = e.ImeProcessedKey;
|
||||
}
|
||||
|
||||
if (key is Key.LeftCtrl or Key.RightCtrl or Key.LeftAlt or Key.RightAlt or Key.LeftShift or Key.RightShift
|
||||
or Key.LWin or Key.RWin)
|
||||
{
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (key is Key.Back or Key.Delete or Key.Escape)
|
||||
{
|
||||
HotkeyBox.Text = "";
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
ModifierKeys modifiers = Keyboard.Modifiers;
|
||||
if (modifiers == ModifierKeys.None)
|
||||
{
|
||||
StatusText.Text = "Press a hotkey with Ctrl, Alt, Shift, or Win plus another key.";
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
HotkeyBox.Text = FormatHotkey(modifiers, key);
|
||||
HotkeyBox.CaretIndex = HotkeyBox.Text.Length;
|
||||
StatusText.Text = "Click Save to apply the new hotkey.";
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private static string FormatHotkey(ModifierKeys modifiers, Key key)
|
||||
{
|
||||
List<string> parts = [];
|
||||
if (modifiers.HasFlag(ModifierKeys.Control))
|
||||
{
|
||||
parts.Add("Ctrl");
|
||||
}
|
||||
|
||||
if (modifiers.HasFlag(ModifierKeys.Alt))
|
||||
{
|
||||
parts.Add("Alt");
|
||||
}
|
||||
|
||||
if (modifiers.HasFlag(ModifierKeys.Shift))
|
||||
{
|
||||
parts.Add("Shift");
|
||||
}
|
||||
|
||||
if (modifiers.HasFlag(ModifierKeys.Windows))
|
||||
{
|
||||
parts.Add("Win");
|
||||
}
|
||||
|
||||
parts.Add(key.ToString());
|
||||
return string.Join("+", parts);
|
||||
}
|
||||
|
||||
private void LoadStartupState()
|
||||
{
|
||||
isLoadingStartupState = true;
|
||||
@@ -569,9 +634,24 @@ public partial class MainWindow : Window
|
||||
return;
|
||||
}
|
||||
|
||||
ApplyConfigFields();
|
||||
ConfigService.Save(controller.Config);
|
||||
controller.ReloadConfig();
|
||||
string previousHotkey = controller.Config.Hotkey;
|
||||
try
|
||||
{
|
||||
ApplyConfigFields();
|
||||
ConfigService.Save(controller.Config);
|
||||
controller.ReloadConfig();
|
||||
}
|
||||
catch (Exception ex) when (ex is FormatException or InvalidOperationException)
|
||||
{
|
||||
controller.Config.Hotkey = previousHotkey;
|
||||
ConfigService.Save(controller.Config);
|
||||
controller.ReloadConfig();
|
||||
HotkeyBox.Text = previousHotkey;
|
||||
MessageBox.Show($"Could not apply hotkey.\n\n{ex.Message}", "Taskbar Launcher");
|
||||
StatusText.Text = $"Hotkey was not changed. Current hotkey: {previousHotkey}";
|
||||
return;
|
||||
}
|
||||
|
||||
LauncherItem? reloadedSelection = selectedPath is null ? null : FindItemByPath(controller.Config.Items, selectedPath);
|
||||
RefreshView(reloadedSelection);
|
||||
StatusText.Text = reloadedSelection is null
|
||||
|
||||
Reference in New Issue
Block a user