Create a function to check for key press in unix using ncurses









up vote
14
down vote

favorite
5












I have been looking for an equivalent to kbhit() and I have read several forums on this subject, and the majority seems to suggest using ncurses.



How should I go about checking if a key is pressed in c++ using ncurses.



The function getch() provided by ncurses reads character from the window.
I would like to write a function that only checks if there is a key press and then I want to do getch().



Thanks in advance.










share|improve this question



























    up vote
    14
    down vote

    favorite
    5












    I have been looking for an equivalent to kbhit() and I have read several forums on this subject, and the majority seems to suggest using ncurses.



    How should I go about checking if a key is pressed in c++ using ncurses.



    The function getch() provided by ncurses reads character from the window.
    I would like to write a function that only checks if there is a key press and then I want to do getch().



    Thanks in advance.










    share|improve this question

























      up vote
      14
      down vote

      favorite
      5









      up vote
      14
      down vote

      favorite
      5






      5





      I have been looking for an equivalent to kbhit() and I have read several forums on this subject, and the majority seems to suggest using ncurses.



      How should I go about checking if a key is pressed in c++ using ncurses.



      The function getch() provided by ncurses reads character from the window.
      I would like to write a function that only checks if there is a key press and then I want to do getch().



      Thanks in advance.










      share|improve this question















      I have been looking for an equivalent to kbhit() and I have read several forums on this subject, and the majority seems to suggest using ncurses.



      How should I go about checking if a key is pressed in c++ using ncurses.



      The function getch() provided by ncurses reads character from the window.
      I would like to write a function that only checks if there is a key press and then I want to do getch().



      Thanks in advance.







      c++ unix command-line ncurses






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 26 '10 at 17:08









      ergosys

      34.7k43162




      34.7k43162










      asked Oct 26 '10 at 16:48









      Mimsy Jack

      83116




      83116






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          16
          down vote



          accepted










          You can use the nodelay() function to turn getch() into a non-blocking call, which returns ERR if no key-press is available. If a key-press is available, it is pulled from the input queue, but you can push it back onto the queue if you like with ungetch().



          #include <ncurses.h>
          #include <unistd.h> /* only for sleep() */

          int kbhit(void)

          int ch = getch();

          if (ch != ERR)
          ungetch(ch);
          return 1;
          else
          return 0;



          int main(void)

          initscr();

          cbreak();
          noecho();
          nodelay(stdscr, TRUE);

          scrollok(stdscr, TRUE);
          while (1)
          if (kbhit())
          printw("Key pressed! It was: %dn", getch());
          refresh();
          else
          printw("No key pressed yet...n");
          refresh();
          sleep(1);








          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            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',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2fstackoverflow.com%2fquestions%2f4025891%2fcreate-a-function-to-check-for-key-press-in-unix-using-ncurses%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








            up vote
            16
            down vote



            accepted










            You can use the nodelay() function to turn getch() into a non-blocking call, which returns ERR if no key-press is available. If a key-press is available, it is pulled from the input queue, but you can push it back onto the queue if you like with ungetch().



            #include <ncurses.h>
            #include <unistd.h> /* only for sleep() */

            int kbhit(void)

            int ch = getch();

            if (ch != ERR)
            ungetch(ch);
            return 1;
            else
            return 0;



            int main(void)

            initscr();

            cbreak();
            noecho();
            nodelay(stdscr, TRUE);

            scrollok(stdscr, TRUE);
            while (1)
            if (kbhit())
            printw("Key pressed! It was: %dn", getch());
            refresh();
            else
            printw("No key pressed yet...n");
            refresh();
            sleep(1);








            share|improve this answer
























              up vote
              16
              down vote



              accepted










              You can use the nodelay() function to turn getch() into a non-blocking call, which returns ERR if no key-press is available. If a key-press is available, it is pulled from the input queue, but you can push it back onto the queue if you like with ungetch().



              #include <ncurses.h>
              #include <unistd.h> /* only for sleep() */

              int kbhit(void)

              int ch = getch();

              if (ch != ERR)
              ungetch(ch);
              return 1;
              else
              return 0;



              int main(void)

              initscr();

              cbreak();
              noecho();
              nodelay(stdscr, TRUE);

              scrollok(stdscr, TRUE);
              while (1)
              if (kbhit())
              printw("Key pressed! It was: %dn", getch());
              refresh();
              else
              printw("No key pressed yet...n");
              refresh();
              sleep(1);








              share|improve this answer






















                up vote
                16
                down vote



                accepted







                up vote
                16
                down vote



                accepted






                You can use the nodelay() function to turn getch() into a non-blocking call, which returns ERR if no key-press is available. If a key-press is available, it is pulled from the input queue, but you can push it back onto the queue if you like with ungetch().



                #include <ncurses.h>
                #include <unistd.h> /* only for sleep() */

                int kbhit(void)

                int ch = getch();

                if (ch != ERR)
                ungetch(ch);
                return 1;
                else
                return 0;



                int main(void)

                initscr();

                cbreak();
                noecho();
                nodelay(stdscr, TRUE);

                scrollok(stdscr, TRUE);
                while (1)
                if (kbhit())
                printw("Key pressed! It was: %dn", getch());
                refresh();
                else
                printw("No key pressed yet...n");
                refresh();
                sleep(1);








                share|improve this answer












                You can use the nodelay() function to turn getch() into a non-blocking call, which returns ERR if no key-press is available. If a key-press is available, it is pulled from the input queue, but you can push it back onto the queue if you like with ungetch().



                #include <ncurses.h>
                #include <unistd.h> /* only for sleep() */

                int kbhit(void)

                int ch = getch();

                if (ch != ERR)
                ungetch(ch);
                return 1;
                else
                return 0;



                int main(void)

                initscr();

                cbreak();
                noecho();
                nodelay(stdscr, TRUE);

                scrollok(stdscr, TRUE);
                while (1)
                if (kbhit())
                printw("Key pressed! It was: %dn", getch());
                refresh();
                else
                printw("No key pressed yet...n");
                refresh();
                sleep(1);









                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 26 '10 at 23:58









                Matthew Slattery

                35.3k381105




                35.3k381105



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


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





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2fstackoverflow.com%2fquestions%2f4025891%2fcreate-a-function-to-check-for-key-press-in-unix-using-ncurses%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

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo