A clever way to automatically detect host OS in QMK based on USB setup pockets – shared by u/kapji.
KBD.news Published August 30, 2022
Creators! Feel free to tip me off about your keyboard related projects to bring them to 150K readers.
Ruslan Sayfutdinov aka KapJI shared his trick to detect host OS in QMK.
I found a way to detect host OS based on USB setup packets in keyboard firmware without need to install any extra tools – kapji.
What is this good for? E.g. you can automatically swap Ctrl and Cmd on Mac and Windows or create platform independent macros for copy-paste. Here is an example by kapji. Some layouts like Hands Down have a manual switch for that, now it can be done automatically.
This method uses the wLength field in get_descriptor packets during the USB setup sequence. Different OSes have different number of such packets and some patterns in these values. The author's guess is that it depends on the implementation of USB stack. For example, Linux always sets it to 0xFFFF.
According to the author, this approach can detect Windows, Linux, MacOS and iOS.
ChromeOS and Android are both identified as Linux (since they're both using the Linux USB stack.)
The focus of Sadek Baroudi's tutorial is how to properly configure RGB Matrix especially for non-trivial ergo keyboards with angles and column stagger.