Plasma secrets: custom shortcuts and actions

Updated: March 7, 2022

After I wrote my review of Ksnip, a handy screenshot tool, someone commented that they would like to be able to associate this program with their shortcut for capturing screenshots. In particular, they also said they would like to be able to take rectangular areas rather than fullscreen or windowed area. This got me thinking, and I figured, perhaps there is some tutorial material here?

The answer is, yes. Now, my mission today is twofold. One, show you how you can assign custom shortcuts to custom actions in Plasma. Two, showcase yet again the amazing and neverending flexibility of the Plasma desktop environment, because nothing else does it half as good as Plasma. In fact, the whole thing reminds me of that catchy, cheesy song The Spy Who Loved Me, the theme sequence from the namesake 007 Bond movie. And it opens with the following lyrics: Nobody does it better, I feel sad for the rest ... Indeed, let is commence this tutorial.

Custom everything

Open the Plasma menu and then type Custom shortcuts. This will open a menu where you can review the existing "custom" shortcuts, with their triggers and actions, and then, optionally add some more. You also should remember that there are two other shortcut categories in the Plasma settings, which can be a little confusing, but somewhat beyond the scope of what we're doing here.

Main

Expand the existing Screenshots section. Click on the Rectangular Region (or any entry) to see how it's defined in the right pane. The Trigger tab merely links to the shortcut in question. The action tab is the interesting piece. Here, the rectangular region screenshot is done using Spectacle, via a D-Bus call. This can be a baseline for what we may want to accomplish with Ksnip, or any other program, for that matter.

Region, action

Add new application & command

Highlight the Screenshot section, and then click on the Edit button to expand it. Here, you will have a wealth of options available. You can delete existing shortcuts or shortcut groups, export or import data, which can be useful if you already have a useful shortcut set from another system, or you perhaps want to create a backup before a major change.

Add new

You can also create a new group, or new actions. There are three categories available. You can define mouse and window actions, or create shortcuts, which again, Monty Python style, come in three flavors. You can define a D-Bus command (like the Spectacle one), a specific keyboard input, or a custom action. Now, let's assume we want to add a D-Bus command to our application. The first (naive) question you want to ask is, does your program actually support this protocol, and is it registered with the system? You can check this with the qdbus command:

qdbus

...
:1.46
 org.kde.kiod5
 org.kde.kioexecd
 org.kde.kpasswdserver
 org.kde.kssld5
:1.51
:1.52
:1.53
:1.6
 org.kde.klauncher5
:1.7
 org.kde.StatusNotifierWatcher
 org.kde.kappmenu
 org.kde.kcookiejar5
 org.kde.kded5
 org.kde.keyboard
 org.kde.plasmanetworkmanagement
 org.kubuntu.NotificationHelper
 org.kubuntu.restrictedInstall
org.freedesktop.DBus

TL;DR: If you do not see your program in the list, then the answer is no. But then, even if you do see it, you may not be comfortable with this approach, as you may not know the exact function that you ought to send to your program via D-Bus. Instead, you can specify a custom command (or URL).

Here, I decided to make use of whatever Ksnip can do. Indeed, if you look at its command-line options, it has the ability to grab rectangular regions (-r), the last rectangular region, set delay, set the filename, and then some. In the simplest terms, you can do something like this:

Ksnip added

Set the trigger, and that's it. Now, if you activate the desired shortcut, this command will launch your application, plus any custom parameters that it supports. In this case, Ksnip can do rectangular areas, but the functionality can be something far more complex. It really depends on the program. And there you go. Now you can start adding custom shortcuts to your programs, however you please, limited only by what is technologically feasible rather than by what Plasma can do.

Conclusion

Plasma's shortcut functionality is one of the more overlooked aspects of this fine desktop environment. And it is a bit clunky, because there's so much covered under this generic term. Perhaps the best thing the KDE dev team could do is to simplify the system module, separate actions from keyboard shortcuts entirely, or use a more intuitive naming convention.

Until then, hopefully, this little guide will help you get through, and even gain some extra productivity. Then, when you combine all the different power tools in Plasma, like Krunner and custom shortcuts and desktop activities, you really start to enjoy the desktop, on a whole new level. Anyway, there you go. Another secret unraveled. Ping me with any requests, and see you soon.

Cheers.

You may also like: