4

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?

Braiam
  • 35,991
  • 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 - https://stackoverflow.com/questions/9731281/bash-script-listen-for-key-press-to-move-on and https://unix.stackexchange.com/questions/179191/bashscript-to-detect-right-arrow-key-being-pressed. – 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 01:53
  • Why not simply logging the x server events? – Braiam May 24 '15 at 13:58
  • How do you plan to handle different users logging in to the machine? – jlliagre May 24 '15 at 20:40
  • Sorry folks, I am new to linux, so I will look into what are x server events, and impact of different users logging into the machine and get back after realising what exactly I want. – Deepak K M May 27 '15 at 04:56
  • @newbie_123 as far as I can understand your requirements, autokey-gtk should solve it. Also look at my answer. – shivams May 27 '15 at 05:03
  • A shell script will not be able to pick up GUI events. You'll need to interact with the GUI for that. – Shadur-don't-feed-the-AI Aug 14 '23 at 10:09

1 Answers1

0

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.

shivams
  • 4,565