Last Updated on 2021-07-02 by Clay
Sometimes, we would like to be able to map the buttons with different functions on the keyboard, such as the arrow keys up, down, left and right to the shortcut keys Alt + WSAD
.
According to a netizen on stackoverflow, this setting can protect his hand.
Today, I will record how to achieve such a function through the xmodmap
command.
Configure shortcut keys
First, we need to know the keycode of the "Alt" key. We can use xev
to check the keycode of the key we want. Enter the following command in Terminal:
xev
We will see the following screen:
We only need to move the mouse to the small window that pops up, click and press the keyboard button we want, for example, I want to know the Keycode of "Alt", so I clicked "Alt":
We can see that for the key we want "Alt_L" (the Alt key on the left), its Keycode is 64.
Step 0: Install xcape
We need to install xcape
tool.
sudo apt install xcape
Step 1: Create a ~/.xmodmap file
Then we come to actually write the configuration program, first make an xmodmap file.
vim ~/.xmodmap
Take the function that we want to make "Alt" + "WSAD" corresponding to the arrow keys as an example, we write this configuration in the configuration file:
keycode 64 = Mode_switch
keysym a = a A Left
keysym d = d D Right
keysym w = w W Up
keysym s = s S Down
Storage is closed.
Step 2: Make execution script
Open an executable file:
vim ~/xmodmap.sh
Write in it:
xmodmap ~/.xmodmap
xcape -r 'Mode_switch=Escape'
After saving the file, we set the permissions of this file:
chmod +x ~/xmodmap.sh
Step 3: Create a file that automatically runs on startup
vim ~/.config/autostart/xmodmap.desktop
Write in it:
[Desktop Entry]
Type=Application
Exec=sh -c "$HOME/xmodmap.sh"
Hidden=false
X-GNOME-Autostart-enabled=true
Name=xmodmap
Comment=xmodmap script
After the save is closed, if you want the settings to take effect immediately, just run the "xmodmap.sh" file you just set. However, after I experienced a reboot, I found that the settings did not take effect automatically, so I did the following again:
Go to the folder "~/.config/autostart/" and directly set the "xmodmap.desktop" file:
Check "Allow executing file as program".
Double click on this file.
Choose trust.
After rebooting, my configuration finally took effect.