Two of the three shortcuts the Snipping Tool owns cannot be claimed with
RegisterHotKey. Windows already holds them, so the only way to answer those keys
is a WH_KEYBOARD_LL hook, which sees every keystroke on the machine and can
swallow the ones you own.
The report was that it worked after launch and stopped later. Restarting fixed it. That shape usually means a leak or a stale handle, and it is a miserable thing to chase because you cannot ask the system how it is doing.
There is no API for “is my hook alive”
Windows removes a low-level hook whose callback overruns
LowLevelHooksTimeout. It does not raise an error, it does not call you again, and
there is no function to query whether your hook is still installed. Your
handle stays valid-looking forever.
So from inside the process, “Windows ate the key” and “the key never reached us” are indistinguishable, and they have opposite fixes. That ambiguity was the real problem, not the hook.
Making it observable
The fix to the ambiguity is one log line. The hook now stamps every key-down it sees, whether or not it owns that combination. If a shortcut fails and there is no stamp, the callback never ran. If there is a stamp and nothing happened, the dispatch is at fault.
That line turned an unfalsifiable complaint into a measurement, and the first log it produced was worse than expected. Of eight presses, five recorded no key-down at all, in stretches of seven to nine seconds, clustered around starting and stopping a recording.
Not dead. Skipped, and recovering on its own.
Why it was skipped
A low-level hook callback is delivered through the message queue of the thread that installed it, and Windows waits only so long before giving up on it. At ordinary priority that thread was competing with a 60 fps 1080p encode, and losing.
The pump runs at TIME_CRITICAL now. That is a heavy thing to reach for, and it
is only defensible because the callback is tiny and bounded: relaxed atomics, a
try_lock that is allowed to fail, and a channel send. Anything that allocates,
blocks or logs on that path turns this fix into a way to starve the machine.
The instrument had its own bug
The first version of that log line let one key-down explain more than one firing. A press the hook had missed would borrow the previous press’s timestamp and, if recent, read as a real measurement. A bogus 580 ms reading sat between two honest 0 ms ones in the very first log. Each stamp is now consumed once.
It also settled an argument I had lost twice by reasoning. Moving a shortcut off the hook
and onto RegisterHotKey reportedly felt slower. A registered hotkey arrives as a
posted message carrying no hint of how long it spent in transit, so the claim was not
checkable. Because the hook stamps every key regardless of ownership, and registered hotkeys
are dispatched after the hook chain, its stamp is the earliest observable moment of a press.
Measured: 0 ms, 0 ms, 1 ms. It cost nothing.
Since there is no health check, re-install forever
You cannot ask whether the hook is alive, so the only robust answer is to keep replacing it. It re-installs every five seconds.
- New first, old second. Unhooking before installing leaves a window in which a keystroke reaches Windows instead of the app, which is the exact failure being fixed. Two live hooks cannot double-fire, because the chain stops at whichever runs first.
- The pump must never sleep. A poll-and-sleep loop adds the sleep to
every keystroke and walks straight into the same timeout. It is driven by
SetTimerand a blockingGetMessageW, neverPeekMessageplus a sleep. My first draft of this fix used the sleep and would have caused the bug it was written to cure. - Five seconds, not thirty. Since nothing reports a dead hook, the refresh interval is the recovery time, and thirty seconds of a dead hook is thirty seconds of a shortcut doing nothing.
The general shape
The bug was not hard. Finding it was, because the system was allowed to fail without saying so, and I had no way to distinguish between two opposite causes.
When a platform can fail without telling you, the first fix is not the fix. It is the log line that makes the failure visible.
Everything useful here came after that line existed: the five missing presses, the seven second stretches, the 0 ms measurement that ended an argument, and the bogus reading that showed the instrument itself needed correcting.