0

I am currently using read(0, &c, 1) to read a single character from stdin however the character does not appear to be processed until I either hit enter or ^D. What is a good way to read a keystroke and store it?

I have tried fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK) but the characters still require an enter or ^D to be processed.

halfer
  • 19,824
  • 17
  • 99
  • 186
Theo Walton
  • 1,085
  • 9
  • 24
  • 1
    Using standard C you can't. What you're doing is OS-specific (more specifically POSIX-specific) and you only make `STDIN_FILENO` non-blocking, it doesn't actually change the *terminal* which handles a lot of the input and buffering itself. You have to make the terminal itself nonblocking, which is done with the [`tcsetattr`](http://pubs.opengroup.org/onlinepubs/009695399/functions/tcsetattr.html) function. – Some programmer dude Jul 21 '17 at 07:23
  • 1
    Or you could use [ncurses](https://en.wikipedia.org/wiki/Ncurses) – r3mainer Jul 21 '17 at 07:24
  • thanks reading that thread now – Theo Walton Jul 21 '17 at 07:30
  • @TheoWalton> it's nice you wrote your problem is solved. Typically on SO, if it's solved because another answer fully answers it, you may just delete the question. Or the community will mark it as a duplicate and it will be put on hold. – spectras Jul 21 '17 at 07:33

0 Answers0