
kbd_mode Command in Linux
The kbd_mode command in Linux is a utility used to manage the keyboard mode of a console. This command is primarily used for setting the keyboard in different modes, such as raw, medium raw, or XLATE (translated) mode. Itâs particularly useful for advanced users and developers who need to manipulate keyboard inputs for various purposes, including debugging, configuring custom input handling, or working with low-level input devices.
Table of Contents
Here is a comprehensive guide to the options available with the kbd_mode command â
- Understanding kbd_mode Command
- kbd_mode Command Options
- Changing Keyboard Modes of kbd_mode Command
- Querying Current Keyboard Mode of kbd_mode Command
- How to Use kbd_mode Command in Linux?
- Advanced Usage of kbd_mode Command in Linux
Understanding kbd_mode Command
Whether you are a developer, system administrator, or advanced user, the kbd_mode command offers valuable functionality for managing keyboard input on Linux systems.
Basic Syntax
The basic syntax for the kbd_mode command is as follows −
kbd_mode [options]
The command can be executed with various options to change the keyboard mode or query the current mode. The most commonly used options include -k, -s, -a, and -u.
kbd_mode Command Options
Let's now take a look at the options used with the kbd_mode command −
-k or --keyboard-raw
This option sets the keyboard to raw mode. In raw mode, the keyboard driver sends keycodes directly to the application without translating them to ASCII characters. For example −
sudo kbd_mode -k

When you set the keyboard to raw mode using the -k option, you might notice that the console behaves differently, and key presses may not produce the expected characters.
-s or --string
This option sets the keyboard to XLATE (translated) mode, which is the default mode for most systems. In XLATE mode, the keyboard driver translates key presses into ASCII characters before sending them to the application. For example −
sudo kbd_mode -s

Using the -s option ensures that the keyboard behaves normally, translating key presses into characters as expected.
-a or --ascii
This option sets the keyboard to medium raw mode. In medium raw mode, the keyboard driver sends scancodes to the application, which are then translated into ASCII characters. For example −
sudo kbd_mode -a

Medium raw mode is useful for applications that need to process scancodes but still want to work with ASCII characters.
-u or --utf8
This option is used to set the keyboard to Unicode mode, where key presses are translated into Unicode characters. For example −
sudo kbd_mode -u

Using the -u option is particularly useful for applications that need to handle international text input.
-h or --help
This option displays the help message, providing a summary of the available options and their usage. For example −
kbd_mode -h

Changing Keyboard Modes of kbd_mode Command
To change the keyboard mode, you must have superuser privileges. This is because changing the keyboard mode can affect the entire console and may require elevated permissions. For example −
sudo kbd_mode -k

In this example, the keyboard is set to raw mode, and you will notice that key presses are no longer translated into ASCII characters.
Querying Current Keyboard Mode of kbd_mode Command
To query the current keyboard mode, you can use the kbd_mode command without any options −
sudo kbd_mode

The command will output the current keyboard mode, indicating whether it is in XLATE, raw, or medium raw mode.
How to Use kbd_mode Command in Linux?
Let's explore some practical examples to demonstrate the use of the kbd_mode command in different scenarios.
Switching to Raw Mode for Debugging
Raw mode can be useful for debugging keyboard input issues. For example, if you are developing a custom input handler and need to see the raw keycodes being sent by the keyboard, you can switch to raw mode. For example −
sudo kbd_mode -k

After switching to raw mode, you can use tools like showkey to display the raw keycodes being sent by the keyboard. For example −
showkey --scancodes

This will display the scancodes for each key press, allowing you to debug and analyze the input.
Returning to Normal Mode
After debugging, you can return to the default XLATE mode to restore normal keyboard behavior. For example −
sudo kbd_mode -s

This ensures that key presses are translated into ASCII characters as expected.
Handling International Input
If you need to handle international input and work with Unicode characters, you can switch to Unicode mode. For example −
sudo kbd_mode -u

In Unicode mode, key presses will be translated into Unicode characters, allowing you to work with a wider range of text input.
Advanced Usage of kbd_mode Command in Linux
For advanced users, the kbd_mode command can be used in conjunction with other tools and scripts to automate keyboard mode changes and manage input handling more effectively.
Automating Keyboard Mode Changes
You can create a script to automate the process of switching keyboard modes based on specific conditions. For example, you might want to switch to raw mode when a certain application is launched and revert to XLATE mode when the application is closed. Take a look at the following Example Script −
#!/bin/bash # Switch to raw mode sudo kbd_mode -k # Launch the application /path/to/your/application # Wait for the application to exit wait $! # Revert to XLATE mode sudo kbd_mode -s
Save this script as switch_kbd_mode.sh and make it executable −
chmod +x switch_kbd_mode.sh
You can then run the script to automate the keyboard mode changes −
./switch_kbd_mode.sh
Custom Input Handling
If you are developing a custom input handler, you can use the kbd_mode command to set the keyboard to raw mode and capture the keycodes directly. This allows you to implement custom key mappings or input processing. For example −
#!/usr/bin/env python import os import sys import termios import tty # Set the keyboard to raw mode os.system('sudo kbd_mode -k') # Open the input device with open('/dev/tty', 'rb') as f: # Get the current terminal attributes old_attrs = termios.tcgetattr(f) try: # Set the terminal to raw mode tty.setraw(f) print('Press keys (Ctrl+C to exit):') while True: # Read a single character from the input device ch = f.read(1) print(f'Key pressed: {ord(ch)}') except KeyboardInterrupt: pass finally: # Restore the original terminal attributes termios.tcsetattr(f, termios.TCSADRAIN, old_attrs) # Revert to XLATE mode os.system('sudo kbd_mode -s')
Save this script as custom_input_handler.py and run it −
python3 custom_input_handler.py
The script sets the keyboard to raw mode, captures key presses, and prints the corresponding keycodes. When you exit the script (Ctrl+C), it restores the terminal settings and reverts to XLATE mode.
Troubleshooting Tips of kbd_mode Command
- Permissions − Ensure that you have superuser privileges when using the kbd_mode command. If you do not have the necessary permissions, the command may fail to execute.
- Compatibility − The kbd_mode command is primarily designed for use with Linux console terminals. If you are using a graphical terminal emulator or a different operating system, the command may not work as expected.
- Restoring Default Mode − If you accidentally switch to an unexpected keyboard mode and experience issues, you can restore the default XLATE mode by running the following command:
sudo kbd_mode -s

Conclusion
The kbd_mode command is a powerful tool for managing keyboard input modes on Linux consoles. By understanding the various options and modes available, you can effectively troubleshoot keyboard input issues, handle custom input processing, and work with international text input.