Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read input from console in OSX

I'm using scanf() to read user input on terminal in a console application. scanf waits until the user hits the return key to read. Is there a way to read the user input on each keystroke?

like image 678
the Reverend Avatar asked Jul 22 '11 22:07

the Reverend


People also ask

How do you access the console on a Mac?

Open Terminal On your Mac, do one of the following: Click the Launchpad icon in the Dock, type Terminal in the search field, then click Terminal. In the Finder , open the /Applications/Utilities folder, then double-click Terminal.

How do I capture a Terminal output on a Mac?

If you want to save the output of all the commands and their results for the current session in a text file on the macOS, you can do that by simply pressing Command + S keys, this will open a prompt that will ask you where you want to save the file to.

Can you use CLI on Mac?

On Linux or macOS, you can use the bundled installer to install version 1 of the AWS Command Line Interface (AWS CLI).

How do I view a text file in Mac Terminal?

To look at the contents of a text-based configuration file, use cat or less . Generally, you'll use less because it has more options (such as searching). To use less , enter the command name followed by the name of the file you want to view.


2 Answers

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.

like image 64
Jerry Coffin Avatar answered Sep 28 '22 12:09

Jerry Coffin


getch() returns the character stream from stdin as it is typed.

like image 28
Paul Sonier Avatar answered Sep 28 '22 12:09

Paul Sonier