Checking if a key is pressed using the gcc compiler in windows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kulaanthenord
    New Member
    • Nov 2014
    • 3

    Checking if a key is pressed using the gcc compiler in windows

    So my teacher is having us use the cygwin gcc compiler to program things in c, but the gcc compiler doesn't have conio.h included in it nor does it seem to have curses.h. I am just wondering if there is a simple method to either create my own kbhit function or just include a specific header file other than the two I mentioned earlier. Thanks
  • divideby0
    New Member
    • May 2012
    • 131

    #2
    There is no standard way of replicating kbhit, getch, etc as the standard input functions are buffered and block while waiting on user input.

    I remember reading an article somewhere where buffering could be turned off, but it's not portable. Try milling over the results from google and see if any will work for your situation.

    Good luck

    Comment

    • kulaanthenord
      New Member
      • Nov 2014
      • 3

      #3
      Alright. I've been googling for about an hour so far with no luck as of yet. Thanks for the information though.

      Comment

      • kulaanthenord
        New Member
        • Nov 2014
        • 3

        #4
        So I figured out a way to do it.

        Basically, making my own getch() function in which it calls a few system commands.

        Code:
        char getch();
        
        char getch()
        {
            //Gets the raw input
            system( "stty raw" );
            //Sets the buffer to not echo onto the screen
            system( "stty -echo" );
            //gets the input
            char input = getchar();
            //resets the input
            system( "stty cooked" );
            //sets the buffer to echo
            system( "stty echo" );
            return input;
        }
        This is definitely not the way I would wish to perform this task, however, it works.
        Last edited by Rabbit; Nov 20 '14, 05:07 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

        Comment

        Working...