Perl script manages keyboard shortcuts
Taking Shortcuts
The Autokey utility automates desktop processes by triggering specific actions when the user enters predefined text shortcuts or presses hotkey combinations. Perl helps to manage these helpers.
Going back five years, I fondly remember the Gnome window manager Metacity launching applications at the press of a keyboard shortcut, simply by adding an entry with the gconf-editor
utility [1]. As a heavy terminal user, I probably open about 100 such windows in the course of the day, so it was a great time saver to map the keyboard shortcut Ctrl+Alt+N to a shell script that opened the new terminal with a special font and background color no matter which application the keyboard was currently focused on.
With the Compiz window manager now reigning over the Unity desktop, this capability seems to be a thing of the past, and I'm just fed up of continually modifying my scripts to reflect the changing fashions that desktop frameworks tend to follow.
Middle Man
Instead, in this column, I will be looking to put a man in the middle to handle the interface between the keyboard, the window manager currently en vogue in the Gnome world, and the actions I want to trigger.
The Autokey [2] project, which has just celebrated its second birthday, looks perfect for this. As an added bonus, it not only supports the Gtk+ 3 toolkit but also Qt4; in other words, if I really wanted to – I could theoretically migrate to a KDE desktop sometime in the future! Never say never.
On Ubuntu, the command
sudo apt-get install autokey-gtk
installs the Autokey utility; if you want to launch it automatically at system boot time, add it to Startup Applications in your Ubuntu configuration (Figure 1).
Autokey distinguishes between in-text replacements (phrases) and scripts that trigger actions. It initiates either one after you type a specific text or press a predefined hotkey combination. As a text replacement example, you can create an entry under Phrases to tell Autokey to output your complete postal address when you type, say, myaddr
in a text document in gedit
or the body of an email message in a web application.
Hope That Helps
Figure 2 shows the definition of the shortcut, hth
, which relies on Autokey to produce the phrase "Hope that helps, let me know … ." To trigger the action, the user needs to type hth in an application that receives its keyboard input from the Gnome desktop and then immediately press Return.
Please note: This will not work, for example, in an Xterm terminal; it really does have to be the Gnome terminal. And, beware: Shortcuts that occur frequently in a normal body of text are – naturally – less well suited than obviously artificial combinations, such as hth
or afaik
. In the Set Abbreviations dialog, users can enter short forms in the list and save themselves a whole lot of typing in the future (Figures 3 and 4) by delegating the replacement to Autokey.
Hot Keys
To trigger actions even when the keyboard focus is outside of open text input boxes (e.g., directly on the desktop), you need to define a hotkey – a combination of keys you need to press simultaneously – instead of a textual short form.
Pressing the Set button in the Hotkeys row in the Autokey dialog reveals the form shown in Figure 5. The easiest way to choose a keyboard shortcut that you definitely do not use in any other context, such as Ctrl+Alt+N, is to click the Press to Set button and then press the actual keyboard shortcut while Autokey listens for your input.
The desktop automation tool doesn't just perform text substitutions; Autokey can also initiate arbitrary actions by calling your home-grown Python scripts. To do this, the Autokey UI uses New Script to create a new script, which is stored in the Sample Scripts
folder, instead of the My Phrases
folder used previously for text replacements.
This differentiation between replacements and dynamic scripts is important; if a script is not marked as such, Autokey would just output the Python code instead of executing it. Figure 5 shows an action mapped to the hotkey Ctrl+Alt+N, which opens the custom terminal window I referred to earlier, of which I need dozens every day.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.