Is there a way I can check if a specific key is pressed in a shell script which is running in background. Consider I am running a shell script which has to play a play list of songs as soon as I press a key or combinations of 2 or 3 keys. This shell script will be added to crontab , so it won't be running in terminal. So is it possible to capture the keys pressed in this shell script?

  • 1
    I'm not quite sure how you would pass on an argument to a background process. You could probably create an alias and call t play, so when you type in play, it invokes the command. Here are some similar questions i found - stackoverflow.com/questions/9731281/… and unix.stackexchange.com/questions/179191/…. – rahul May 23 '15 at 15:03
  • I am looking kind of sticky keys, like if you press Ctrl+alt+del in windows , it is detected and lock screen, password change etc. Options screen appears. I actually want to create a playlist of songs and play it after I press Ctrl+d for example . – Deepak K M May 23 '15 at 15:09
  • How should a background shell script watch the keyboard, when the shell running in foreground is handling it? This might work with an executable. – ott-- May 23 '15 at 18:45
  • 3
    Do you mean that your shell script should react when it is running in a terminal and the user presses a key in this terminal? Or when the script is running in the background and the user presses the key anywhere in the GUI no matter which window is focused at the time? Or is this a passive check, where you want to know whether a key is pressed but you don't need to be notified when the key is pressed? Is that in X11 (i.e. in a GUI), or in a text mode console? – Gilles 'SO- stop being evil' May 23 '15 at 22:04
  • I want to capture the key pressed anywhere in the GUI , I want to cron this shell script at reboot. It won't be running in terminal. – Deepak K M May 24 '15 at 1:53

Your requirements are not very much clear. But if I understand correctly, I would suggest you to first try out autokey-gtk. It is a python based application in which you can write your scripts in Python which will be executed whenever you press a pre-defined set of keys. See if this fits your need.

Then, if you want to do manually, there are essentially two ways:

Solution#1 : Keylogger Approach

You can write a script which would keep on logging your keyboard keys and initiate an event whenever a certain combination is pressed. For logging keys, you could look at the code of pykeylogger.

Solution#2 : Keybinder Approach

First of all you need to bind your keys using some method. One of the methods is using keybinder. Or you could simply bind your keys using "Keyboard Shortcuts" in your settings. Now, whenever you press a certain set of keys, a script would run which will write some text into a certain XYZ file in you home folder.

In the background, your script which was started using crontab would be reading this XYZ file every 1 second. According to the content of the file, this script will initiate different actions.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.