From 7ff53ebcae10130fb37710fa779815c5104025b8 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Thu, 22 Jan 2026 18:21:13 +0100 Subject: [PATCH] Fixed shortcut key monitoring --- .../Dialogs/ShortcutDialog.razor | 57 ++++++++--------- .../Dialogs/ShortcutDialog.razor.cs | 61 ++++++++----------- 2 files changed, 52 insertions(+), 66 deletions(-) diff --git a/app/MindWork AI Studio/Dialogs/ShortcutDialog.razor b/app/MindWork AI Studio/Dialogs/ShortcutDialog.razor index d515cfc2..f48eb539 100644 --- a/app/MindWork AI Studio/Dialogs/ShortcutDialog.razor +++ b/app/MindWork AI Studio/Dialogs/ShortcutDialog.razor @@ -11,45 +11,38 @@ @T("Press the desired key combination to set the shortcut. The shortcut will be registered globally and will work even when the app is not focused.") - - - @* Hidden input to capture keyboard events *@ - - @if (string.IsNullOrWhiteSpace(this.currentShortcut)) - { - - @T("Press a key combination...") - - } - else - { - - @this.GetDisplayShortcut() - - } - - + + + + + @if (!string.IsNullOrWhiteSpace(this.validationMessage)) { - + @this.validationMessage } - - - @T("Supported modifiers: Ctrl/Cmd, Shift, Alt. Example: Ctrl+Shift+R") - - + @T("Clear Shortcut") diff --git a/app/MindWork AI Studio/Dialogs/ShortcutDialog.razor.cs b/app/MindWork AI Studio/Dialogs/ShortcutDialog.razor.cs index 3b91e456..588140bb 100644 --- a/app/MindWork AI Studio/Dialogs/ShortcutDialog.razor.cs +++ b/app/MindWork AI Studio/Dialogs/ShortcutDialog.razor.cs @@ -28,44 +28,42 @@ public partial class ShortcutDialog : MSGComponentBase /// [Parameter] public string ShortcutName { get; set; } = string.Empty; + + private static readonly Dictionary USER_INPUT_ATTRIBUTES = new(); - private ElementReference hiddenInput; private string currentShortcut = string.Empty; private string validationMessage = string.Empty; private Severity validationSeverity = Severity.Info; private bool hasValidationError; - // Current key state + // + // Current key state: + // private bool hasCtrl; private bool hasShift; private bool hasAlt; private bool hasMeta; private string? currentKey; - - private bool isFirstRender = true; + private MudTextField? inputField; #region Overrides of ComponentBase - protected override void OnInitialized() + protected override async Task OnInitializedAsync() { - base.OnInitialized(); + await base.OnInitializedAsync(); + + // Configure the spellchecking for the user input: + this.SettingsManager.InjectSpellchecking(USER_INPUT_ATTRIBUTES); + this.currentShortcut = this.InitialShortcut; this.ParseExistingShortcut(); } - protected override async Task OnAfterRenderAsync(bool firstRender) - { - await base.OnAfterRenderAsync(firstRender); - - // Auto-focus the hidden input when the dialog opens - if (this.isFirstRender) - { - this.isFirstRender = false; - await this.hiddenInput.FocusAsync(); - } - } - #endregion + + private string ShowText => string.IsNullOrWhiteSpace(this.currentShortcut) + ? T("Press a key combination...") + : this.GetDisplayShortcut(); private void ParseExistingShortcut() { @@ -107,15 +105,9 @@ public partial class ShortcutDialog : MSGComponentBase } } - private async Task FocusInput() - { - // Focus the hidden input to capture keyboard events - await this.hiddenInput.FocusAsync(); - } - private async Task HandleKeyDown(KeyboardEventArgs e) { - // Ignore pure modifier key presses + // Ignore pure modifier key presses: if (IsModifierKey(e.Code)) { this.UpdateModifiers(e); @@ -124,10 +116,9 @@ public partial class ShortcutDialog : MSGComponentBase return; } - // Update modifiers this.UpdateModifiers(e); - // Get the key + // Get the key: this.currentKey = TranslateKeyCode(e.Code); // Validate: must have at least one modifier + a key @@ -140,11 +131,10 @@ public partial class ShortcutDialog : MSGComponentBase return; } - // Build the shortcut string this.UpdateShortcutString(); - - // Validate the shortcut await this.ValidateShortcut(); + + this.StateHasChanged(); } private void UpdateModifiers(KeyboardEventArgs e) @@ -213,10 +203,7 @@ public partial class ShortcutDialog : MSGComponentBase private string GetDisplayShortcut() { - if (string.IsNullOrWhiteSpace(this.currentShortcut)) - return string.Empty; - - // Convert internal format to display format + // Convert internal format to display format: return this.currentShortcut .Replace("CmdOrControl", OperatingSystem.IsMacOS() ? "Cmd" : "Ctrl") .Replace("CommandOrControl", OperatingSystem.IsMacOS() ? "Cmd" : "Ctrl"); @@ -376,4 +363,10 @@ public partial class ShortcutDialog : MSGComponentBase // Default: return as-is _ => code, }; + + private void HandleBlur() + { + // Re-focus the input field to keep capturing keys: + this.inputField?.FocusAsync(); + } }