From ab606317282eb770742092e8850bb21d22a5ee3a Mon Sep 17 00:00:00 2001 From: Alex 'mcmonkey' Goodwin Date: Tue, 2 Nov 2021 02:28:34 -0700 Subject: [PATCH] auto-disable programs: fully remove the keyboard hook when disabled relates to #22 --- KeyboardChatterBlocker/KeyboardInterceptor.cs | 25 ++++++++++++++++--- KeyboardChatterBlocker/MainBlockerForm.cs | 4 +++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/KeyboardChatterBlocker/KeyboardInterceptor.cs b/KeyboardChatterBlocker/KeyboardInterceptor.cs index 80f6fe8..49296b3 100644 --- a/KeyboardChatterBlocker/KeyboardInterceptor.cs +++ b/KeyboardChatterBlocker/KeyboardInterceptor.cs @@ -86,7 +86,18 @@ public KeyboardInterceptor(KeyBlocker blocker) KeyBlockHandler = blocker; KeyboardProcCallback = KeyboardHookCallback; MouseProcCallback = MouseHookCallback; - KeyboardHookID = SetKeyboardHook(KeyboardProcCallback); + EnableKeyboardHook(); + } + + /// + /// Enables the keyboard hook (if not already enabled). + /// + public void EnableKeyboardHook() + { + if (KeyboardHookID == IntPtr.Zero) + { + KeyboardHookID = SetKeyboardHook(KeyboardProcCallback); + } } /// @@ -300,15 +311,23 @@ public void DisableMouseHook() } /// - /// Dispose the object, removing the hook. + /// Disables the keyboard hook (to fully disable when Auto-Disable Programs are used). /// - protected virtual void Dispose(bool disposing) + public void DisableKeyboardHook() { if (KeyboardHookID != IntPtr.Zero) { UnhookWindowsHookEx(KeyboardHookID); KeyboardHookID = IntPtr.Zero; } + } + + /// + /// Dispose the object, removing the hook. + /// + protected virtual void Dispose(bool disposing) + { + DisableKeyboardHook(); DisableMouseHook(); } diff --git a/KeyboardChatterBlocker/MainBlockerForm.cs b/KeyboardChatterBlocker/MainBlockerForm.cs index e8ecf8d..e103059 100644 --- a/KeyboardChatterBlocker/MainBlockerForm.cs +++ b/KeyboardChatterBlocker/MainBlockerForm.cs @@ -162,11 +162,15 @@ public void SetAutoDisable(bool disable) Program.Blocker.IsAutoDisabled = disable; if (disable) { + Program.Blocker.Interceptor.DisableKeyboardHook(); + Program.Blocker.Interceptor.DisableMouseHook(); EnableNoteLabel.Text = "(Automatically disabled due to an open process)"; EnableNoteLabel.BackColor = Color.FromArgb(64, 255, 0, 0); } else { + Program.Blocker.Interceptor.EnableKeyboardHook(); + Program.Blocker.AutoEnableMouse(); EnableNoteLabel.Text = ""; EnableNoteLabel.BackColor = Color.Transparent; foreach (string notBlocking in Program.Blocker.AutoDisablePrograms)