Linux: Prevent keyboard turning screen on under X

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.

Rena

Posted 2015-01-30T21:57:10.270

Reputation: 141

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

Answers

2

The script is run immediately after you press the bound key, but the screen is only turned back on when you release it.

You can test this by entering

xset dpms force off

in your terminal and holding down Enter.

The screen stays off until you release the key and then immediately turns back on.

A simple workaround is to add a small delay in order to run the xset command after the key is released (using the sleep command, for example).

holytrousers

Posted 2015-01-30T21:57:10.270

Reputation: 21

1

Something I have done is to use xscreensaver (set to blank the screen only, not to run any graphics hacks), and then watch it using xscreensaver-command -watch:

   -watch  Prints a line each time the screensaver changes state: when the
           screen  blanks,  locks,  unblanks,  or when the running hack is
           changed.  This option never returns; it is intended for use  by
           shell  scripts  that  want  to react to the screensaver in some
           way.  An example of its output would be:
           BLANK Fri Nov  5 01:57:22 1999
           RUN 34
           RUN 79
           RUN 16
           LOCK Fri Nov  5 01:57:22 1999
           RUN 76
           RUN 12
           UNBLANK Fri Nov  5 02:05:59 1999

The man page includes an example that's worth a look.

Unfortunately, stock xscreensaver insists on setting DPMS settings to its own values from time to time. I submitted a patch to stop it doing that, but jwz refused it. You may find the patch useful - or just compile xscreensaver with --without-dpms-ext.

Alternatively, it may be instructive to examine the xscreensaver code for clues as to how to write your own watcher.

Toby Speight

Posted 2015-01-30T21:57:10.270

Reputation: 4 090