Skip to content

Commit

Permalink
auto-disable programs: fully remove the keyboard hook when disabled
Browse files Browse the repository at this point in the history
relates to #22
  • Loading branch information
mcmonkey4eva committed Nov 2, 2021
1 parent 6a234bf commit ab60631
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
25 changes: 22 additions & 3 deletions KeyboardChatterBlocker/KeyboardInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,18 @@ public KeyboardInterceptor(KeyBlocker blocker)
KeyBlockHandler = blocker;
KeyboardProcCallback = KeyboardHookCallback;
MouseProcCallback = MouseHookCallback;
KeyboardHookID = SetKeyboardHook(KeyboardProcCallback);
EnableKeyboardHook();
}

/// <summary>
/// Enables the keyboard hook (if not already enabled).
/// </summary>
public void EnableKeyboardHook()
{
if (KeyboardHookID == IntPtr.Zero)
{
KeyboardHookID = SetKeyboardHook(KeyboardProcCallback);
}
}

/// <summary>
Expand Down Expand Up @@ -300,15 +311,23 @@ public void DisableMouseHook()
}

/// <summary>
/// Dispose the object, removing the hook.
/// Disables the keyboard hook (to fully disable when Auto-Disable Programs are used).
/// </summary>
protected virtual void Dispose(bool disposing)
public void DisableKeyboardHook()
{
if (KeyboardHookID != IntPtr.Zero)
{
UnhookWindowsHookEx(KeyboardHookID);
KeyboardHookID = IntPtr.Zero;
}
}

/// <summary>
/// Dispose the object, removing the hook.
/// </summary>
protected virtual void Dispose(bool disposing)
{
DisableKeyboardHook();
DisableMouseHook();
}

Expand Down
4 changes: 4 additions & 0 deletions KeyboardChatterBlocker/MainBlockerForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ab60631

Please sign in to comment.