Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8626997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:09:52+00:00 2026-06-12T08:09:52+00:00

Possible Duplicate: What is equivalent to getch() & getche() in Linux? I’m a newbie

  • 0

Possible Duplicate:
What is equivalent to getch() & getche() in Linux?

I’m a newbie in linux programming. 🙂

I just wonder if my program (using C language) can capture every single key stroke immediately in linux, and then decide whether to echo it or not and how to display it, just like telnet.

for example, if a user strikes ‘A’, I want the program to display ‘B’, if he types ‘B’, ‘C’ is wanted, and so on.

It sounds funny and useless. I am just curious about it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:09:53+00:00Added an answer on June 12, 2026 at 8:09 am

    Basically, it depends heavily on how you define immediately.

    There are two tasks here. The first is to disable the regular key echoing that is built into most C input libraries. The second is to print out the new character instead of the old one.

    In pseudo code.

     echo(off);
     while (capturing && charIsAvailable()) {
       c = readOneChar();
       if (c == '\n') {
         capturing = false;
       }
       printf("%c", c++);
     }
     echo(on);
    

    There are a number of systems communicating to capture a key press.

    1. The keyboard
    2. (possibly) a USB bus
    3. The CPU interrupt handler
    4. The operating system
    5. The X window server process
    6. The X “window” that has focus.

    The last step is done with a program that runs a continuous loop that captures events from the X server and processes them. If you wanted to expand this program in certain ways (get the length of time the key was pressed) you need to tell the other programs that you want “raw” keyboard events, which means that you won’t really be receiving fully “cooked” characters. As a result, you will have to keep track of which keys are up and down, and how long, and handle all the odd meta key behavior in your program (that’s not an ‘a’ it’s a ‘A’ because shift is down, etc).

    There are also other processing modes to consider, like canonical and non-canonical, which will control whether you wish the events to be received in line oriented chunks (line events) or character oriented chunks (character events). Again this is somewhat complicated by the need to make the upstream programs aware of the requirements of the downstream client.

    Now that you have some idea of your environment, let’s revisit the actual code needed to suppress character output.

    // define a terminal configuration data structure
    struct termios term;
    
    // copy the stdin terminal configuration into term
    tcgetattr( fileno(stdin), &term );
    
    // turn off Canonical processing in term
    term.c_lflag &= ~ICANON;
    
    // turn off screen echo in term
    term.c_lflag &= ~ECHO;
    
    // set the terminal configuration for stdin according to term, now
    tcsetattr( fileno(stdin), TCSANOW, &term);
    
    
    (fetch characters here, use printf to show whatever you like)
    
    // turn on Canonical processing in term
    term.c_lflag |= ICANON;
    
    // turn on screen echo in term
    term.c_lflag |= ECHO;
    
    // set the terminal configuration for stdin according to term, now
    tcsetattr( fileno(stdin), TCSANOW, &term);
    

    Even this is not immediate. To get immediate, you need to get closer to the source, which eventually means a kernel module (which still isn’t as immediate as the keyboard micro-controller, which isn’t as immediate as the moment the switch actually closes). With enough items in between the source and the destination, eventually it becomes possible to notice the difference, however, in practice this code has been worked on a lot by people who are seeking the best tradeoff between performance and flexibility.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to implement getch() function of C in Linux? What is the
Possible Duplicate: JavaScript equivalent to printf/string.format How can I create a Zerofilled value using
Possible Duplicate: what is the git equivalent for revision number? I have been using
Possible Duplicate: How to decode HTML entities using jQuery? HtmlSpecialChars equivalent in Javascript? I
Possible Duplicate: iif equivalent in c# I have several lines of code using IIf
Possible Duplicate: remove_if equivalent for std::map Yesterday i wrote a program, which use multiset
Possible Duplicate: What is the equivalent of the C# “using” block in IronPython? I'm
Possible Duplicate: Programmatically using tab character in .NET? Is there an equivalent value for
Possible Duplicate: Equivalent of SQL ISNULL in LINQ? I am recently migrated from ADO.Net
Possible Duplicate: Alphabetic equivalent of PHP is_numeric If you check if the data is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.