When working on our computers, we often prefer to have the monitor turn off after a certain period of inactivity to save energy and reduce eye strain. However, there are times when we want the computer to play music or perform other tasks while the monitor is off. In such cases, it can be frustrating when a simple keyboard press accidentally wakes the monitor.
Understanding the Wake-up Mechanism in Linux
In Linux, the wake-up mechanism is controlled by the kernel and the power management system. The kernel receives input from various devices, such as the keyboard, mouse, and network interfaces, and can wake the system from a suspended or powered-off state based on these inputs. The power management system, on the other hand, controls the monitor's power state and can be configured to keep the monitor off even when the system receives input.
The Keyboard Input and the systemd Service
When a key is pressed on the keyboard, the input is sent to the system's input subsystem, which then notifies the systemd service. Systemd is a system manager in Linux that handles various system services, including power management. The systemd service can then decide whether to wake the system or keep it in the current state.
Preventing Specific Keys from Waking the Monitor
To prevent specific keys from waking the monitor, we need to modify the systemd service configuration. The following steps show how to prevent the multimedia keys from waking the monitor:
Open the systemd service configuration file:
sudo nano /etc/systemd/logind.confFind the following line:
HandleSystemKeys=yesAnd change it to:
HandleSystemKeys=noAdd the following lines to the end of the file:
[Login] HandleLidSwitch=ignore HandleSuspendKey=ignore HandleHibernateKey=ignore HandleLidSwitchDocked=ignore HandleHotPlug=ignoreSave the file and exit the editor.
Restart the systemd service:
sudo systemctl restart systemd-logind
This will prevent the system from waking up when the multimedia keys are pressed. If you want to prevent other keys from waking the system, you can add their corresponding scan codes to the following file:
/lib/udev/rules.d/95-keyboard-quirks.rules
Add the following lines to the file:
ACTION!="remove", KBD_KEYCODE="$(kbd_keycode KEYCODE)", RUN+="/bin/systemd-inhibit --what=idle:sleep:hibernate:hybrid-sleep --who=KEYBOARD --mode=block /bin/true"
Replace KEYCODE with the scan code of the key you want to prevent from waking the system. You can find the scan codes of the keys using the evtest tool.
Testing the Configuration
To test the configuration, you can use the following command to suspend the system:
systemctl suspend
Or use the following command to turn off the monitor:
xset dpms force off
Then, try pressing the keys you prevented from waking the system. The monitor should remain off.
The wake-up mechanism in Linux is controlled by the kernel and the power management system.
The keyboard input is sent to the system's input subsystem and then notified to the systemd service, which can then decide whether to wake the system or keep it in the current state.
To prevent specific keys from waking the monitor, you need to modify the systemd service configuration and add the keys' scan codes to the udev configuration file.