Capture Characters from Standard Input Without Waiting For Enter to Be Pressed

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:

  1. 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. See conio.h at Wikipedia. It lists getch(), which is declared deprecated in Visual C++.

  2. curses available for Linux. Compatible curses implementations are available for Windows too. It has also a getch() function. (try man 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



Leave a reply



Submit