diff --git a/src/common/interop/KeyboardHook.cpp b/src/common/interop/KeyboardHook.cpp index 5fc9106605..d25e66962f 100644 --- a/src/common/interop/KeyboardHook.cpp +++ b/src/common/interop/KeyboardHook.cpp @@ -25,6 +25,12 @@ KeyboardHook::KeyboardHook( KeyboardHook::~KeyboardHook() { quit = true; + + // Notify the DispatchProc thread so that it isn't stuck at the Wait step + Monitor::Enter(queue); + Monitor::Pulse(queue); + Monitor::Exit(queue); + kbEventDispatch->Join(); // Unregister low level hook procedure diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/HotkeySettingsControlHook.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/HotkeySettingsControlHook.cs index e3422ac342..738d768ce0 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/HotkeySettingsControlHook.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/HotkeySettingsControlHook.cs @@ -6,7 +6,7 @@ using interop; namespace Microsoft.PowerToys.Settings.UI.Lib { public delegate void KeyEvent(int key); - + public delegate bool IsActive(); public class HotkeySettingsControlHook @@ -49,5 +49,11 @@ namespace Microsoft.PowerToys.Settings.UI.Lib break; } } + + public void Dispose() + { + // Dispose the KeyboardHook object to terminate the hook threads + hook.Dispose(); + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Controls/HotkeySettingsControl.xaml.cs b/src/core/Microsoft.PowerToys.Settings.UI/Controls/HotkeySettingsControl.xaml.cs index a2b11a3d98..ba8e7cd32a 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Controls/HotkeySettingsControl.xaml.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/Controls/HotkeySettingsControl.xaml.cs @@ -8,6 +8,7 @@ using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; using System; +using System.Data; // The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236 namespace Microsoft.PowerToys.Settings.UI.Controls @@ -54,9 +55,16 @@ namespace Microsoft.PowerToys.Settings.UI.Controls HotkeyTextBox.GettingFocus += HotkeyTextBox_GettingFocus; HotkeyTextBox.LosingFocus += HotkeyTextBox_LosingFocus; + HotkeyTextBox.Unloaded += HotkeyTextBox_Unloaded; hook = new HotkeySettingsControlHook(Hotkey_KeyDown, Hotkey_KeyUp, Hotkey_IsActive); } + private void HotkeyTextBox_Unloaded(object sender, RoutedEventArgs e) + { + // Dispose the HotkeySettingsControlHook object to terminate the hook threads when the textbox is unloaded + hook.Dispose(); + } + private void KeyEventHandler(int key, bool matchValue, int matchValueCode, string matchValueText) { switch ((Windows.System.VirtualKey)key)