4
I'm trying to bind a key (using xbindkeys
) to turn my monitors on/off under X. So far I have the key bound to this script:
#!/bin/bash
MOUSE="Logitech USB-PS/2 Optical Mouse"
if [[ ! -z $(xset q | grep "Monitor is On") ]]; then
logger "Turning off display"
xinput disable "$MOUSE"
xset s 2 2
xset dpms force off
else
logger "Turning on display"
xinput enable "$MOUSE"
xset s 3600 3600
xset dpms force on
fi
This works almost perfectly: if the screen is on, it disables the mouse (to prevent accidental wakeups), sets the blanking time to 2 seconds (just in case something wakes it up) and turns the screen off. If it's off, it restores previous settings and turns the screen back on.
Unfortunately, this seems to not work when actually bound to a key: it always believes the screen is on. I suspect that's because pressing the key turns them back on, and then runs the script which turns them off again.
I can't find any way to prevent the screen being turned on automatically on keypress, except to disable the keyboard entirely (which would make it hard to turn back on).
An even better solution would be if I could have a script run any time the screen gets turned on/off; then I'd be able to still turn the screen back on by pressing any key, not just the one bound to that script.
IIRC X11 exposes three events per key press: key-down, key-press (possibly repeated) and key-up. Check to make sure your script isn't being invoked by more than one of those. – a CVn – 2015-01-30T22:28:12.757