- Posted on
- • Questions and Answers
Capture raw keyboard input with `evtest` (bypass X11/Wayland)
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Capturing Raw Keyboard Input in Linux Using evtest
Q&A: Understanding evtest
for Raw Keyboard Inputs
Q1: What is evtest
?
A1: evtest
is a command-line utility in Linux used to capture detailed information about input devices, including keyboards. It allows users to monitor raw input devices beyond the graphical environments like X11 or Wayland.
Q2: Why use evtest
instead of other input capturers?
A2: Unlike other tools that depend on graphical interface configurations, evtest
operates directly with device files in the /dev/input
directory. This allows it to capture inputs even in environments without a graphical user interface (GUI).
Q3: How can I install evtest
?
A3: evtest
can be installed from the package repositories of most Linux distributions. For instance, on Ubuntu, you can install it using:
sudo apt-get install evtest
Q4: How do I use evtest
to monitor a specific keyboard?
A4: First, list all input devices by running evtest
without arguments. Identify your keyboard from the list, and then run evtest
followed by the device path. For example:
evtest /dev/input/event3
This command will start displaying all the raw inputs from the specified keyboard device.
Background: Working with evtest
evtest
provides a simple yet powerful interface for capturing input data directly from hardware devices. It lists down all the events supported by the device, such as key presses, and provides real-time feedback when these events occur.
A basic example of using evtest
begins with finding the exact device file:
- Open a terminal.
- Type
evtest
and hit Enter to produce a list of devices. Each device will be associated with a file path under/dev/input/
. - Select the appropriate device (often named eventX, where X is a number) corresponding to your keyboard.
- Run
evtest
followed by your device path to start capturing inputs.
Executable Script: Demonstrating evtest
Functionality
Let's create a simple script that uses evtest
to monitor key presses and outputs them in a more readable format:
#!/bin/bash
# Script to capture and display key press events using evtest
# Check if evtest is installed
if ! command -v evtest &> /dev/null
then
echo "evtest could not be found, please install it first."
exit
fi
# List devices and prompt user to select one
echo "Please select a device from the list to monitor:"
evtest
read -p "Enter the device path (e.g., /dev/input/event3): " device_path
# Clear the screen and begin monitoring the selected device
clear
echo "Monitoring $device_path. Press CTRL+C to stop."
echo ""
# Capture and format the output from evtest
sudo evtest $device_path | grep --line-buffered -E 'type 1 \(EV_KEY\)' | while read line
do
if [[ $line == *"value 1"* ]] ; then
key=$(echo $line | awk '{print $4}')
echo "Key pressed: $key"
fi
done
This script checks for the installation of evtest
, prompts the user to select a device, and then monitors and formats the keyboard press events. The script filters out other events to focus only on key presses.
Conclusion: The Versatility of evtest
Understanding and utilizing tools like evtest
enhances your capability to interact and experiment with Linux systems at a deeper level. Whether you are a developer, a system administrator, or just a tech enthusiast, being able to capture raw input directly from devices can be immensely valuable, especially during diagnostics, debugging, or developing input-driven applications.
By leveraging evtest
beyond conventional GUI-dependent tools, one gains more control and insight into the finer workings of Linux input systems, helping bridge the gap between user interaction and device management in varying environments.
Further Reading
For readers interested in exploring more about evtest
and related topics in Linux input systems, these resources might prove helpful:
Linux Input Subsystem Documentation:
https://www.kernel.org/doc/Documentation/input/input.txt
Explore the detailed kernel documentation covering the input subsystem, helpful for understanding how Linux handles input devices at a low level.Exploring Linux’s evtest Tool:
https://www.linux.com/topic/desktop/exploring-linuxs-evtest-tool/
A thorough guide explaining the uses ofevtest
, including practical examples to help users start experimenting.List and Understand Linux Processes:
https://opensource.com/article/21/2/linux-process-management
This article can be beneficial for those looking to understand how processes work in Linux, which complements managing and using tools likeevtest
.Using Input Event Drivers:
https://elinux.org/images/7/72/Input-Event-Drivers.pdf
Dive deeper into driver-level handling of input events in Linux, enhancing the understanding needed for effective use of tools likeevtest
.Getting Started with Linux Device Drivers:
https://opensourceforu.com/tag/linux-device-drivers-series/
A series aimed at helping novices find their way around writing and understanding Linux device drivers, setting a good base for mastering utilities likeevtest
.