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
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
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.