Perl script manages keyboard shortcuts
Taking Shortcuts
![](/var/linux_magazin/storage/images/issues/2015/173/perl-autokey-automation/schilli_michael_2.png/647161-1-eng-US/Schilli_Michael_2.png_medium.png)
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).
![](/var/linux_magazin/storage/images/issues/2015/173/perl-autokey-automation/figure-1/647164-1-eng-US/Figure-1_large.png)
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.
![](/var/linux_magazin/storage/images/issues/2015/173/perl-autokey-automation/figure-2/647167-1-eng-US/Figure-2_large.png)
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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.
-
SUSE Offers CentOS 7 Support with Liberty Linux Lite
SUSE's Liberty Linux support offering now includes CentOS 7, which means businesses won't be forced to migrate those servers for some time.
-
Ubuntu's App Center Finally Supports Local Installs Again
If you regularly download .deb files and would prefer a GUI method of installing, Ubuntu has your back.
-
AlmaLinux Now Supports Raspberry Pi 5
If you're looking to create with the Raspberry Pi 5 and want to use AlmaLinux as your OS, you're in luck because it's now possible.