Skip to content

Commit

Permalink
special temporary all-input-block hotkey option
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Feb 27, 2023
1 parent 4021f82 commit 14e26a7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
4 changes: 2 additions & 2 deletions KeyboardChatterBlocker/AcceleratedKeyMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace KeyboardChatterBlocker
{
/// <summary>
/// Represents essentially a Dictionary[Key, T] with a special case for low range keys.
/// Represents essentially a Dictionary[<see cref="Keys"/>, <typeparamref name="T"/>] with a special case for low range keys.
/// </summary>
/// <typeparam name="T">The type of value in the Dictionary.</typeparam>
public class AcceleratedKeyMap<T>
Expand All @@ -24,7 +24,7 @@ public class AcceleratedKeyMap<T>
public Dictionary<Keys, T> MainDictionary = new Dictionary<Keys, T>(1024);

/// <summary>
/// Get or set a key-to-T mapping.
/// Get or set a key-to-<typeparamref name="T"/> mapping.
/// </summary>
/// <param name="key">The key index.</param>
public T this[Keys key]
Expand Down
25 changes: 25 additions & 0 deletions KeyboardChatterBlocker/KeyBlocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ public void ApplyConfigSetting(string setting)
case "measure_from":
MeasureMode = (MeasureFrom)Enum.Parse(typeof(MeasureFrom), settingValue, true);
break;
case "hotkey_tempblock":
BlockAllInputsKeySet = settingValue.Split('+').Select(s => s.Trim().ToLowerInvariant()).Where(s => !string.IsNullOrWhiteSpace(s)).Select(s => (Keys)Enum.Parse(typeof(Keys), s, true)).ToArray();
break;
}
}

Expand Down Expand Up @@ -211,9 +214,23 @@ public string GetConfigurationString()
{
result.Append($"hotkey_{pair.Key}: {pair.Value}\n");
}
if (BlockAllInputsKeySet != null)
{
result.Append($"hotkey_tempblock: {string.Join(" + ", BlockAllInputsKeySet)}\n");
}
return result.ToString();
}

/// <summary>
/// A special set of keys that when pressed together will block all other key or mouse inputs until released.
/// </summary>
public Keys[] BlockAllInputsKeySet = null;

/// <summary>
/// Whether ALL key inputs should be temporarily blocked.
/// </summary>
public bool ShouldBlockAll => BlockAllInputsKeySet != null && BlockAllInputsKeySet.Length > 0 && BlockAllInputsKeySet.All(k => KeyIsDown[k]);

/// <summary>
/// Event for when a key is blocked.
/// </summary>
Expand Down Expand Up @@ -310,6 +327,10 @@ public bool AllowKeyDown(Keys key, bool defaultZero)
StatsKeyCount[key]++;
ulong timeNow = GetTickCount64();
ulong timeLast = MeasureMode == MeasureFrom.Release ? KeysToLastReleaseTime[key] : KeysToLastPressTime[key];
if (ShouldBlockAll)
{
return false;
}
if (timeLast > timeNow) // In the future = number handling mixup, just allow it.
{
KeysToLastPressTime[key] = timeNow;
Expand Down Expand Up @@ -348,6 +369,10 @@ public bool AllowKeyUp(Keys key)
return true;
}
KeyIsDown[key] = false;
if (ShouldBlockAll)
{
return false;
}
if (!KeysWereDownBlocked[key]) // Down wasn't blocked = allow it.
{
KeysToLastReleaseTime[key] = timeNow;
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ I've taken a similar approach to the software solutions mentioned above, but wit
- `keys.<KEY>`: (for example, `keys.H`) set to the time (in ms) for that specific key's keyboard chatter threshold.
- `auto_disable_programs`: Set to a list of executable names (case insensitive, without the `.exe`) separated by slashes (like `some_video_game/other_game`) that will cause the chatter block to automatically disable whenever that program is open.
- `auto_disable_on_fullscreen`: set to `true` to auto-disable the chatter block when any application is open in fullscreen - see also [#26](https://github.com/mcmonkeyprojects/KeyboardChatterBlocker/issues/26).
- `hotkey_<type>`: (where type is `toggle`, `enable`, `disable`) set to a key combo. Must be a combination of any of `control`, `alt`, `win`, `shift`, and any valid key separated by `+`. For example `control + a`, `win + alt + b`, `shift + d1`. Note that hotkeys including `control` often won't work as they often get reserved by other applications (or Windows itself). This line will register a global hotkey that performs the applicable action (eg `hotkey_disable: win + shift + pause` will allow you to hold those three keys together at any time to immediately disable the chatter blocker).
- `hotkey_toggle`, `hotkey_enable`, `hotkey_disable`: set to a key combo. Must be a combination of any of `control`, `alt`, `win`, `shift`, and any valid key separated by `+`. For example `control + a`, `win + alt + b`, `shift + d1`. Note that hotkeys including `control` often won't work as they often get reserved by other applications (or Windows itself). This line will register a global hotkey that performs the applicable action (eg `hotkey_disable: win + shift + pause` will allow you to hold those three keys together at any time to immediately disable the chatter blocker).
- `hotkey_tempblock`: set to a key combo (any list of keys separated by `+`). Refer to [Microsoft Keys Enum](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.keys?view=netframework-4.7.2) for a list of valid key IDs. Note that you need to use precise key IDs, such as `LShiftKey` (for left shift) rather than generic ones like `Shift`. If you have `hotkey_tempblock` set, any time hold this combination down, all key inputs (down or up) will be discarded. If you have mouse buttons listed as blockable, mouse inputs will be blocked too. For example, `hotkey_tempblock: pause` will allow you to hold the Pause key at any time to pause all input recognition. This is useful for example to block press a key down, and then hold your Pause input to block the release, allowing an app to see it as still being held down (for example: press down on Shift, press down on Pause, release Shift, release Pause, then type - all your input text will then be capitalized).
- `measure_from` set to `Press` or `Release` (defaults to `Press`) to choose when to measure chatter delay from - the last key press, or last key release.

### Other Features
Expand Down

0 comments on commit 14e26a7

Please sign in to comment.