It's been randomly failing on Wine and on some VMs (only some fvwm / mutter) [1], so I'm assuming this is caused by a focus-related Wine or WM bug. This also only affects older dinput versions, which are using LL-hooks for keyboard input.
[1]: https://test.winehq.org/data/patterns.html#dinput:device8