How to Use the Command 'evtest' (with Examples)
- Linux
- December 17, 2024
Evtest is a powerful utility within Linux systems utilized for testing and debugging input device drivers. It provides essential information and data on input devices such as keyboards, mice, and other peripherals connected to your system. Evtest plays a critical role in diagnosing issues related to input devices and aids developers and system administrators in assessing how these devices interact with the operating system at a low level. This command is particularly valuable for those who need thorough insights into hardware-level interactions and want to ensure ideal device functionality.
Use Case 1: List All Detected Input Devices
Code:
sudo evtest
Motivation:
Listing all detected input devices is the very first step in gaining an overview of the input hardware connected to your system. This is crucial when you are diagnosing issues or need to configure specific devices. By identifying all connected devices, you can move forward in testing individual units, ensuring they operate as expected, or receive appropriate configuration attention.
Explanation:
sudo
: This is a prefix that requires administrative privileges to execute the command. Input device information is generally secured, and accessing it usually requires elevated permissions.evtest
: This is the command to call the evtest utility. Without any additional arguments, it provides a list of all detected input devices along with their respective details.
Example Output:
After executing the command, you might find a list similar to the following:
Available devices:
/dev/input/event0: "AT Translated Set 2 keyboard"
/dev/input/event1: "Lid Switch"
/dev/input/event2: "Sleep Button"
/dev/input/event3: "Power Button"
/dev/input/event4: "USB Optical Mouse"
/dev/input/event5: "Integrated Webcam"
Use Case 2: Display Events from a Specific Input Device
Code:
sudo evtest /dev/input/eventnumber
Motivation:
Monitoring events from a specific input device is essential when diagnosing issues that may be localized to that particular device. For instance, if a particular key on your keyboard is not responding, or a button on your mouse seems unresponsive, capturing events can reveal whether the input is being registered at the hardware level.
Explanation:
sudo
: Administrative privileges are needed to access and monitor the input events. This ensures you have the necessary permissions to listen to device events.evtest
: Calls the evtest utility to proceed with event monitoring./dev/input/eventnumber
: This is the specific device event handler that you wish to monitor. You replaceeventnumber
with the actual number from the devices list previously obtained.
Example Output:
When executed, the command generates a live stream of events like:
Event: time 1632837399.969232, type 1 (EV_KEY), code 30 (KEY_A), value 1
Event: time 1632837399.969233, type 1 (EV_KEY), code 30 (KEY_A), value 0
Event: time 1632837400.152364, type 1 (EV_KEY), code 31 (KEY_S), value 1
Event: time 1632837400.152365, type 1 (EV_KEY), code 31 (KEY_S), value 0
Use Case 3: Grab the Device Exclusively, Preventing Other Clients from Receiving Events
Code:
sudo evtest --grab /dev/input/eventnumber
Motivation:
Grabbing a device exclusively is important when you need to perform a focused test without interference from other system processes. This could be used during development or testing phases, where unintended interactions with other software might cause erratic behavior or skew results.
Explanation:
sudo
: As this operation affects system behavior significantly, administrative permissions are required.evtest
: This initializes the evtest command.--grab
: The grab option instructs evtest to take exclusive control of the device, ensuring that other processes cannot receive events from it while this command is active./dev/input/eventnumber
: Specifies which device to grab exclusively, determined from the device list obtained earlier.
Example Output:
When the command runs, you will not see immediate output. However, it ensures that the device’s events are not accessed by any other application until you terminate the session.
Input device /dev/input/eventnumber grabbed.
Use Case 4: Query the State of a Specific Key or Button on an Input Device
Code:
sudo evtest --query /dev/input/eventnumber event_type event_code
Motivation:
Knowing the state of a specific key or button is useful when verifying if the physical aspect of a keyboard or mouse is functioning as expected. This capability helps ensure that direct hardware interactions are correctly recognized by the system, and the outputs correspond to intentional inputs.
Explanation:
sudo
: Necessary administrative privileges for querying device states.evtest
: Utilizes the evtest tool to access detailed input device information.--query
: Enables querying a specific key or button’s state from the input device./dev/input/eventnumber
: Refers to the target device for querying.event_type
andevent_code
: Specifies the type and code of the event you want to query, corresponding to a particular key or button.
Example Output:
This outputs the state of the key or button, indicating if it’s pressed or released, such as:
Querying device stateā¦
Event type 1 (EV_KEY), code 30 (KEY_A): value 0
Conclusion
The evtest command is an invaluable tool for those needing to perform detailed diagnostics and operations on input devices within a Linux environment. From listing all detected devices, monitoring specific device events, ensuring exclusive device access, to querying the state of specific inputs, evtest offers comprehensive capabilities that facilitate system administration and device troubleshooting, ensuring your devices operate optimally under various conditions.