Reputation: 35463
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 :)
Upvotes: 1
Views: 2962
Reputation: 12449
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.
Upvotes: 1