Perl script manages keyboard shortcuts

Taking Shortcuts

Article from Issue 173/2015
Author(s):

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).

Figure 1: To launch Autokey automatically, you just need to add the utility to Startup Applications in Ubuntu.

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.

Figure 2: Sample text substitution with Autokey: The shortcut hth expands to a longer phrase. This can save a whole lot of typing.

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.

Figure 3: In a webmail provider's email application, the short hth …
Figure 4: … expands to a polite phrase at the end of a message.

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.

Figure 5: You can configure hotkeys in this Autokey dialog.

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

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
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

News