Hotkey keys leak to the OS depending on press and release order
What happens
A hotkey that does not pass to the OS still sends its keys to the OS. What leaks depends on the order the keys are pressed and released. Two examples:
- Config chord (Left Ctrl + Right Shift + C + O), or any chord with a key: release Left Ctrl before the letter. The next report is Shift + letter, and the OS types it. Release the Shift first instead, and the OS gets Ctrl + letter.
- Firmware upgrade chord (Left Shift + Right Shift + A): press A while only one Shift is down. That report sends A to the OS. The next report completes the chord, and the board reboots into BOOTSEL at once. The OS never gets the key-up, so it auto-repeats A until the device goes away. The result is a row of
As in the focused window.
Why
process_keyboard_report in src/keyboard.c swallows only the one report in which the whole chord is down:
if (hotkey != NULL) {
...
hotkey->action_handler(state, &new_report);
if (!hotkey->pass_to_os)
return;
}
send_key(&new_report, state);Every report before or after that one goes to send_key as normal. fw_upgrade_hotkey_handler_A calls reset_usb_boot at once, so no release report can go out first.
A possible fix
We fixed this in a fork and tested it on hardware. The change has two parts:
- After a non-pass-through hotkey matches, hold back the chord's keys until each one is released. Other keys pass as normal. Send the matching report with the chord's keys removed before running the action. That report is the key-up for any chord key the OS already saw.
- Before an action that reboots (firmware upgrade, config mode), send an all-keys-up report and give it time to leave. A short delay (50 ms worked for us) before
reset_usb_bootlets the queued report reach the host. The receiving board also needs the delay when the chord asks the other board to reboot.
A key pressed before the chord completes still reaches the OS for a moment. For example, with Ctrl and C down before Shift, the OS sees Ctrl + C. The fix above only makes sure it is released straight away.
Happy to open a PR if you want one. Please say which shape you prefer.
Source: hrvach/deskhop