Keyboard Builders' Digest
10% off ergo caps at 3dkeycaps! Code: KBDNEWS
Keyboard Builders' Digest / Projects

How to detect host OS in QMK

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
Supported by readers like Raphael S., Micah Alpern, and Jacob Mikesell. Join them now to keep kbd.news free.

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.

The idea is coming from an Arduino project called FingerprintUSBHost.

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.)

Do you like this post? Share, donate, subscribe, tip me off!

Published on Tue 30th Aug 2022. Featured in KBD #92 (source).


Tags: QMKtipOS

Related

Quantum Painter tutorial

How to add Quantum Painter/color LCD to (almost) any QMK keyboard – a tutorial by Esse Woods aka OakNinja .

Unicode in QMK

Dealing with Unicode in QMK revealed by mikefettis.

QMK debounce masterclass

QMK contributor u/x34l explains different debounce algorithms and shares some of his measurements.

Caps Word and other QMK tips

A QMK keymap with short blog posts by Mental_General_5445 explaining tricks like autocorrection, Caps Word, getting Shift keys right, etc.

QMK's sound features

Help with QMK startup sounds by psychephylax.

×
top