Enhance hotkey recording functionality in MainWindow.xaml.cs by adding escape key support to cancel recording and improving error handling. Introduce a delayed registration mechanism to prevent immediate re-entry of hotkeys, ensuring stability during hotkey updates.
This commit is contained in:
@@ -255,7 +255,7 @@ namespace ClipForge
|
|||||||
{
|
{
|
||||||
if (_isRecordingHotkey) return;
|
if (_isRecordingHotkey) return;
|
||||||
_isRecordingHotkey = true;
|
_isRecordingHotkey = true;
|
||||||
HotkeyRecorderButton.Content = "Press any key...";
|
HotkeyRecorderButton.Content = "Press any key... (Esc to cancel)";
|
||||||
HotkeyRecorderButton.Focus(FocusState.Programmatic);
|
HotkeyRecorderButton.Focus(FocusState.Programmatic);
|
||||||
HotkeyRecorderButton.KeyDown += OnHotkeyCaptureKeyDown;
|
HotkeyRecorderButton.KeyDown += OnHotkeyCaptureKeyDown;
|
||||||
}
|
}
|
||||||
@@ -265,9 +265,19 @@ namespace ClipForge
|
|||||||
if (!_isRecordingHotkey) return;
|
if (!_isRecordingHotkey) return;
|
||||||
e.Handled = true;
|
e.Handled = true;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
var key = e.Key;
|
var key = e.Key;
|
||||||
|
// Escape cancels without changing the hotkey
|
||||||
|
if (key == VirtualKey.Escape)
|
||||||
|
{
|
||||||
|
StopRecordingHotkey(restoreDisplay: true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (HotkeyHelper.IsModifierKey(key))
|
if (HotkeyHelper.IsModifierKey(key))
|
||||||
return; // wait for a non-modifier key
|
return; // wait for a non-modifier key
|
||||||
|
if (key == VirtualKey.None)
|
||||||
|
return;
|
||||||
|
|
||||||
uint mod = 0;
|
uint mod = 0;
|
||||||
if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0) mod |= HotkeyHelper.MOD_CONTROL;
|
if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0) mod |= HotkeyHelper.MOD_CONTROL;
|
||||||
@@ -279,12 +289,48 @@ namespace ClipForge
|
|||||||
_settingsService.Settings.HotkeyModifiers = (int)mod;
|
_settingsService.Settings.HotkeyModifiers = (int)mod;
|
||||||
_settingsService.Settings.HotkeyVirtualKey = (int)vk;
|
_settingsService.Settings.HotkeyVirtualKey = (int)vk;
|
||||||
|
|
||||||
var ok = _hotkeyService.UpdateHotkey(mod, vk);
|
|
||||||
var display = HotkeyHelper.ToDisplayString(mod, vk);
|
var display = HotkeyHelper.ToDisplayString(mod, vk);
|
||||||
HotkeyRecorderButton.Content = ok ? display : display + " (in use?)";
|
StopRecordingHotkey(restoreDisplay: false);
|
||||||
|
HotkeyRecorderButton.Content = display;
|
||||||
|
|
||||||
|
// Defer registration so the key is released first; otherwise the new hotkey can fire
|
||||||
|
// immediately and re-enter (e.g. OnClipRequested) and crash.
|
||||||
|
_ = DelayedHotkeyRegister(mod, vk, display);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
StopRecordingHotkey(restoreDisplay: true);
|
||||||
|
_ = ShowToastAsync("⚠ " + ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void StopRecordingHotkey(bool restoreDisplay)
|
||||||
|
{
|
||||||
HotkeyRecorderButton.KeyDown -= OnHotkeyCaptureKeyDown;
|
HotkeyRecorderButton.KeyDown -= OnHotkeyCaptureKeyDown;
|
||||||
_isRecordingHotkey = false;
|
_isRecordingHotkey = false;
|
||||||
|
if (restoreDisplay)
|
||||||
|
{
|
||||||
|
var s = _settingsService.Settings;
|
||||||
|
HotkeyRecorderButton.Content = HotkeyHelper.ToDisplayString((uint)s.HotkeyModifiers, (uint)s.HotkeyVirtualKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async System.Threading.Tasks.Task DelayedHotkeyRegister(uint mod, uint vk, string display)
|
||||||
|
{
|
||||||
|
await System.Threading.Tasks.Task.Delay(300);
|
||||||
|
App.MainQueue?.TryEnqueue(() =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var ok = _hotkeyService.UpdateHotkey(mod, vk);
|
||||||
|
HotkeyRecorderButton.Content = ok ? display : display + " (in use?)";
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
HotkeyRecorderButton.Content = display;
|
||||||
|
_ = ShowToastAsync("⚠ " + ex.Message);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- NAV ---
|
// --- NAV ---
|
||||||
|
|||||||
Reference in New Issue
Block a user