Global Shortcut Keys for Windows and Ubuntu 20.04 @ FreelimbO | Saturday, Aug 14, 2021 | 15 minutes read | Update at Saturday, Aug 14, 2021

Software: AutoHotkey, xev, xbindkeys, xmodmap, Autokey, and xkb.

To master divine skills, one must first castrate oneself.After the previous post where I voluntarily disabled my own abilities—severing my meridians to lose the top 12 function keys, the six-pack keys, the arrow keys, and the numeric keypad—how on earth do I survive using a computer in the 21st century? The answer is simple: brutal keyboard shortcuts.Think back to when we first learned Ctrl+C and Ctrl+V—not only did it make copying writing homework lightning-fast, but it also opened the door to a whole new world.

First, let’s think about what losing 40% of the keyboard keys actually means. The top row of 12 function keys isn’t used very often in daily life; even if certain apps require them, it’s rare that all 12 need to be used simultaneously. The “six-pack” keys—Insert, Delete, Home, End, Page Up, Page Down—Hmm, they all seem pretty useful, but we can use the Backspace key on the main keyboard to handle the Delete function. The difference is that you can’t delete characters in place; you have to move the cursor past the part you want to delete and then go back to delete it. I can do it, but it depends on whether you’re willing to take that detour. The arrow keys are just for moving the cursor.Numeric Keypad: It’s not just for numbers—it also includes directional keys and mathematical symbols. However, aside from the directional keys, all these functions are already available on the main keyboard; the numeric keypad simply rearranges these keys in a more intuitive way. In addition, four less frequently used keys have been sacrificed: Print Screen, Scroll Lock, Pause/Break, and Num Lock. These are standard features on full-size keyboards, but the 60% layout lacks them.However, among all these lost functions, I personally find the loss of the four arrow keys, Home, and End to be the most significant. Therefore, I’ll demonstrate using these six keys first; if readers find this insufficient, they can apply the same principles to other keys.

I use two operating systems daily: Windows and Ubuntu. I haven’t yet found a universal solution for global key combinations, so I’ll explain them separately for each system here, though the underlying principles are similar. The idea is to pair a modifier key with an alphanumeric key and then specify what command or key code that combination should trigger.We need to find a key in the main keyboard area that’s easy to reach but rarely used as our modifier key. Readers using a computer can look down at their keyboards; those on a phone can close their eyes and visualize it—the answer is obvious. There’s only one key that brazenly occupies precious real estate on the keyboard, yet most people rarely use it. Its name is Caps Lock.Ask yourself: how many times have you pressed Caps Lock in your entire life so far? Isn’t it only when playing online games or trading insults in English with foreigners that you’d turn on Caps Lock to send their whole family a “greeting”? We only need to type in all caps when we want to forget about international etiquette, right?No, that won’t do—we must be polite. To uphold international etiquette, eliminating Caps_Lock is beneficial and harmless.

Once you’ve decided on Caps_Lock as the modifier key, the next step is the second part of the key combination: choose the alphanumeric keys you’re most comfortable with to pair with it. For me, I use the Vim movement keys: h (←), j (↓), k (↑), and l (→).

Windows

First, let’s talk about the global hotkey solution I use on Windows. AutoHotkey, an open-source, free software, does the trick. Go to the [download page](https://autohotkey.com/download/

) in the top-left corner to download and install it.

Open the Windows Run dialog by pressing ``[Win]+r`

, then enter the following:

shell:startup

. Create a new text file in this folder and paste the following code:

#Persistent
SetCapsLockState, AlwaysOff

Capslock & h::Send {Blind}{Left DownTemp}
Capslock & h up::Send {Blind}{Left Up}

Capslock & j::Send {Blind}{Down DownTemp}
Capslock & j up::Send {Blind}{Down Up}

Capslock & k::Send {Blind}{Up DownTemp}
Capslock & k up::Send {Blind}{Up Up}

Capslock & l::Send {Blind}{Right DownTemp}
Capslock & l up::Send {Blind}{Right Up}

Capslock & a::Send {Blind}{Home DownTemp}
Capslock & a up::Send {Blind}{Home Up}

Capslock & e::Send {Blind}{End DownTemp}
Capslock & e up::Send {Blind}{End Up}

. The first line tells the system to keep AutoHotkey active unless the user explicitly stops it. The second line disables the Caps Lock key’s original function, preventing it from sending the Caps Lock` command.The next six code blocks follow the same template: the first line defines the behavior when the key combination is pressed, and the second line defines the behavior when it is released. Aside from the modifiers “Blind” and “DownTemp,” this template should be easy to understand. “Blind” means “blind compliance”; regardless of what mode other modifier keys have switched to, once this key combination is detected, the command that follows is executed first.This creates an interesting effect: if we need to use this key combination in conjunction with other modifier keys, the “blind” mode acts somewhat like parentheses in arithmetic operations, adjusting the order of operations to ensure that our key combination first sets the direction keys, and then those direction keys interact with the other modifier keys.For example, suppose we’ve typed some text but want to select a few consecutive characters to cut or copy. We could use the mouse to highlight them, or we could use Shift + arrow keys. Since we don’t have physical arrow keys available, we can press Shift + Caps Lock + h to achieve the effect of selecting backward.

