yuexiaduzhuo https://www.reddit.com/r/i3wm/comments/9ebemt/locking_i3_when_lid_of_laptop_is_closed/
I use xss-lock, quite a handy daemon that listens to all sort of events.
exec --no-startup-id xss-lock -- ~/.config/i3/lock.sh
This is my lock script:
#!/bin/sh
set -e
xset s off dpms 0 10 0
i3lock --color=4c7899 --ignore-empty-password --show-failed-attempts --nofork
xset s off -dpms
It sets DPMS so the locker does not stay on forever (avoiding battery drain when just locked). You can just invoke i3lock straight away really.
Then to lock my screen via a shortcut I just force DPMS to disable the screen and that triggers xss-lock.
bindsym ctrl+$mod+l exec --no-startup-id sleep 1 && xset dpms force suspend