Close

Program Context Driven Shortcut Overlays

A project log for PolyKybd (Displays In Your Keycaps)

Freedom at your fingertip.

thpollthpoll 05/24/2024 at 16:530 Comments

For a long time, I have been talking about this topic, finally, it's here and I love it!

The missing piece was the code that loads overlays depending on the active window. The mapping is stored in a yaml file that looks like this:

Gimp:
  overlay: overlays/gimp_template.png
  app: gimp-2.*
Inkscape:
  overlay: overlays/inkscape_template.mods.png
  app: inkscape.*
  title: .*Inkscape
KiCad PcbNew:
  overlay: overlays/kicad_pcb_template.png
  app: kicad.*
  title: PCB Editor

The fields `app` and `title` are regular expressions (Python). If these match, the `overlay` will be loaded (which can be a list of entries a la [a, b, c]). At least one of the two has to be present. The first entry that matches `app` and `title` will be displayed. The name/key of the block is just used for debugging.

The overlay can be png, gif, jpeg, bmp or whatever Qt allows to load (currently it needs an alpha channel all the time, but this restriction should go away, however, I recommend png as it has lossless compression and you can always have an alpha channel).

The white pixels will be read and turned on. Everything else is ignored. If the name contains ".mods." like "overlays/inkscape_template.mods.png" then the code will split the RGBA channels to the following scheme:

R: shortcuts to display with CTRL
G: shortcuts to Display with SHIFT
B: shortcuts to display with ALT
A: shortcuts to display when no modifier is pressed

In case the name contains ".combo.mods." then the shortcuts will be used for the following modifier combinations:

R: shortcuts to display with CTRL + SHIFT
G: shortcuts to Display with CTRL + ALT
B: shortcuts to display with ALT + SHIFT
A: shortcuts to display with GUI key (not yet, hopefull y soon)

For now, I decided to not support CTRL + ALT + SHIFT as it is rather rare, but you can try to convince me (pls. write a comment).

Save the pixel color values even if the alpha channel makes it transparent and set the format to 8-bit RGBA. If you export a png in Gimp there is a dialog to do so (always set "Save color values from transparent pixels"):

(I first used the Gimp command `decompose` to split up to RGBA channels and before saving the command `compose` can be used again to make a single image out of it - make sure that there are no other channels present, otherwise compose will fail). You can take a look at my gimp files in the "overlays" folder as a reference.

What's now missing, are more overlay files with all the icons needed. I now have the following:

Would be great to have an automatic way for the icon collection...

One More Word On Platform Support

While my current Python library `PyWinCtl` (to make program context shortcuts possible) supports active window reporting for Windows, MacOSX and XDesktop, it lacks an implementation for Wayland.

Today, I came across this sample https://github.com/luisbocanegra/plasma-cursor-eyes that uses a KWin script to report the cursor position to Python, changing that to report the active window should work in the same way, so I'm looking forward to that!

Enjoy your weekend,

Thomas

Discussions