Skip to content

Commit 4021f82

Browse files
committed
measure_from config setting, for #34
1 parent 2015a2a commit 4021f82

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

KeyboardChatterBlocker/KeyBlocker.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ public class KeyBlocker
4242
/// </summary>
4343
public Dictionary<string, string> Hotkeys = new Dictionary<string, string>();
4444

45+
/// <summary>
46+
/// Enum to represent when to measure the time delay from.
47+
/// </summary>
48+
public enum MeasureFrom
49+
{
50+
Press, Release
51+
}
52+
53+
/// <summary>
54+
/// When to measure the time delay from.
55+
/// </summary>
56+
public MeasureFrom MeasureMode = MeasureFrom.Press;
57+
4558
/// <summary>
4659
/// Load the <see cref="KeyBlocker"/> from config file settings.
4760
/// </summary>
@@ -149,6 +162,9 @@ public void ApplyConfigSetting(string setting)
149162
Hotkeys["disable"] = settingValue;
150163
HotKeys.Register(settingValue, () => Program.MainForm.SetEnabled(false));
151164
break;
165+
case "measure_from":
166+
MeasureMode = (MeasureFrom)Enum.Parse(typeof(MeasureFrom), settingValue, true);
167+
break;
152168
}
153169
}
154170

@@ -175,6 +191,7 @@ public string GetConfigurationString()
175191
result.Append("is_enabled: ").Append(IsEnabled ? "true" : "false").Append("\n");
176192
result.Append("global_chatter: ").Append(GlobalChatterTimeLimit).Append("\n");
177193
result.Append("hide_in_system_tray: ").Append(Program.HideInSystemTray ? "true" : "false").Append("\n");
194+
result.Append($"measure_from: {MeasureMode}\n");
178195
result.Append("\n");
179196
foreach (KeyValuePair<Keys, uint?> chatterTimes in KeysToChatterTime.MainDictionary)
180197
{
@@ -222,6 +239,11 @@ public string GetConfigurationString()
222239
/// </summary>
223240
public AcceleratedKeyMap<ulong> KeysToLastPressTime = new AcceleratedKeyMap<ulong>();
224241

242+
/// <summary>
243+
/// A mapping of keys to the last release time.
244+
/// </summary>
245+
public AcceleratedKeyMap<ulong> KeysToLastReleaseTime = new AcceleratedKeyMap<ulong>();
246+
225247
/// <summary>
226248
/// The global chatter time limit, in milliseconds.
227249
/// </summary>
@@ -287,7 +309,7 @@ public bool AllowKeyDown(Keys key, bool defaultZero)
287309
KeyIsDown[key] = true;
288310
StatsKeyCount[key]++;
289311
ulong timeNow = GetTickCount64();
290-
ulong timeLast = KeysToLastPressTime[key];
312+
ulong timeLast = MeasureMode == MeasureFrom.Release ? KeysToLastReleaseTime[key] : KeysToLastPressTime[key];
291313
if (timeLast > timeNow) // In the future = number handling mixup, just allow it.
292314
{
293315
KeysToLastPressTime[key] = timeNow;
@@ -319,13 +341,16 @@ public bool AllowKeyDown(Keys key, bool defaultZero)
319341
/// <returns>True to allow the key-up, false to deny it.</returns>
320342
public bool AllowKeyUp(Keys key)
321343
{
344+
ulong timeNow = GetTickCount64();
322345
if (!IsEnabled || IsAutoDisabled) // Not enabled = allow everything through.
323346
{
347+
KeysToLastReleaseTime[key] = timeNow;
324348
return true;
325349
}
326350
KeyIsDown[key] = false;
327351
if (!KeysWereDownBlocked[key]) // Down wasn't blocked = allow it.
328352
{
353+
KeysToLastReleaseTime[key] = timeNow;
329354
return true;
330355
}
331356
KeysWereDownBlocked[key] = false;

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ I've taken a similar approach to the software solutions mentioned above, but wit
7272
- `keys.<KEY>`: (for example, `keys.H`) set to the time (in ms) for that specific key's keyboard chatter threshold.
7373
- `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.
7474
- `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).
75-
- `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 using `control` often won't work as it gets reserved by other applications (or Windows itself) often. 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).
75+
- `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).
76+
- `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.
7677

7778
### Other Features
7879

0 commit comments

Comments
 (0)