DownTemp, on the other hand, sends this key while instructing the system not to assume the key is still held down if it receives other commands. This modifier is generally equivalent to the Down modifier, but if your current key combination produces a modifier key, then DownTemp makes a difference—it prevents subsequent keys from combining with the key produced by the combination. That’s a mouthful.Here, we’re simply sending an arrow key, so using Down or DownTemp makes no difference.

The second line of the template defines the behavior for releasing the key combination. This line is redundant in most apps unless the app intercepts the effect of releasing the arrow keys.For example, in text editors, most arrow keys only perform an action when pressed; releasing them does not trigger any additional action. However, from the computer’s perspective, it detects two separate events, so AutoHotkey allows you to define actions for when a key is released. To be on the safe side, we’ve also defined the action for when the combined arrow key is released.

Save it as “scriptHJKL.ahk”—remember to change the file extension to .ahk. Then double-click the script icon to activate it (if your system hasn’t automatically associated the “.ahk” extension with AutoHotkey, you’ll need to run AutoHotkey first). Usually, nothing will happen on the desktop. But if you open any web page, you’ll find that Caps Lock + J lets you scroll down the page.You can also try opening some apps with the other five keys to test them out. That wraps up the Windows section—as uneventful as it gets.

Ubuntu

It’s a real shame that AutoHotkey, despite being so useful, doesn’t have an Ubuntu version. No worries, though—a quick Google search will turn up plenty of alternatives.The most popular solution online is to use xmodmap in conjunction with AutoKey. Xmodmap is also software designed by Xorg for keyboard mapping on the X desktop system, but its functionality is quite limited. If you’re just modifying one-to-one key mappings, it can handle it easily. But when it comes to mapping multiple keys to a single key, it falls flat.AutoKey, though its name sounds a lot like Windows’ AutoHotkey, isn’t quite as “hot,” can handle multi-key-to-one mappings—but… unfortunately, it can’t disable Caps Lock.So, the vast community of Ubuntu users has combined these two tools: Xmodmap is used to disable Caps Lock and remap its keycode to a non-existent one, and then AutoKey uses that keycode in combination with other alphanumeric keys to create key combinations. Brilliant! Brilliant!

Most Linux distributions should already have xmodmap installed. If yours doesn’t, you can use$ sudo apt install xkeycaps

to install the graphical interface version of xmodmap, though you can also use it via the command line.Once you have xmodmap, you can create a one-line script in your home directory: `

$ echo "keycode 66 = Hyper_L Hyper_R" > disableCapsLock
$ echo "keycode 66 = Caps_Lock" > enableCapsLock

. The first line instructs the system to echo the text within the quotes, and the >` symbol redirects the output to a text file named “disableCapsLock”. The command within the quotes instructs xmodmap to replace the Caps_Lock keycode (66) with the “Hyper” modifier key—a key that once existed in Linux but has since been deprecated.The second line restores the keycode for Caps_Lock back to “Caps_Lock”. From now on, we can use the following two commands to disable and re-enable Caps_Lock:

$ xmodmap disableCapsLock
$ xmodmap enableCapsLock

Okay, now that we’ve turned Caps_Lock into a modifier key, we’ll use AutoKey to handle the key combinations.

$ sudo apt install autokey-gtk
$ autokey&

Install the version of AutoKey with a graphical interface.The second line launches AutoKey. Then, go to “New” -> “Phrase,” and enter “

" in the white box on the right—without the double quotes, just the angle brackets.Next, click “Set” -> “Press to Set” to the right of “Hotkey” in the bottom-right corner. Then, press Caps Lock and H simultaneously to assign Hyper+H as the shortcut to move the cursor to the left. After setting this up, you must click “Save” in the top menu for the changes to take effect. You can set up other shortcuts the same way. If you’re unsure about a symbol, check the [official Help page](https://github.com/autokey/autokey/wiki/Special-Keys

).

