Basically, it depends a lot on how you determine right away.
There are two tasks here. First off, turn off the regular echo built into most input libraries. The second is to print a new character instead of the old.
In pseudo code.
echo(off); while (capturing && charIsAvailable()) { c = readOneChar(); if (c == '\n') { capturing = false; } printf("%c", c++); } echo(on);
There are several reporting systems for fixing keystrokes.
- Keyboard
- (possibly) USB bus
- CPU interrupt handler
- operating system
- Window Server Process X
- X-window in which there is focus.
The last step is performed using a program that starts a continuous loop that captures events from the X server and processes them. If you want to deploy this program in certain ways (get the time, press a key), you need to tell other programs that you want "raw" keyboard events, which means that you really wonβt receive fully "cooked" "characters." you will need to keep track of which keys are up and down, and for how long, and process all the odd meta-key actions in your program (this is not βaβ this is βAβ because it is a down shift, etc.).
There are other processing modes, for example, canonical and noncanonical, which will control whether you want events to be received in lines oriented to the line (events of the line) or character-oriented fragments (symbolic events). Again, this is somewhat complicated by the need to inform upstream programs about the requirements of the client listed below.
Now that you have an idea of ββyour environment, let me re-examine 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);
Not even that right away. To get immediate access, you need to approach the source, which ultimately means the kernel module (which is still not as direct as the keyboard microcontroller, which is not as close as the moment the switch closes). If there is a sufficient number of objects between the source and the recipient, in the end it becomes possible to notice the difference, however, in practice, this code has worked hard for people who are looking for the best compromise between performance and flexibility.