Search the FAQ Archives

3 - A - B - C - D - E - F - G - H - I - J - K - L - M
N - O - P - Q - R - S - T - U - V - W - X - Y - Z
faqs.org - Internet FAQ Archives

Unix - Frequently Asked Questions (4/7) [Frequent posting]
Section - How do I read characters ... without requiring the user to hit RETURN?

( Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Single Page )
[ Usenet FAQs | Web FAQs | Documents | RFC Index | Neighborhoods ]


Top Document: Unix - Frequently Asked Questions (4/7) [Frequent posting]
Previous Document: News Headers
Next Document: How do I check to see if there are characters to be read ... ?
See reader questions & answers on this topic! - Help others by sharing your knowledge

4.1)  How do I read characters from a terminal without requiring the user
      to hit RETURN?

      Check out cbreak mode in BSD, ~ICANON mode in SysV.

      If you don't want to tackle setting the terminal parameters
      yourself (using the "ioctl(2)" system call) you can let the stty
      program do the work - but this is slow and inefficient, and you
      should change the code to do it right some time:

      #include <stdio.h>
      main()
      {
            int c;

            printf("Hit any character to continue\n");
            /*
             * ioctl() would be better here; only lazy
             * programmers do it this way:
             */
            system("/bin/stty cbreak");        /* or "stty raw" */
            c = getchar();
            system("/bin/stty -cbreak");
            printf("Thank you for typing %c.\n", c);

            exit(0);
      }

      Several people have sent me various more correct solutions to
      this problem.  I'm sorry that I'm not including any of them here,
      because they really are beyond the scope of this list.

      You might like to check out the documentation for the "curses"
      library of portable screen functions.  Often if you're interested
      in single-character I/O like this, you're also interested in
      doing some sort of screen display control, and the curses library
      provides various portable routines for both functions.

User Contributions:

Comment about this article, ask questions, or add new information about this topic:

CAPTCHA




Top Document: Unix - Frequently Asked Questions (4/7) [Frequent posting]
Previous Document: News Headers
Next Document: How do I check to see if there are characters to be read ... ?

Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Single Page

[ Usenet FAQs | Web FAQs | Documents | RFC Index ]

Send corrections/additions to the FAQ Maintainer:
tmatimar@isgtec.com (Ted Timar)





Last Update March 27 2014 @ 02:12 PM