0

I have written a C console program meant to check the key pressed at any time the program is running. However I have to press ENTER each time to process the input from the keyboard. How to get the key pressed each time without having to press the ENTER key ? Here is what I have so far:

# include <stdio.h>
#include <stdlib.h>

#define RUNNING 1


int main()
{   printf("---------------------------------------------------------
 -------------\n\n");
    puts("Welcome to PUSH COUNTER appliction  :)");
    puts("Type any key to test here ...");
    printf("---------------------------------------------------------
 -------------\n\n");
    while (RUNNING){

    char c = getchar();

    if(c != ' ') {
        printf("Not a space button... Program listening for push button .\n\n");
    }
    else {
        printf("Finally pressed a space bar. Updating counter now ...\n");
        }
    }
     return(0);

}
codigomonstruo
  • 1,081
  • 1
  • 11
  • 45
  • This is OS-specific. What operating system? – Barmar May 05 '17 at 16:59
  • I am using a mac OSX – codigomonstruo May 05 '17 at 17:01
  • 1
    platform dependent...in windows would be imported. I think in mac it is curses.h – Yusuf Jama May 05 '17 at 17:09
  • On Unix systems, you have to put the terminal into 'raw' (or perhaps 'cbreak') mode — which is not entirely trivial — and then arrange that your program resets the original mode when the program exits. Using a library such as the curses library can simplify that, at a cost in overhead. Or you can get into the `tc*` functions and/or `` (for POSIX) to play with these properties. See [Canonical vs non-canonical terminal input](http://stackoverflow.com/questions/358342/) for some information about how this works. – Jonathan Leffler May 05 '17 at 18:20
  • The duplicate ([How to handle key pressed in a Linux console](http://stackoverflow.com/questions/2984307/)) is tagged 'Linux' but it applies to POSIX systems reasonably generally. – Jonathan Leffler May 05 '17 at 18:22

0 Answers0