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:

  1. Open a terminal.
  2. Type evtest and hit Enter to produce a list of devices. Each device will be associated with a file path under /dev/input/.
  3. Select the appropriate device (often named eventX, where X is a number) corresponding to your keyboard.
  4. 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: