
setkeycodes Command in Linux
The setkeycodes command in Linux is a utility that allows users to map scancodes from keyboard keys to specific keycodes. This is particularly useful for configuring keyboards that may not be recognized correctly by the Linux kernel or for remapping keys to different functions.
Table of Contents
Here is a comprehensive guide to the options available with the setkeycodes command â
- Understanding setkeycodes Command
- Syntax for the setkeycodes Command
- How to Use setkeycodes Command in Linux?
- Examples of setkeycodes Command in Linux
Understanding setkeycodes Command in Linux
The setkeycodes command in Linux serves a very specific purpose: it allows users to modify the kernel's scancode-to-keycode mapping table. This means it provides a way to tell the Linux kernel how to interpret the raw signals coming from a keyboard.
When a key is pressed, the keyboard sends a "scancode," which is a hardware-level signal. The kernel then translates this scancode into a "keycode," which is a software-level representation of the key. setkeycodes gives users the ability to change this translation, which is particularly useful for keyboards with unusual or non-standard keys.
The setkeycodes command maps scancodes to keycodes, allowing users to customize the behavior of their keyboard. Scancodes are the raw codes sent by the keyboard to the computer when a key is pressed or released, while keycodes are the codes that the operating system translates into actions or characters.
Syntax for the setkeycodes Command
The basic syntax of the setkeycodes command is as follows −
setkeycodes scancode keycode
Parameters
- scancode − The raw code generated by the keyboard.
- keycode − The code that the operating system translates into actions or characters.
Understanding Scancodes and Keycodes
To effectively use setkeycodes, it's important to understand the difference between scancodes and keycodes.
- Scancodes − These are the raw codes generated by the keyboard hardware. Each key press or release generates a unique scancode.
- Keycodes − These are codes assigned by the operating system to represent specific keys or actions. The keycodes are mapped to actions or characters by the operating system's input subsystem.
Viewing Current Keycode Mappings
Before making changes, you might want to view the current keycode mappings. This can be done using the dumpkeys command.
dumpkeys

Identifying Scancodes
To identify the scancodes generated by your keyboard, you can use the showkey command.
showkey -s

How to Use setkeycodes Command in Linux
This utility is primarily used in situations where a keyboard has keys that the Linux kernel doesn't recognize or interprets incorrectly. By using setkeycodes, users can manually define the mapping between a specific scancode and the desired keycode. This allows for customization of keyboard behavior at a very low level.
However, it's important to note that changes made with setkeycodes may not persist across reboots, and that modern Linux systems, especially those using graphical environments, often rely on higher-level input handling systems that may override these low level changes. Also there are limitations to the command working with modern USB keyboards.
The setkeycodes command is used to map a specific scancode to a keycode. This can be useful for remapping keys on your keyboard.
Example: Remapping a Key
Suppose you have a key that generates the scancode 0x70 and you want to map it to the keycode 101 (which corresponds to the F13 key).
setkeycodes 0x70 101

This command maps the scancode 0x70 to the keycode 101.
Persistent Keycode Mappings
To make the keycode mappings persistent across reboots, you can add the setkeycodes commands to a startup script, such as /etc/rc.local.
Example: Persistent Mapping in /etc/rc.local
Edit the /etc/rc.local file and add the setkeycodes commands.
#!/bin/sh -e # Add custom keycode mappings setkeycodes 0x70 101 exit 0
Troubleshooting Keycode Mappings
If the key mappings do not work as expected, you can check the following:
- Ensure that the correct scancode is being used.
- Verify that the keycode is available and not already in use.
Common Use Cases for setkeycodes
Here are some common use cases for the setkeycodes command:
- Customizing Function Keys − Mapping scancodes to function keys (F13 to F24) for custom actions.
- Special Keyboards − Configuring keyboards with special or non-standard keys.
- Gaming Keyboards − Remapping keys on gaming keyboards for better control.
Examples of setkeycodes Command in Linux
Here are some detailed examples of setkeycodes usage.
Example 1: Remapping a Media Key
Suppose you have a media key that generates the scancode 0xB3 and you want to map it to the keycode 142 (which corresponds to the "Stop" media key).
setkeycodes 0xB3 142

This command maps the scancode 0xB3 to the keycode 142.
Example 2: Remapping a Special Key on a Laptop
Suppose you have a special key on your laptop that generates the scancode 0x8A and you want to map it to the keycode 148 (which corresponds to the "Calculator" key).
setkeycodes 0x8A 148

This command maps the scancode 0x8A to the keycode 148.
Conclusion
The setkeycodes command is a powerful utility for customizing the behavior of your keyboard in Linux. By mapping scancodes to keycodes, you can remap keys to different functions, configure special keyboards, and customize your keyboard for specific use cases.