The first thing to know: Getch and Getche is in conio.h this header file, but, but conio.h this file is not the C language standard library file, so under the Linux gcc is not this header file, and most of us use Getch and Getche function, because the GetChar function of the It's not very powerful, so let's start by instilling some knowledge about Linux.
Stty is a common command used to change and print terminal line settings.
1, at the command line, suppress the output capitalization method
Stty-icanon set a one-time read operation, such as using the GetChar () read operation, do not need to press ENTER
Stty Icanon Cancel the above settings
Stty-a View information
Stty-echo Settings command is not displayed
Stty echo Cancellation does not display settings
Stty Raw-echo
Stty Raw Echo
Stty IUCLC #开启
STTY-IUCLC #恢复
2, suppress the output lowercase at the command line
Stty OLCUC #开启
stty-olcuc# Recovery
3, hit the first to know: Getch and Getche is in the conio.h this header file, but, but print out the terminal's number of rows and columns
Stty size
4, change the method of Ctrl+d:
Stty EOF "string"
The system defaults to Ctrl+d to represent the end of the file, which can be changed by this method!
5, Shielding display
Stty-echo #禁止回显
Stty Echo #打开回显
Test method:
Stty-echo;read;stty Echo;read
6, ignore carriage return character
Stty IGNCR #开启
stty-igncr# Recovery
7. Timing input
Timeout_read ()
{
Timeout=$1
old_stty_settings= ' stty-g ' #save current settings
Stty-icanon min 0 Time #set 10seconds,not 100seconds
Eval Read VarName #=read $varname
Stty "$old _stty_settings" #recover settings
}
A simpler approach is to take advantage of the-t option of the Read command
Read-t varname
Maybe it's a bit more than that, so start with the code, notice, the dry goods are coming.
Char Getch ()
{
char c;
System ("Stty-echo");
System ("Stty-icanon");
C=getchar ();
System ("Stty Icanon");
System ("Stty echo");
REUTRN C;
}
Char Getche ()
{
char c;
System ("Stty-icanon");
C=getchar ();
System ("Stty Icanon");
return C;
}
OK, you can write the above code directly with an. h file and you can use it directly, thank you.
Implementation of Getch and Getche functions under Linux System