Taking Input in c program without pressing enter IIT Bombay Problem Statement While giving input to c or c++ programs, enter/return key has to be pressed. Some programs require keyboard recognition and ability to give input without pressing enter/return key. Proposed Solutions The operating system used is Ubuntu 12.041 and IDE used is Code::Blocks2 . The programs written and tested are console application projects. Link for Code::Block manual is given in reference [1]. Below we have discussed some of the method to take input in c/c++ programs without pressing enter/return key. 1. Modifying terminal behaviour using stty command In this method, stty is used to modify terminal behaviour. Terminal buffers all information until enter/return key is pressed. When the enter/return key is pressed the buffered information is sent to the c program. This buffering can be suppressed using ssty and input will be sent to c program as soon as key is pressed. The code used is [2] 1 #include<stdio.h> 2 3 int main(void){ 4 int c; 5 /* use system call to make terminal send all keystrokes directly to stdin */ 6 system ("/bin/stty raw"); 1 TM R Ubuntu 12.04 with intel Core i3-2120 CPU @ 3.30GHzx4 processor 4 GB RAM and 32-bit architecture and 64 bit architecture. 2 Version:10.05 1 7 8 9 10 11 12 13 14 } 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; Listing 1: Using stty command This program does the check for users default terminal behaviour and assumes after program exits the behaviour expected is ‘cooked’ [3]. In raw mode many key sequence like CTRL-C or CTRL-D won’t work with explicitely processing them in the program. Further information on stty can be obtained using ‘man stty’ 2. Disabling ICANON flag In unix like environment ICANON flag is enabled by default. ICANON flag enables input to be buffered until ‘\n’ or EOF os encountered. Thus enter/return has to be pressed to give input to programs. By disabling the canonical mode [4] characters are taken as soon as some key is pressed. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include<stdio.h> #include <termios.h> #include <unistd.h> //termios, TCSANOW, ECHO, ICANON //STDIN_FILENO int main(void){ int c; static struct termios oldt, newt; /* tcgetattr gets the parameters of the current terminal STDIN_FILENO will tell tcgetattr that it should write the settings of stdin to oldt */ tcgetattr( STDIN_FILENO, &oldt); /*now the settings will be copied*/ newt = oldt; /* ICANON normally takes care that one line at a time will be processed that means it will return if it sees a "\n" or an EOF or an EOL */ newt.c_lflag &= ~(ICANON); /* Those new settings will be set to STDIN TCSANOW tells tcsetattr to change attributes immediately. */ tcsetattr( STDIN_FILENO, TCSANOW, &newt); 2 19 20 21 22 23 24 25 26 27 28 29 } /* This is your part: I choose ’e’ to end input. Notice that EOF is also turned off in the non-canonical mode */ while((c=getchar())!= ’e’) putchar(c); /* restore the old settings */ tcsetattr( STDIN_FILENO, TCSANOW, &oldt); return 0; Listing 2: Disabling ICANON flag When above code is executed the output is echoed as soon as some key is pressed. Echo can be suppressed by disabling echo flag. Echo flag can be disabled by changing line 15 in listing 2 as given in listing 3 1 newt.c_lflag &= ~(ICANON | ECHO); Listing 3: Disabling echo References [1] IIT Bombay. Code::block manual http://www.it.iitb.ac.in/frg/wiki/images/e/e8/ CodeBlockManual.pdf. [2] Stack Overflow. c - how to avoid press enter with any getchar() - stack overflow http://stackoverflow.com/questions/1798511/ how-to-avoid-press-enter-with-any-getchar. [3] Wikipedia. Posix terminal interface - wikipedia, the free encyclopedia http://en.wikipedia.org/wiki/POSIX_terminal_interface. [4] W. Richard Stevens and Stephen A. Rago. Advanced Programming in the UNIXr Environment http: // infohost. nmt. edu/ ~ eweiss/ 222_ book/ 222_ book/ 0201433079/ ch18lev1sec10. html . Addition Wesley Professional, second edition, June 17 2005. 3