It doesn’t seem any harder than configuring Windows, does it? After using it for a while, I started to feel something was off, and I finally realized this was a trap.

The first issue is that if you’re using a laptop’s built-in keyboard and then connect an external keyboard, the Hyper key settings will be reset whenever the keyboard is changed.This is actually a bit annoying. While you can run the xmodmap command again to switch Caps Lock back to Hyper, if you’ve already pressed Caps Lock before switching to Hyper, your typing will be in all caps once you switch to Hyper.Ugh, you have to carefully hold down Shift while suppressing your anger to prevent caps lock from activating, enter the command to restore Caps Lock and turn off caps lock, and then switch to Hyper. Never mind—a martial artist’s heart is as still as water; just treat it as a form of self-cultivation.

Another issue is absolutely unbearable. If you’re a Traditional Chinese user, you’ll find that in most apps, you can’t use this combination of arrow keys to select characters—the text box crashes the moment you press the key combination. Only the physical arrow keys work for character selection. I’m about to spit blood… I’ve searched the entire internet and found no solution… let me spit another liter of blood.

It turns out there are very few Ubuntu users worldwide who, like me, use a 60% keyboard without physical arrow keys and also rely on arrow key combinations to select characters. Foreigners don’t have this problem—once they type a letter, it’s set in stone. Unlike us, with so many homophones, we need arrow keys to make selections. Users across the strait don’t have this problem either—probably because their keyboards have arrow keys, or because their Romanized input rarely auto-selects the wrong characters.

I searched high and low online. At first, I thought I could solve the problem by switching input methods, but after trying every possible combination of Zhuyin, CiYin, New Cool Input, and input method frameworks like Fcitx, Gcin, and iBus, the text selection box still crashed.

Well, if one door closes, another opens. I decided to see if there was an app that would let me select characters properly. I tried many text editors: those that allowed selection had unsightly ghosting (Chrome extension for SSH), and those that let me select characters without ghosting had the cursor disappearing (Chrome extension for Markdown). Other text editors would exit the selection box the moment I pressed a key combination (LibreOffice, Text Editor, Vim, Remarkable, …) — there were just too many. Eventually, I found someone explaining that this seems to be an inevitable issue with the GNOME desktop environment.Want to type in Chinese? Want to select text? Then go find a keyboard with arrow keys. I’m at my wit’s end…

Just as I was feeling utterly defeated, scouring online stores for a 60% keyboard with arrow keys, I stumbled upon a keyboard that was practically a goddess in my eyes—the Leopold FC660M—available for purchase in the US. It features silent red switches. Even though its size exceeded what I could accept and the price was steep, I had once actually handled one of its relatives—a keyboard from the same series but in a different size. I’ll never forget that day…

I wandered around the sweltering Guanghua Market, drenched in sweat, until I finally found that shop called “XX Corps” in the basement. I went inside and told the enthusiastic owner I was looking for a quiet 60% keyboard with a great feel. The owner grinned mischievously and ran off, returning moments later with an FC980M and urging me to give it a try.I thought to myself, “It’s just a keyboard—what’s there to get so worked up about?” I sat down and set the keyboard up.

I closed my eyes. My right index finger pressed the J key with neither haste nor haste, neither too lightly nor too heavily. The tactile feedback was perfectly balanced—add a fraction more and it would be too heavy; subtract a fraction and it would be too light.In that brief moment—less than a second—as my finger bottomed out, the world seemed to stand still, and my heart suddenly skipped a beat.

That feeling was so familiar, yet so distant. It was like holding the hand of a girl I had a crush on for the very first time: delicate, smooth, shy, warm, conscious, strong—yet full of tenderness.In that moment, I was shaken to my core, my blood surging. Only one thought filled my mind: “No need to look any further—this is the one.”

But when I opened my eyes, it was as if I’d been knocked back down to earth from heaven. I was still sitting in that basement, surrounded by the same bustling crowd of geeks, and the keyboard before me was still the FC980M—a size I simply couldn’t accept.I wasn’t ready to give up. I asked the owner if they had a 60% version of this model. He regretfully told me they didn’t; the smallest available was the 65% 66-key FC660M. Sigh, what a bummer. Heaven, why must you be so cruel to me? The tactile feedback was superb, the keystrokes were silent—but it wasn’t the size I wanted.

