Macro Keyboard

Ever wanted to add a second Keyboard just for using Macros? I’m going to show you how with Linux.

Right now if you were to plug in a second Keyboard it will do the exact same thing as the main Keyboard. However we can remap the Keys on the second Keyboard.

First we need some info about the second Keyboard. Run lsusb and it will give you a list of connected USB devices. We need to find the Vendor and Product IDs.

lsusb
Bus 002 Device 003: ID 0a5c:5800 Broadcom Corp. BCM5880 Secure Applications Processor
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 005: ID 413c:8187 Dell Computer Corp. DW375 Bluetooth Module
Bus 001 Device 006: ID 046d:c52b Logitech, Inc. Unifying Receiver
Bus 001 Device 004: ID 413c:2513 Dell Computer Corp. internal USB Hub of E-Port Replicator
Bus 001 Device 008: ID 258a:002a  
Bus 001 Device 009: ID 046d:c30b Logitech, Inc. NetPlay Keyboard
Bus 001 Device 003: ID 413c:2513 Dell Computer Corp. internal USB Hub of E-Port Replicator
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

I plugged in a Logitech NetPlay Keyboard so if I look at line 8 I see it. I need to write down 046d:c30b. Those are the Vendor/Product IDs.

Now I need to find the input event the Keyboard is assigned to.

ls /dev/input/by-id
usb-Logitech_NetPlay_Keyboard-event-kbd

This will list everything by the Event ID but it’s a system link to a different directory. I prefer using the real directory for it. So to find what event directory it uses I can issue this command.

ls -l /dev/input/by-id
usb-Logitech_NetPlay_Keyboard-event-kbd -> ../event23

It tells me /dev/input/event23 is the directory. So write that down as well.

Now the fun part. We need the scan codes to the keys we want to use. It’s easier to just get all of the scan codes and when we go to create the configuration file we can just hash out the keys we don’t need so down the road if we add more it will be an easy process.

To get the Scan codes there is a program called evtest. It’s in the apt repo if you don’t have it. To get the codes I issued this command

sudo evtest /dev/input/event23

From there it’s simple, press the key you want and write down the scan code. For a example the ESC (Escape) key.

Event: time 1625489641.058334, type 4 (EV_MSC), code 4 (MSC_SCAN), value 70029
Event: time 1625489641.058334, type 1 (EV_KEY), code 1 (KEY_ESC), value 1

The value we want is the first line far right 70029.

What I did was I printed out this picture and wrote down the key scan codes on the keycaps.

Special Keys such as FN and such I wrote down on the back.

After getting all of the scan codes it’s time to create a file for everything.

sudo nano /etc/udev/hwdb.d/70-keyboard.hwdb

Inside the file we need to declare the Device. and then from there remap the keys.

evdev:input:b*v046DpC30Be*
# ESC
 KEYBOARD_KEY_70029=a

With that example I remapped the ESC (Escape) key to A. One way to add Macros is assign F-Keys to a macro in the Keyboard shortcuts options. Even if the Keyboard doesn’t have say a F-24 it can still be assigned and be used.

Once you have mapped the keyboard and saved the file you can run these two commands to reload the event devices.

sudo systemd-hwdb update
sudo udevadm trigger --verbose --sysname-match="event*"

Say you want to run Programs or Scripts with a press of a single key? This can be done in X11. Install xinput from the apt repo and run xinput list

xinput list
Logitech NetPlay Keyboard               	id=20	[slave  keyboard (3)]

My ID is 20 but it can change so with a single line of code I can have it tell me when the ID changes.

xinput list | grep NetPlay | awk '{print $5}' | sed 's/id=//g'

So the xinput list will be grepped with NetPlay and awk will read the 5th word placement and sed will display the ID number so when the command runs it will display just the ID number.

Remember all of those keys we got key scan codes from? We get to do it again for X11.

xinput text 20

Press a key and get a number. For an example ESC (Escape) is 38.

Now to write a BASH script. Be sure to set the right file permissions after you save it.

nano macros.sh

#!/bin/bash
id="$(xinput list | grep NetPlay | awk '{print $5}' | sed 's/id=//g')"
xinput test $id | while read in ; do
  [[ $in = "key press   38" ]] && /opt/sublime_text/sublime_text
  echo "Sublime Text"
done

In this example I got the ESC (Escape) key to launch Sublime Text. Just change the value in bold to do what you want it to do. You can even mix in xdotool for mouse movement commands if you wanted.

It’s pretty neat on what you can do. Remap a keyboard for Video Games, Create Macros for starting and navigating programs. The possibilities are endless.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s