views:

367

answers:

2

I have a script that is executed periodically in the background. I want to prevent its execution if the Shift key is pressed.

The idea is to poll the keyboard's Shift button state, and if it's pressed — terminate the script immediately.

Any ideas? X server is allowed to use: I guess it will help.

UPD: I'm currently using this stupid hack:

[ $( sh -c 'cat /dev/input/by-id/usb-*kbd & sleep 0.5 ; kill $! 2>/dev/null' | wc -c ) -gt 1 ] && exit 

The script just detects current keyboard events but does not distinguish them. 0.5sec is the kbd-events watch period. Not very nice, but still works :)

+1  A: 

First off, you can monitor key up/down events, but as far as I know, there's no way to tell if the key is currently pressed. If you're OK with that, then...

That implies that the thing listening for the key event has to be running in another thread. The shell script will have to spawn a program in the background that listens for key events and sends a signal to the parent script on keypress. You can use trap to respond to the signal by exiting gracefully.

Check out KeyPress. It might give you a good start.

You may also be able to monitor /dev/input/eventN. This perl module may help.

dj_segfault
Thank you! /dev/input/ looks like somthing close enough to the solution! :)
o_O Tync
A: 

Hi o_O Tync! Could you please say how you solved that task?

Ashcroft
Unfortunately, I have not managed to do it yet :(The only clue I have now is that `sudo cat /dev/input/by-id/usb-*kbd` gives me something when a key is pressed/released or is being held down. I believe it's not worth trying to decode them: my script just tries to read a few bytes from there during a second, and checks whether anything's captured (= whether any key is being held).Detection of key's state seems to be much more complex: something like a C application that uses Xorg API.
o_O Tync
So this works for Shift, too? Somewhere I read, that solely pressing a modifier like Shift wouldn't cause any system events. But then again, xev recognized whether Shift is pressed or released...Anyway, could you please post your code snippet? Strangely, /dev/input/by-id/ on my PC only contains entries for my mouse, but maybe your code can help nevertheless!TIA!On a sidenote: I'm not yet familiar with xev, but maybe it can be used for our purpose. Think of a delay at the script-start during which the release of shift is monitored. Not very elegant, though, and most likely unreliable...
Ashcroft
Yep, it works for any key: /dev/input/* has all hardware events. The fact that a single Shift press actually does nothing is implemented by the OS (or some subsystem): the keyboard still reports that the key was pressed. Concerning the absence of `/dev/input/by-id/usb-*-kbd': your keyboard can have another name there, just try reading from every file in /dev/input and find the one that reports key presses, then find a symlink in one of /dev/input/by-* folders (just to make it simpler).
o_O Tync
Thanks for your reply! I found '/dev/input/event4' and '/dev/input/by-path/platform-i8042-serio-0-event-kbd', but unfortunately reading from /dev/input needs SU rights. No option! =(
Ashcroft