Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions KeyboardChatterBlocker/KeyBlocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, string> pair in Hotkeys)
Expand Down Expand Up @@ -366,6 +370,11 @@ public string GetConfigurationString()
/// </summary>
public bool AutoDisableOnFullscreen = false;

/// <summary>
/// Whether to automatically disable the blocker only if one of listed programs are focused.
/// </summary>
public bool DisableOnlyIfFocus = false;

/// <summary>
/// If true, reset timeouts for keys when another key is pressed.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions KeyboardChatterBlocker/MainBlockerForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 62 additions & 1 deletion KeyboardChatterBlocker/MainBlockerForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Diagnostics;
using System.IO;
using System.Globalization;
using System.Runtime.InteropServices;

namespace KeyboardChatterBlocker
{
Expand All @@ -18,6 +19,7 @@ namespace KeyboardChatterBlocker
/// </summary>
public partial class MainBlockerForm : Form
{

/// <summary>
/// Whether the form is still loading.
/// </summary>
Expand Down Expand Up @@ -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);

/// <summary>
/// Method auto-called (by event) for when a key is blocked.
/// </summary>
Expand Down Expand Up @@ -272,7 +280,21 @@ public void CheckAutoDisable()
return;
}
bool any = false;
foreach (string proc in Process.GetProcesses().Select(p => p.ProcessName.ToLowerInvariant()))

List<string> processes = new List<string>();

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))
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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";
}
}

/// <summary>
/// If enabled, any close should fully close the form and exit the program.
/// </summary>
Expand Down Expand Up @@ -725,6 +773,19 @@ private void AutoDisableOnFullscreenCheckbox_CheckedChanged(object sender, Event
Program.Blocker.SaveConfig();
}

/// <summary>
/// Event method to handle the 'auto disable on fullscreen' checkbox state changing.
/// </summary>
private void DisableOnFocusCheckbox_CheckedChanged(object sender, EventArgs e)
{
if (Loading)
{
return;
}
Program.Blocker.DisableOnlyIfFocus = DisableOnFocusCheckbox.Checked;
Program.Blocker.SaveConfig();
}

/// <summary>
/// Event method to handle the 'other key reset' checkbox state changing.
/// </summary>
Expand Down
33 changes: 33 additions & 0 deletions KeyboardChatterBlocker/MainBlockerForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@
<metadata name="ChatterConfigure.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Time.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Key.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ChatterDelay.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ChatterConfigure.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="TrayIcon.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>189, 17</value>
</metadata>
Expand Down Expand Up @@ -1280,6 +1292,27 @@
<metadata name="StatsRateColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="StatsKeyColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="StatsCountColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="StatsChatterColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="StatsRateColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ConfigureKeyColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ConfigureThresholdColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="RemoveColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ConfigureKeyColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
Expand Down