I asked the owner if I could take a look at the 65% version. Perhaps I could overlook that extra 5% that pushed it past my size threshold, but the owner said it wasn’t that he didn’t want to show it to me—it was simply sold out. That’s why he’d recommended the 98-key FC980M to me. Oh heavens! You let me meet him, yet destined us to have no future together. How hateful, how tragic.

Back to the present: I’m currently having trouble with Chinese character selection in Ubuntu. Could this be the perfect opportunity to get my hands on an FC660M? Could all this hassle be fate’s way of paving the path for the FC660M to find its way into my home? I’m torn. FC660M, why didn’t you just rename yourself the FC610M or FC620M?If you only had 61 or 62 keys, maybe you could fit into a 60% size. Other companies have managed to squeeze arrow keys into 60% keyboards, but they aren’t Leopold’s silent red-switch keyboards. Oh heavens! How utterly unfair!

After moping around for a few days, I calmed down and used xev and xbindkeys to analyze the keystroke sequence sent by my current solution. I discovered that the Hyper modifier key is still being captured by Ubuntu’s apps—even though it’s an invalid key, the character selection box is still triggering an error because of it.As I kept searching, I stumbled upon a solution by a German guy. It wasn’t specifically for Chinese input, but it was one way to implement key combinations on Ubuntu.

Legend has it that the original Unix-like computers and their corresponding ancient keyboards defined many modifier keys, and the Hyper key mentioned earlier—which caused errors—was just one of them. Others include Meta and Alt-Gr.If the problem is caused by a Hyper-based key combination, you might want to try the German guy’s Xmodmap+X11-XKB solution.

First, modify the DisableCapsLock file mentioned earlier:

$ echo "keycode 66 = ISO_Level3_Shift" > disableCapsLock

. This time, we’ll change the Caps_Lock keycode to Alt-Gr, that ancient modifier key. Next, go to AutoKey and clear all key combination definitions. Theoretically, this shouldn’t cause any conflicts, but we need to be able to tell which solution worked.

Next, enter the following command in the terminal to edit Linux’s low-level keyboard mapping: `

$ cp /usr/share/X11/xkb/symbols/us ./us_backup
$ sudo vi /usr/share/X11/xkb/symbols/us

`. This is the mapping definition file for US keyboards. The first line creates a backup of the keyboard configuration file. Be prepared for the possibility that if you make a mistake, you might not be able to use the keyboard after logging back in—you’ll have to remotely SSH in to revert the changes.The second line opens this configuration file in an editor. Next, paste the following code at the beginning of the file:

partial keypad_keys
xkb_symbols "altgr-vim" {
    key <CAPS> { [ ISO_Level3_Shift      ] };
    key <AC06> { [ h, H, Left,  Left     ] };
    key <AC07> { [ j, J, Down,  Down     ] };
    key <AC08> { [ k, K, Up,    Up       ] };
    key <AC09> { [ l, L, Right, Right    ] };
    key <AC01> { [ a, A, Home,  Home     ] };
    key <AD03> { [ e, E, End,   End      ] };
};

Then, search for “xkb_symbols “basic” {” in the editor. Once found, paste the following code on the line immediately following the curly brace:

include "us(altgr-vim)"

Save the file, log out, and log back in. After that, use$ xmodmap disableCapsLock to enable global arrow key combinations and freely use key combinations to select text!(But then wouldn’t the tragic love story of the FC660M come to an end?!)

If you’re curious about how to expand this mapping table, you can follow the “altgr-vim” format. Find the key codes you want to pair with the Alt-Gr modifier at the bottom of the same file, paste them into “altgr-vim,” and then modify the symbol inside the square brackets. This way, you can add back the function keys you need.

© 2016 - 2026 FreelimbO — Hardware, DIY & Tech Reviews

Powered by Hugo with theme Dream.

 總瀏覽
關於 FreelimbO

FreelimbO是個奉行一時開箱一時爽,一直開箱一直爽的3C愛好者,同時也是喜歡折騰自己的自虐者,明知有簡單的方法可以花錢了事,但卻偏偏喜歡走土法炮製路線。

這個網誌開始是作者想要在樹莓派上架站寫網誌紀錄在樹莓派上架站寫網誌,但是之後也會持續分享開箱心得、讀書心得、人工智能相關論文閱讀心得、遊戲心得、還有最慘無人道的各種踩坑排雷心得。敬請加入你的書籤,持續關注最新文章,謝謝。

Social Links