Checking if a specific key is pressed in shell scriptbashscript to detect right arrow key being pressedexit...

How to securely dispose of a smartphone?

What is this mount with two buttons on side of Vivitar 75-205mm lens?

Are all commands with an optional argument fragile?

How is this practical and very old scene shot?

How could a satellite follow earth around the sun while staying outside of earth's orbit?

"Vector quantity" --More than two dimensions?

What is "oversubscription" in Networking?

Can a nowhere continuous function have a connected graph?

Just graduated with a master’s degree, but I internalised nothing

Why wasn't EBCDIC designed with contiguous alphanumeric characters?

Converting Geographic Coordinates into Lambert2008 coordinates

How did researchers find articles before the Internet and the computer era?

Sharing referee/AE report online to point out a grievous error in refereeing

Does a Hand Crossbow with the Repeating Shot Infusion still require a Free Hand to use?

How do I ensure my employees don't abuse my flexible work hours policy?

Why wasn't ASCII designed with a contiguous alphanumeric character order?

Grant dbcreator only for databases matching prefix

How to properly say asset/assets in German

Most important new papers in computational complexity

How receiver knows the exact frequency in the channel to "listen to"?

Security Patch SUPEE-11155 - Possible issues?

Can one use the present progressive or gerund like an adjective?

Can a stressful Wish's Strength reduction be cured early by a Greater Restoration spell?

How do I organize members in a struct to waste the least space on alignment?



Checking if a specific key is pressed in shell script


bashscript to detect right arrow key being pressedexit shell script from a subshellSourcing a shell script from within EmacsSupply input to a program with a password promptWait for key in shell script that may get piped to /bin/bashIs “wait &” (“wait ampersand”) a useful (bash) shell / shell script idiom or technique?How to bind bash script to a key combination?Starting an interactive shell as an asynchronous process (signal delivery)How to write script for checking Error jobs and auto resubmit?Using a shell script with jstest, how can I get a gamepad to interact with my script?Is it possible to use commands like `fg` in a shell-script?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







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?










share|improve this question
















bumped to the homepage by Community 58 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 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
    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


















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?










share|improve this question
















bumped to the homepage by Community 58 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 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
    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














4












4








4


2






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?










share|improve this question
















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?







shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 24 '15 at 13:59









Braiam

24.2k20 gold badges81 silver badges145 bronze badges




24.2k20 gold badges81 silver badges145 bronze badges










asked May 23 '15 at 13:39









Deepak K MDeepak K M

591 gold badge1 silver badge8 bronze badges




591 gold badge1 silver badge8 bronze badges





bumped to the homepage by Community 58 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 58 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 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
    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














  • 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
    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








1




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'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





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





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




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
May 23 '15 at 22:04







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
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





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










1 Answer
1






active

oldest

votes


















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.






share|improve this answer




























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f205223%2fchecking-if-a-specific-key-is-pressed-in-shell-script%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer






























      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.






      share|improve this answer




























        0












        0








        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 27 '15 at 5:11

























        answered May 27 '15 at 5:02









        shivamsshivams

        3,0061 gold badge14 silver badges28 bronze badges




        3,0061 gold badge14 silver badges28 bronze badges






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f205223%2fchecking-if-a-specific-key-is-pressed-in-shell-script%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

            Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

            Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...