In Linux, how does one implement getch () in DOS ()

Source: Internet
Author: User
In Linux, The getch () mechanism seems to have to press enter to capture the pressed keys, but because I want to press keypad without entering keys, so I have been stuck there with a program that was originally stored in DOS. It was very simple to compile the program. It took a long time in Linux .... -_-"

Finally, we should find the solution method. We need to reset some of terminal's attention to make it available to our functions ....


Program example:

# Include <termios. h>
# Include <unistd. h>
# Include <assert. h>
# Include <string. h>
/*------------------------------------------------*/
Int getch (void ){
Int C = 0;

Struct termios org_opts, new_opts;
Int res = 0;
// ----- Store old settings -----------
Res = tcgetattr (stdin_fileno, & org_opts );
Assert (RES = 0 );
// ---- Set new terminal parms --------
Memcpy (& new_opts, & org_opts, sizeof (new_opts ));
New_opts.c_lflag & = ~ (Icanon | echo | echoe | echok | echonl | echoprt | echoke | icrnl );
Tcsetattr (stdin_fileno, tcsanow, & new_opts );
C = getchar ();
// ------ Restore old settings ---------
Res = tcsetattr (stdin_fileno, tcsanow, & org_opts );
Assert (RES = 0 );
Return (C );
}

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.