Read input from console in OSX
I'm using scanf()
to read user input on terminal in a console application. scanf waits开发者_StackOverflow中文版 until the user hits the return key to read. Is there a way to read the user input on each keystroke?
The usual way would be to use the getch
function from (the Mac port of) ncurses.
Note that while getchar
reads a single character, it still normally does buffered reading, so you need to press 'return'/'enter' before it'll return.
getch()
returns the character stream from stdin as it is typed.
char c = getchar();
It should do the trick.
精彩评论