9 Oct

The Logitech G213 and Linux

The “Logitech G213 Prodigy Gaming Keyboard” is a pretty good rubber dome keyboard with the usual multimedia keys plus the gaming button which turns of some annoying keys while gaming. It has no unneeded nick-nack, the illumination is bright (enough) and the keys have a good feel while typing.

The problem comes with the illuminated keys, the standard color effect used by the keyboard is a color cycle animation which is terribly distracting while typing. What i planned when i bought this keyboard was to set the illumination under Windows and then use the keyboard under Linux, but since Logitech considers the G213 an “entry level” keyboard (Even though it is pretty expensive) it cannot save the set illumination as a profile inside the keyboard like the G9 mouse or highest priced G keyboards for instance can.
This means you have to run the Gaming Software under Windows all the time for the keyboard to behave as expected.

I had now two possible ways to go, either send the G213 back or get it to at least set the color correctly under Linux.

There are a couple of old projects doing that for older keyboards and other Logitech equipment under Linux, but not for the G213. It seems that with every new keyboard at least parts of the commands used change so you cannot use the old projects (Like Gnome15, G19Linux and others based on that code) with any equipment that is not specifically included in the projects. Another problem for me is that these projects seem to be mostly abandoned (even though the software still should work) and they are either written in C++ or are too complicated for me to adapt them to my needs.

You can see where this is heading… i started to write my own script to be able to set the colors on my keyboard, called “G213Colors”.

As a starting point i used USBPcap to capture the raw USB data sent to the keyboard from the Logitech Gaming Software and opened the resulting files with Wireshark.
It took me a long time to understand that multiple lines in Wireshark belong to the same data package since i did not find a nice introduction into the Wireshark interface, but in the end i found a 20-byte data string sent to the keyboard that was fortunately not encrypted, obfuscated or contained any incrementing numbers or something. This string contained the color i was setting in the expected format (RGB) which was very good news to me since now i only had to test-send this string to my keyboard to see if it would be working.

Since i wanted to send raw data over USB i could not use Bash scripts, it had to be a real programming (scripting) language, so i decided to use Python since it has a large array of plugins for all kinds of needs and is easy to learn if you already know another language and is preinstalled on most Linux distributions.

In Python there is PyUSB to achieve what i wanted even though i have to say PyUSB is a bit too low-level for my taste but there is no alternative for it.

Under Linux there is usually a kernel driver attached to the keyboard’s USB interface(s) as soon as you plug it in (Which makes sense since the kernel is handling all keyboards) which prevents any data from being sent to the keyboard, so you have to detach (and re-attach later) this driver with PyUSB.

After a lot of trial and error due to unread of confusing documentation i managed to send the command and my keyboard changed from that annoying color changing cycle to a nice white color. Success!

The command structure is rather simple, no matter if you just want to set one color or want to activate a color effect, it is always a 20-byte string containing a 3-byte RGB color value or sometimes also a 2-byte integer which controls the speed. This example sets the color Purple (Hex color value ff00ff) with the command 11ff0c3a0001ff00ff0200000000000000000000 (In a hex representation), notice the color value at byte position 7.

What i could not determine is why the Gaming Software is always sending the string 11ff0c0a00000000000000000000000000000000 in advance of changing a color, in my tests it made no difference at all if i also send it or not, when someone has an idea please leave a comment.

Since i found no software at all able to control the G213 keyboard i decided to publish my script under MIT license on GitHub, where you will find instruction on how to “install” and use this script:

Download G213Colors on GitHub

It can then be called from cron and other parts of Linux as well as being linked from the menu or integrated into your own scripts.

I hope it is as useful to you as it is to me, have fun!

11 thoughts on “The Logitech G213 and Linux

  1. How difficult would it be to convert this to a windows batch (or Powershell?) script to run at startup? All I want is to use the G213 keyboard with one solid back-light color, and get rid of the bloated Logitech Gaming Software (which causes the Surface Pro to think a keyboard and a mouse are connected even when they are not).

    • Hi,
      it shouldn’t be too hard, you would have to install python and PyUSB on windows, also you would need to remove lines 59 to 62 and 67 to 69 since there are no kernel drivers on windows.
      that should be it…

      • Ideally I would like a batch file (or executable) that runs native on Windows and does not require installing any other support library that is not built into Windows 7 and newer. If you would do this and leave the finished product and source code in the public domain, I will send you a $200 contribution (e.g. two hundred US dollars via PayPal) once it is done and working reliably. If you are not interested, I would open the same offer to anyone interested–limited to the first individual with success as described but not past a deadline of May 1st 2017.

  2. You’re a life saver! (well, or at least an “eye saver” lol)

    I will attempt the same packet sniffing on my G403 mouse, inspired by your script. Though admittedly pulsating lights on a mouse is significantly less annoying than the keyboard.

    • you’re welcome ^^

      about packet sniffing: the most difficult part was to read the structure of the usb data in Wireshark, i found the representation extremely confusing.

  3. Hey thanks for posting this. I bought one of these keyboards the other day and I assumed I could set it in windows and it would remember its colour settings.
    I’m using FreeBSD and I am able to use the native usbconfig utility with the following parameters (in case any other FreeBSD users find this):
    usbconfig -u N -a M do_request 0x21 0x09 0x0211 0x0001 0x14 0x11 0xff 0x0c 0x3a 0x00 0x01 0xRR 0xGG 0xBB 0x02 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
    (substituting N and M for the device and, RR, GG and BB for the colour as appropriate)

    Basically the same as device.ctrl_transfer in your sendData method with an extra parameter stating the length of the data following (0x14).
    I may even try my hand at a C utility just to learn a bit more about libusb.

    • hi Ian, no, the keyboard itself doesn’t save anything… i was as surprised as you, since my G9 for instance remembers all settings.
      Your solution looks nice! And if you have new stuff you can learn always go for it!

  4. Hi,

    thank you very much for this software, it works great for my G213.
    However, the keyboard multimedia keys seem to not be producing any key codes for me,
    did you by chance get these to work? (Volume and playback control)

    • they work fine on my side, maybe try to unplug the keyboard, maybe something got tangled up and the driver was not correctly reattached. if it happens again let me know.

Leave a Reply

Your email address will not be published. Required fields are marked *