Bug: wintercept always sends processed keys to keyboard device 1, so mapped keys are lost in an RDP session
Requirements
- I've searched platform-specific issues, issues and discussions to see if this has been reported before.
- My issue does not involve multiple simultaneous key presses.
Describe the bug
With the Interception variant running on a Windows host, when I connect to that host over Remote Desktop, every key that is in defsrc produces no output inside the RDP session. Keys that are not in defsrc type normally. With my config the only letters in defsrc are h j k l, so the visible symptom is "h/j/k/l are dead over RDP". It happens regardless of which client machine I connect from.
The LLHOOK variant (kanata.exe) with the same config works correctly in the same RDP session.
The cause is the asymmetry between the two output paths in the wintercept build:
- Unmapped keys are passed back to the device they came from:
https://github.com/jtroo/kanata/blob/3aa9fa535ead451d5fb04c6e8dbd48532250c3ec/src/kanata/windows/interception.rs#L108-L111
if !MAPPED_KEYS.lock().contains(&key_event.code) { log::debug!("{key_event:?} is not mapped"); intrcptn.send(dev, &strokes[i..i + 1]); - Keys produced by kanata's processing loop are always sent to keyboard device
1: https://github.com/jtroo/kanata/blob/3aa9fa535ead451d5fb04c6e8dbd48532250c3ec/src/oskbd/windows/interception.rs#L164-L166Stroke::Keyboard { .. } => { ic.send(1, &strokes[0..1]); }
In an RDP session, input arrives through the Remote Desktop virtual keyboard (hardware ID TS_INPT\TS_KBD). It is a regular keyboard-class device, so the Interception filter driver attaches to it; on my machine it is device 8. Unmapped strokes are sent back to device 8 and reach the RDP session. Mapped strokes are re-emitted on device 1, a physical keyboard attached to the console, so they never reach the RDP session.
Relevant kanata config
(defcfg)
(defsrc
caps grv
h j k l
lsft rsft
)
(deflayer default
@cap _
_ _ _ _
_ _
)
(deflayer arrows
_ _
left down up rght
_ _
)
(defalias
cap (tap-hold-press 200 200 esc (layer-toggle arrows))
)To Reproduce
- On a Windows 11 host with the Interception driver installed, run
kanata_wintercept.exe(or the gui variant) with the config above. - From another machine, connect to the host with Remote Desktop.
- In the RDP session, open Notepad and type
ahjkl. - Only
aappears.h j k lproduce nothing. - Stop kanata, run
kanata.exe(LLHOOK) with the same config, repeat step 3:ahjklappears as expected.
Expected behavior
Keys output by kanata should reach the same device/session the input came from, so mapped keys behave the same as unmapped ones over RDP.
Kanata version
1.8.1 (kanata_wintercept_cmd_allowed.exe from the release). The relevant code is unchanged on main as of 3aa9fa5.
Debug logs
kanata_wintercept_cmd_allowed.exe --debug, typing in the RDP session. I added a dummy windows-interception-keyboard-hwids-exclude ("1") only so that kanata logs the device number and hwid; nothing is excluded. Unmapped a/g go back to their device, mapped h/j are sent "to driver" (device 1) and never show up in the session:
[INFO] exclude check - res 32; device #8 is intercepted: true; hwid [84, 0, 83, 0, 95, 0, 73, 0, 78, 0, 80, 0, 84, 0, 92, 0, 84, 0, 83, 0, 95, 0, 75, 0, 66, 0, 68, 0, 0, 0, ...]
[DEBUG] ...interception: got stroke Keyboard { code: A, state: DOWN, information: 0 }
[DEBUG] ...interception: KeyEvent { code: KEY_A (65), value: Press } is not mapped
[DEBUG] ...interception: got stroke Keyboard { code: H, state: DOWN, information: 0 }
[DEBUG] ...interception: sending KeyEvent { code: KEY_H (72), value: Press } to processing loop
[DEBUG] ...kanata: key press H
[DEBUG] ...oskbd::windows::interception: kanata sending Keyboard { code: H, state: DOWN, information: 0 } to driver
[DEBUG] ...interception: got stroke Keyboard { code: G, state: DOWN, information: 0 }
[DEBUG] ...interception: KeyEvent { code: KEY_G (71), value: Press } is not mapped
[DEBUG] ...interception: got stroke Keyboard { code: J, state: DOWN, information: 0 }
[DEBUG] ...interception: sending KeyEvent { code: KEY_J (74), value: Press } to processing loop
[DEBUG] ...oskbd::windows::interception: kanata sending Keyboard { code: J, state: DOWN, information: 0 } to driverThe byte array is TS_INPT\TS_KBD in UTF-16LE.
Operating system and I/O mechanism
Windows 11 host, Interception driver. RDP clients: Windows (mstsc) and macOS (Windows App).
Additional context
Workaround (verified): exclude the RDP virtual keyboard so kanata doesn't intercept it:
(defcfg
windows-interception-keyboard-hwids-exclude (
;; TS_INPT\TS_KBD (UTF-16LE)
"84, 0, 83, 0, 95, 0, 73, 0, 78, 0, 80, 0, 84, 0, 92, 0, 84, 0, 83, 0, 95, 0, 75, 0, 66, 0, 68, 0"
)
)With this, the log shows device #8 is intercepted: false and every stroke, including h j k l, is passed through (is from undesired device), and ahjkl types normally in the RDP session. For remote sessions this is arguably the right behaviour anyway, since the client side usually does its own remapping. It doesn't help when the client has no remapper, though. It might be worth mentioning this hwid in the docs for windows-interception-keyboard-hwids-exclude.
Possible fix: remember the device of the most recently intercepted keyboard stroke in the input loop (e.g. an AtomicI32 set next to tx.try_send(key_event)), and have write_interception send keyboard strokes to that device, falling back to 1 if none has been seen. The same applies to mouse output and device 11. The open design question is which device to use for output that isn't tied to one input (macros, timers, tap-hold resolved later), but "last input device" seems a reasonable default and matches today's behaviour when only one keyboard is used.
This may also be related to #767 (wintercept stops sending anything after a device is disconnected/reconnected), if device 1 is the one that went away, but I haven't verified that.
I'm happy to open a PR for this approach if it sounds acceptable.
Source: jtroo/kanata