Go back

Using a secondary keyboard for macros

Date published: 2022-07-21 13:19:41 — Categories: Tech,Linux

Macros are everything. Heck, even using my tiling window manager requires them. Currently, most people use a combination of keys to get their work done, for example, you want to open a browser so you use Super+B. If you read the title you probably know where I am going with this. You can use a secondary keyboard to not only automate more, but have those quick shortcuts on a single keypress. This will increases productivity if done well enough.

First we need a secondary keyboard. You may have one laying around at home, but you can buy one for cheap as well from places like Amazon or Ebay. Once connected, we need to figure out what device we want to use as a keyboard. We can see a list of devices in Linux using the /proc/bus/input/devices file. So use less to find your device and note down it's event number under the "Handlers" field.

Next we want to tell X we don't want to use that keyboard for anything anymore. For this we use xinput list which will give us a list of keyboards available. We then use xinput --disable <id> to disable it.

Now that you have your keyboard's event id, we can proceed on using actkbd to figure out which keys we want to use by their scan codes. Use actkbd -s -d /dev/input/event<id> exactly for this purpose. Once you've noted down what keys do what, you can now create a configuration file.

The configuration file that actkbd uses is very simple. It uses "#" for comments and entries are defined in a bit of a special format on each line. The format is relatively simple: <key(s)>:<event type>:<attributes>:<command>. We are only interested in the keys field and event type. The keys field consists of a list of keys simultaneously used divided by a "+" sign. For example, if you want to represent Super+R you can use 19+125 where 19 is 'r' and 125 is Super. The event type is a string field separated by either commas or white-spaces, the available values are "key" (key press event, default), "rep" (key repeat event), "rel" (key release event). For example, if you want to pause media when you press the '0' key, you can use 11:key::playerctl play-pause in your configuration file. Please note that the command executed is blocking, you can overcome this issue by appending an "&" sign at the end of the command.

Now that we have a configuration file ready to go, we can start actkbd like so: sudo actkbd -c <path to configuration file> -d <same as before>. You can, of course, figure out each time you turn on your computer what your device's event number is but that will get annoying pretty quick. What we can do to solve this issue is write a quick python script that looks for your keyboard in /proc/bus/input/devices and automatically figures out this stuff for us. I have created one that can be found here so you can use that, but I want to explain real quick what it does. First of all, it opens the devices file, looks for the first result which matches a name in variable "whitelist". After that, it goes ahead and parses a bit more to find the event ID, disables xinput from using that keyboard and starts actkbd.

Run the Python script at X startup and you have yourself a working second keyboard ready to use for your macro-ing needs!