Real-time keyboard input to console (in Windows)?
Solution 1
You will be surprised, but this code will do what you want:
/* getchar example : typewriter */
#include <stdio.h>
int main ()
{
char c;
puts ("Enter text. Include a dot ('.') in a sentence to exit:");
do {
c=getchar();
putchar (c);
} while (c != '.');
return 0;
}
Solution 2
C++ has no notion of a "keyboard". It only has an opaque FILE called "stdin" from which you can read. However, the content of that "file" is populated by your environment, specifically by your terminal.
Most terminals buffer the input line before sending it to the attached process, so you never get to see the existence of backspaces. What you really need is to take control of the terminal directly.
This is a very platform-dependent procedure, and you have to specify your platform if you like specific advice. On Linux, try ncurses
or termios
.
Solution 3
You could use ReadConsoleInput
, adding incoming caracters to your list, look for the backspace keys (INPUT_RECORD->KEY_EVENT_RECORD.wVirtualScanCode == VK_BACKSPACE) and removing the last caracter from your list for all of them.
Related videos on Youtube
SE Does Not Like Dissent
Updated on June 04, 2022Comments
-
SE Does Not Like Dissent almost 2 years
I have a doubly-linked list class, where I want to add characters to the list as the user types them, or removes the last node in the list each time the user presses backspace, whilst displaying the results in console in real-time.
What functions would I use to intercept individual keyboard input, and display it in real-time to the console? So the following results:
User starts typing:
Typ_
User stops typing:
Typing this on screen_
User presses backspace 5 times:
Typing this on s_
Particular OS is windows (vista, more specifically).
As a side-note GetAsyncKeyState under windows.h appears to perhaps be for keyboard input, however the issue of real-time display of the console remains.
-
AndersK over 12 yearsprobably good if you could specify OS as well because IO on that level differs between operating systems and there may be more enhanced options.
-
Mooing DuckI don't think this is trivially done. I think you have to effectively remove console input (somehow), listen to the windows message pump for a keydown/up event, process it yourself, and render the console output yourself.
-
-
Kerrek SB over 12 yearsCheers. And good luck -- I have no idea how to access the Windows console. Looking forward to the answer!
-
Kerrek SB over 12 yearsOh, you could try pdcurses. I believe that's available for Windows, and it should be very similar to ncurses (so your program should be fairly portable).
-
SE Does Not Like Dissent over 12 yearsIs there anyway to 'unget' a char from the console display? It has to be logically possible as this is what occurs when you type input for std::cin and press backspace after typing something.
-
E.Freitas over 12 yearsHave you looked at ungetc() ? cplusplus.com/reference/clibrary/cstdio/ungetc
-
SE Does Not Like Dissent over 12 yearsIndeed it does. Thank you very much.