Turning the monitor off on key press with session lock

Hi,

I wonder if there is an easy way to configure Hyprland, Hypridle, and Hyprlock so that when I press, say, Alt+Ctrl+L, the screen locks and the monitor turns off.

The first step - locking the screen - is straightforward:

exec-once = hypridle
bind = ALT CTRL, l, exec, loginctl lock-session

However, this does not turn off the monitor immediately; it will only do so once Hypridle’s timeout expires (see the configuration below).

I tried using a simple script, lock.sh (see below), and binding it instead, and it works. However, the screen does not turn on when a key is pressed, but only after the timeout expires.

unlock.sh:

#!/bin/bash
hyprctl dispatch dpms off
loginctl lock-session

Thanks,

PS. For reference, here is my hypridle.config:

general {
    lock_cmd = pidof hyprlock || hyprlock       # avoid starting multiple hyprlock instances.
    before_sleep_cmd = loginctl lock-session    # lock before suspend.
    after_sleep_cmd = hyprctl dispatch dpms on  # to avoid having to press a key twice to turn on the display.
}
listener {
    timeout = 300                                 # 5min
    on-timeout = loginctl lock-session            # lock screen when timeout has passed
}

listener {
    timeout = 330                                 # 5.5min
    on-timeout = hyprctl dispatch dpms off        # screen off when timeout has passed
    on-resume = hyprctl dispatch dpms on          # screen on when activity is detected after timeout has fired.
}
bind = mod, key, exec, ~/script.sh

script:

sleep 1
hyprctl dispatch dpms off

Does this work? Usually without a sleep, your key up event wakes hyprland up immediately.

Thank you, Vaxry, for investing your time in my issue. I believe you misunderstood the problem, though.

It is not that the screen does not turn off with my solution; rather, it cannot be turned back on with a keypress. After invoking the script (whether my version or yours) with the bind key combination, there are only two options to turn the screen back on:

  1. Wait for 330 seconds (so that the hyperidle kicks in) and then press a key.
  2. Log in using a different console (ALT+CTRL+F2) and run the command: hyprctl -i 0 dispatch dpms on.

Doing the latter demonstrates the difference between your version and mine of the script: with my version, the lock screen is displayed, whereas with yours, the screen does not lock (unless 300 seconds have passed and hyperidle has locked it).

I suspect this issue occurs because Hyperidle is not aware that the screen has been turned off and that the session may have been locked. As a result, it does not trigger the on-resume callback.

C.

No, by default hyprland doesn’t have a way to resume. Enable either mouse_move_enables_dpms or key_press_enables_dpms (see Variables – Hyprland Wiki )

Maybe the defaults there could be changed…

1 Like