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
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4
-
Fedora Unleashes Atomic Desktops
Fedora has combined its solid distribution with rpm-ostree system to make it possible to deliver a new family of Fedora spins, called Fedora Atomic Desktops.
-
Bootloader Vulnerability Affects Nearly All Linux Distributions
The developers of shim have released a version to fix numerous security flaws, including one that could enable remote control execution of malicious code under certain circumstances.