From d1153043d87fe9105b45e4922cdbf62e9fa2ddce Mon Sep 17 00:00:00 2001 From: KcDaRookie Date: Wed, 8 Jul 2026 10:13:30 +0200 Subject: [PATCH] Add option to disable only if program in list is focused --- KeyboardChatterBlocker/KeyBlocker.cs | 9 +++ .../MainBlockerForm.Designer.cs | 15 +++++ KeyboardChatterBlocker/MainBlockerForm.cs | 63 ++++++++++++++++++- KeyboardChatterBlocker/MainBlockerForm.resx | 33 ++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-) diff --git a/KeyboardChatterBlocker/KeyBlocker.cs b/KeyboardChatterBlocker/KeyBlocker.cs index dbb2cdb..463bfae 100644 --- a/KeyboardChatterBlocker/KeyBlocker.cs +++ b/KeyboardChatterBlocker/KeyBlocker.cs @@ -169,6 +169,9 @@ public void ApplyConfigSetting(string setting) case "auto_disable_on_fullscreen": AutoDisableOnFullscreen = SettingAsBool(settingValue); break; + case "disable_only_on_focus": + DisableOnlyIfFocus = SettingAsBool(settingValue); + break; case "other_key_resets_timeout": OtherKeyResetsTimeout = SettingAsBool(settingValue); break; @@ -273,6 +276,7 @@ public string GetConfigurationString() result.Append("auto_disable_programs: ").Append(string.Join("/", AutoDisablePrograms)).Append("\n"); } result.Append("auto_disable_on_fullscreen: ").Append(AutoDisableOnFullscreen ? "true" : "false").Append("\n"); + result.Append("disable_only_on_focus: ").Append(DisableOnlyIfFocus ? "true" : "false").Append("\n"); result.Append("other_key_resets_timeout: ").Append(OtherKeyResetsTimeout ? "true" : "false").Append("\n"); result.Append("\n"); foreach (KeyValuePair pair in Hotkeys) @@ -366,6 +370,11 @@ public string GetConfigurationString() /// public bool AutoDisableOnFullscreen = false; + /// + /// Whether to automatically disable the blocker only if one of listed programs are focused. + /// + public bool DisableOnlyIfFocus = false; + /// /// If true, reset timeouts for keys when another key is pressed. /// diff --git a/KeyboardChatterBlocker/MainBlockerForm.Designer.cs b/KeyboardChatterBlocker/MainBlockerForm.Designer.cs index 19faefd..4e5886c 100644 --- a/KeyboardChatterBlocker/MainBlockerForm.Designer.cs +++ b/KeyboardChatterBlocker/MainBlockerForm.Designer.cs @@ -80,6 +80,7 @@ private void InitializeComponent() this.AboutLabel2 = new System.Windows.Forms.Label(); this.AboutLabel1 = new System.Windows.Forms.Label(); this.EnableNoteLabel = new System.Windows.Forms.Label(); + this.DisableOnFocusCheckbox = new System.Windows.Forms.CheckBox(); ((System.ComponentModel.ISupportInitialize)(this.ChatterLogGrid)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.ChatterThresholdBox)).BeginInit(); this.TrayIconContextMenu.SuspendLayout(); @@ -402,6 +403,7 @@ private void InitializeComponent() // // AutoDisableProgramsTabPage // + this.AutoDisableProgramsTabPage.Controls.Add(this.DisableOnFocusCheckbox); this.AutoDisableProgramsTabPage.Controls.Add(this.AutoDisableOnFullscreenCheckbox); this.AutoDisableProgramsTabPage.Controls.Add(this.AutoDisableProgramsList); this.AutoDisableProgramsTabPage.Controls.Add(this.RemoveProgramButton); @@ -646,6 +648,18 @@ private void InitializeComponent() this.EnableNoteLabel.Text = "EnableNote"; this.EnableNoteLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // + // DisableOnFocusCheckbox + // + this.DisableOnFocusCheckbox.AutoSize = true; + this.DisableOnFocusCheckbox.CheckAlign = System.Drawing.ContentAlignment.MiddleRight; + this.DisableOnFocusCheckbox.Location = new System.Drawing.Point(8, 5); + this.DisableOnFocusCheckbox.Name = "DisableOnFocusCheckbox"; + this.DisableOnFocusCheckbox.Size = new System.Drawing.Size(132, 17); + this.DisableOnFocusCheckbox.TabIndex = 7; + this.DisableOnFocusCheckbox.Text = "Disable only if focused"; + this.DisableOnFocusCheckbox.UseVisualStyleBackColor = true; + this.DisableOnFocusCheckbox.CheckedChanged += new System.EventHandler(this.DisableOnFocusCheckbox_CheckedChanged); + // // MainBlockerForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -737,6 +751,7 @@ private void InitializeComponent() private System.Windows.Forms.Label label1; private System.Windows.Forms.ComboBox MeasureFromComboBox; private System.Windows.Forms.CheckBox SaveStatsCheckbox; + private System.Windows.Forms.CheckBox DisableOnFocusCheckbox; } } diff --git a/KeyboardChatterBlocker/MainBlockerForm.cs b/KeyboardChatterBlocker/MainBlockerForm.cs index 5a7a0ab..cc84362 100644 --- a/KeyboardChatterBlocker/MainBlockerForm.cs +++ b/KeyboardChatterBlocker/MainBlockerForm.cs @@ -10,6 +10,7 @@ using System.Diagnostics; using System.IO; using System.Globalization; +using System.Runtime.InteropServices; namespace KeyboardChatterBlocker { @@ -18,6 +19,7 @@ namespace KeyboardChatterBlocker /// public partial class MainBlockerForm : Form { + /// /// Whether the form is still loading. /// @@ -108,6 +110,12 @@ public MainBlockerForm() versionAboutLabel.Text = "Version: " + Application.ProductVersion; } + [DllImport("user32.dll")] + private static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll")] + private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); + /// /// Method auto-called (by event) for when a key is blocked. /// @@ -272,7 +280,21 @@ public void CheckAutoDisable() return; } bool any = false; - foreach (string proc in Process.GetProcesses().Select(p => p.ProcessName.ToLowerInvariant())) + + List processes = new List(); + + if (Program.Blocker.DisableOnlyIfFocus) + { + processes.Add(GetActiveProcessName().ToLowerInvariant()); + } + else + { + foreach (string proc in Process.GetProcesses().Select(p => p.ProcessName.ToLowerInvariant())){ + processes.Add((string)proc); + } + } + + foreach (string proc in processes) { if (programsToCheck.Contains(proc)) { @@ -324,6 +346,7 @@ public void MainBlockerForm_Load(object sender, EventArgs e) AutoDisableTimer.Start(); AutoDisableProgramsList.Items.AddRange(Program.Blocker.AutoDisablePrograms.Select(s => new ListViewItem(s)).ToArray()); AutoDisableOnFullscreenCheckbox.Checked = Program.Blocker.AutoDisableOnFullscreen; + DisableOnFocusCheckbox.Checked = Program.Blocker.DisableOnlyIfFocus; ChatterThresholdBox.Value = Program.Blocker.GlobalChatterTimeLimit; MeasureFromComboBox.Text = Program.Blocker.MeasureMode.ToString(); EnabledCheckbox.Checked = Program.Blocker.IsEnabled; @@ -366,6 +389,31 @@ public void StatsUpdateTimer_Tick(object sender, EventArgs e) } } + public static string GetActiveProcessName() + { + // 2. Get the handle of the focused window + IntPtr hwnd = GetForegroundWindow(); + if (hwnd == IntPtr.Zero) return "unknown"; + + // 3. Get the Process ID (PID) from that window handle + GetWindowThreadProcessId(hwnd, out uint pid); + + try + { + // 4. Get the process object and return its lowercased name + using (Process p = Process.GetProcessById((int)pid)) + { + return p.ProcessName.ToLowerInvariant(); + } + } + catch (Exception) + { + // Handles cases where the process closes abruptly + // or your app lacks permissions to access it + return "unknown"; + } + } + /// /// If enabled, any close should fully close the form and exit the program. /// @@ -725,6 +773,19 @@ private void AutoDisableOnFullscreenCheckbox_CheckedChanged(object sender, Event Program.Blocker.SaveConfig(); } + /// + /// Event method to handle the 'auto disable on fullscreen' checkbox state changing. + /// + private void DisableOnFocusCheckbox_CheckedChanged(object sender, EventArgs e) + { + if (Loading) + { + return; + } + Program.Blocker.DisableOnlyIfFocus = DisableOnFocusCheckbox.Checked; + Program.Blocker.SaveConfig(); + } + /// /// Event method to handle the 'other key reset' checkbox state changing. /// diff --git a/KeyboardChatterBlocker/MainBlockerForm.resx b/KeyboardChatterBlocker/MainBlockerForm.resx index 9cab9f7..48f914e 100644 --- a/KeyboardChatterBlocker/MainBlockerForm.resx +++ b/KeyboardChatterBlocker/MainBlockerForm.resx @@ -129,6 +129,18 @@ True + + True + + + True + + + True + + + True + 189, 17 @@ -1280,6 +1292,27 @@ True + + True + + + True + + + True + + + True + + + True + + + True + + + True + True