Custom hot key programming with acpid
Key In
A little research from the command line and a short script bends your keyboard to your will.
Linux support for laptops has improved by leaps and bounds in the last decade. At this point, if you buy a laptop from any of the major manufacturers and load a Linux distro with a reasonably recent kernel version, things pretty much "just work" – problems with proprietary display drivers aside.
What can sometimes not work, however, are some of the vendor-specific hot keys on the keyboard. For example, I do a lot of presentations, and I'd like the hot key that's supposed to switch between my laptop display and an external monitor (Fn+F7 on my ThinkPad) to work under Linux. In this article, I describe my solution for this problem and offer some pointers for customizing hot key events.
Getting to Know acpid
Most Linux systems support ACPI (Advanced Configuration and Power Interface [1]), a popular standard for device configuration and power management. The ACPI standard specifies a structure for defining and customizing hardware events. In Linux, the acpid daemon [2] listens for ACPI events and maps each event to an action. Hot key events are typically handled by acpid.
The trick to customizing your hot key configuration is getting acpid to divulge exactly which event numbers are occurring when you press a particular button. The easiest way to do this is to put acpid into debugging mode with the -d switch. To do this, you need to kill the current acpid process and restart it, as shown in Listing 1.
Listing 1
Kill acpid and Restart in Debug Mode
Note that I'm being careful to use the same command-line options that were used in the original acpid invocation; then I add the -d flag to capture the debugging output. At this point, quite a bit of output is directed to your terminal as acpid starts up. Once that output settles down, you can go ahead and hit the hot key you're interested in programming. When I hit Fn+F7 on my laptop, I get a report similar to the output shown in Listing 2.
The relevant bit here is the event identifier, "ibm/hotkey HKEY 00000080 00001007", because you will need this string when you are programming custom events into acpid. The acpid daemon stores the different event actions in configuration files that go into the /etc/acpi/events directory. To see whether a configuration file related to your Fn+F7 events already exists, enter the following code:
# grep -l "ibm/hotkey HKEY 00000080 00001007" /etc/acpi/events/* /etc/acpi/events/ibm-videobtn # cat /etc/acpi/events/ibm-videobtn # /etc/acpi/events/ibm-videobtn # This is called when the user presses the video button. It is # currently a placeholder. event=ibm/hotkey HKEY 00000080 00001007 action=/bin/true
As you can see, a configuration file is related to this event, but the final line, action=/bin/true, tells you that pressing this hot key does not switch the monitor. (Note that a close reading of the acpid debugging output In Listing 2 would lead you to the same conclusion.) To get this hot key working, you'll need a little script.
Listing 2
acpid Output for Function Keys
Fun with xrandr
To create a script, you'll need a command-line method for controlling the internal and external displays. Happily, modern Linux distributions come with xrandr (short for "X Rotate AND Resize"), which has all the necessary functionality. For example, to discover the current display settings, enter xrandr -q (Listing 3).
Listing 3
Show Current Display Settings
In Listing 3, VGA is the external monitor and LVDS is the internal laptop video display. For each display, xrandr lists the various screen resolutions supported and marks the currently active screen(s) with an asterisk (*). In the example shown in Listing 3, the external monitor is running at the maximum resolution of 1920x1200, and the laptop display is currently shut off. Additionally, you can use xrandr to change your video settings (Listing 4).
The xrandr command has a lot of other tricks up its sleeve, including the ability to create a single virtual display out of multiple screens, the ability to rotate screens between portrait and landscape mode, and so on. However, the basic commands I have presented here are sufficient for the needs of this simple example.
Listing 4
Changing Video Settings with xrandr
Getting Your Script On
When I press Fn+F7, I want to rotate through three possible video modes corresponding to the xrandr commands in Listing 4. Those modes are:
- both displays on and showing the same screen
- laptop display only
- external monitor only.
Although I could have added additional options, I figured these modes were the basics of what I needed when giving presentations. If I needed to, I could always just run xrandr from the command line for any additional options.
I freely admit I cribbed a lot of good ideas for this script from an article on the ThinkWiki site [3] (which is a hugely useful site, by the way, even if you are not using a ThinkPad). One of the most critical tricks in this particular situation is to remember that acpid is going to be running your script with root privileges, but the display is owned by the user on the console of the machine. For your script to use xrandr effectively, it needs to run all commands through su to get access to the display as the console user.
My sample Perl script is shown in Listing 5. The ThinkWiki article provides alternative Bash and Python scripts. The first block of code is devoted to figuring out which user is currently on the console. Then the script runs xrandr -q and parses out the configuration of the currently active displays.
Finally, a big if block figures out which state the system is currently in and chooses the appropriate xrandr command to move to the next screen mode.
Listing 5
Sample video-switch Script
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
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.