Capture characters from standard input without waiting for enter to be pressed
That's not possible in a portable manner in pure C++, because it depends too much on the terminal used that may be connected with stdin
(they are usually line buffered). You can, however use a library for that:
conio available with Windows compilers. Use the
_getch()
function to give you a character without waiting for the Enter key. I'm not a frequent Windows developer, but I've seen my classmates just include<conio.h>
and use it. Seeconio.h
at Wikipedia. It listsgetch()
, which is declared deprecated in Visual C++.curses available for Linux. Compatible curses implementations are available for Windows too. It has also a
getch()
function. (tryman getch
to view its manpage). See Curses at Wikipedia.
I would recommend you to use curses if you aim for cross platform compatibility. That said, I'm sure there are functions that you can use to switch off line buffering (I believe that's called "raw mode", as opposed to "cooked mode" - look into man stty
). Curses would handle that for you in a portable manner, if I'm not mistaken.
how to get a character from stdin without waiting for user to put it? [duplicate]
ncurses has the capability to do this through it's own getch() function. See this page
#include <curses.h>
int main(void) {
initscr();
timeout(-1);
int c = getch();
endwin();
printf ("%d %c\n", c, c);
return 0;
}
Is there a way to get user input without pressing the enter key? [duplicate]
This works for me (I am on linux):
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
int main()
{
struct termios old_tio, new_tio;
unsigned char c;
/* get the terminal settings for stdin */
tcgetattr(STDIN_FILENO,&old_tio);
/* we want to keep the old setting to restore them a the end */
new_tio=old_tio;
/* disable canonical mode (buffered i/o) and local echo */
new_tio.c_lflag &=(~ICANON & ~ECHO);
/* set the new settings immediately */
tcsetattr(STDIN_FILENO,TCSANOW,&new_tio);
do {
c=getchar();
printf("%d ",c);
} while(c!='q');
/* restore the former settings */
tcsetattr(STDIN_FILENO,TCSANOW,&old_tio);
return 0;
}
It makes the console unbuffered.
reference: http://shtrom.ssji.net/skb/getc.html
How to avoid pressing Enter with getchar() for reading a single character only?
On a linux system, you can modify terminal behaviour using the stty
command. By default, the terminal will buffer all information until Enter is pressed, before even sending it to the C program.
A quick, dirty, and not-particularly-portable example to change the behaviour from within the program itself:
#include<stdio.h>
#include<stdlib.h>
int main(void){
int c;
/* use system call to make terminal send all keystrokes directly to stdin */
system ("/bin/stty raw");
while((c=getchar())!= '.') {
/* type a period to break out of the loop, since CTRL-D won't work raw */
putchar(c);
}
/* use system call to set terminal behaviour to more normal behaviour */
system ("/bin/stty cooked");
return 0;
}
Please note that this isn't really optimal, since it just sort of assumes that stty cooked
is the behaviour you want when the program exits, rather than checking what the original terminal settings were. Also, since all special processing is skipped in raw mode, many key sequences (such as CTRL-C or CTRL-D) won't actually work as you expect them to without explicitly processing them in the program.
You can man stty
for more control over the terminal behaviour, depending exactly on what you want to achieve.
Related Topics
Most Efficient Way to Find the Greatest of Three Ints
Store Hex Value as String (Arduino Project)
The Definitive C++ Book Guide and List
Read File Line by Line Using Ifstream in C++
How to Detect Unsigned Integer Overflow
C++ Returning Reference to Local Variable
How to Find the Length of an Array
Order of Evaluation in C++ Function Parameters
Determine If Two Rectangles Overlap Each Other
How to Use Std::Maps With User-Defined Types as Key
Balanced Array Index While Summing an Array from Left and Right
How to Peek At the Next Element in a Range-For Loop
What Is an Undefined Reference/Unresolved External Symbol Error and How to Fix It
How to Set, Clear, and Toggle a Single Bit
How to Pass Objects to Functions in C++
Why Is Integer Assignment on a Naturally Aligned Variable Atomic on X86
How to Automatically Generate a Stacktrace When My Program Crashes