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!

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

Leave a Reply

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