Implementation of Getch and Getche functions under Linux System

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.