Added step to dispose the keyboard hook object (#4449)

This commit is contained in:
Arjun Balgovind 2020-06-23 17:19:36 -07:00 committed by GitHub
parent ca99f60964
commit 2104a1250e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 1 deletions

View File

@ -25,6 +25,12 @@ KeyboardHook::KeyboardHook(
KeyboardHook::~KeyboardHook() KeyboardHook::~KeyboardHook()
{ {
quit = true; 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(); kbEventDispatch->Join();
// Unregister low level hook procedure // Unregister low level hook procedure

View File

@ -6,7 +6,7 @@ using interop;
namespace Microsoft.PowerToys.Settings.UI.Lib namespace Microsoft.PowerToys.Settings.UI.Lib
{ {
public delegate void KeyEvent(int key); public delegate void KeyEvent(int key);
public delegate bool IsActive(); public delegate bool IsActive();
public class HotkeySettingsControlHook public class HotkeySettingsControlHook
@ -49,5 +49,11 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
break; break;
} }
} }
public void Dispose()
{
// Dispose the KeyboardHook object to terminate the hook threads
hook.Dispose();
}
} }
} }

View File

@ -8,6 +8,7 @@ using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Input;
using System; using System;
using System.Data;
// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236 // The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236
namespace Microsoft.PowerToys.Settings.UI.Controls namespace Microsoft.PowerToys.Settings.UI.Controls
@ -54,9 +55,16 @@ namespace Microsoft.PowerToys.Settings.UI.Controls
HotkeyTextBox.GettingFocus += HotkeyTextBox_GettingFocus; HotkeyTextBox.GettingFocus += HotkeyTextBox_GettingFocus;
HotkeyTextBox.LosingFocus += HotkeyTextBox_LosingFocus; HotkeyTextBox.LosingFocus += HotkeyTextBox_LosingFocus;
HotkeyTextBox.Unloaded += HotkeyTextBox_Unloaded;
hook = new HotkeySettingsControlHook(Hotkey_KeyDown, Hotkey_KeyUp, Hotkey_IsActive); 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) private void KeyEventHandler(int key, bool matchValue, int matchValueCode, string matchValueText)
{ {
switch ((Windows.System.VirtualKey)key) switch ((Windows.System.VirtualKey)key)