Re: std::cin and disabling canonical line processing (buffering)



Tom,

Oh, this is *so* close!

I find that I have to set the console mode to 0 rather than
ENABLE_PROCESSED_INPUT in order to get a ^C char -- as expected. No surprise
there.

And the text-binary translation is now gone, which is great! As an aside, I
also found that using:

_setmode(0, _O_BINARY)

turned off the text translation, but this is clearly an unreliable hack and
I prefer your method of opening up the console in binary mode from the get-go.

But, (and you *knew* this was coming!):

I cannot read the ESC character; it gets silently eaten. My suspicion is
that since ESC forms the prefix of several "special" keys that it is being
treated as part of a special sequence, such as:

// Up Arrow ESC [ A
// Down Arrow ESC [ B
// Right Arrow ESC [ C
// Left Arrow ESC [ D
// Home ESC [ 1 ~
// Insert ESC [ 2 ~
// Delete ESC [ 3 ~
// End ESC [ 4 ~
// PageUp ESC [ 5 ~
// PageDn ESC [ 6 ~

I would have thought that by NOT enabling mouse or window input modes that
these would be passed through raw. Apparently not. And just for grins, I
tried it with those modes enabled and still cannot get an ESC char.

Ideas or suggestions?

Thanks so much for your help!

-- Harold

"Tom Widmer [VC++ MVP]" wrote:

> I don't know a way to disable the newline filtering of the std::cin
> streambuf, but you can create one that doesn't have that filtering enabled:
>
> #include <windows.h>
> #include <iostream>
> #include <fstream>
>
> int main()
> {
> HANDLE inputHandle = GetStdHandle(STD_INPUT_HANDLE);
> BOOL b = SetConsoleMode(inputHandle, ENABLE_PROCESSED_INPUT);
>
> std::ifstream ifs("CONIN$", std::ios::binary);
> char c;
> while(ifs.get(c))
> {
> if (c == 'x')
> break;
> std::cout << int(c) << std::endl;
> }
>
> //and if you want you can change std::cin to this behaviour:
> std::streambuf* oldbuf = std::cin.rdbuf(ifs.rdbuf());
> while(std::cin.get(c))
> {
> if (c == 'x')
> break;
> std::cout << int(c) << std::endl;
> }
>
> std::cin.rdbuf(oldbuf);
> }
>
> Hope that helps.
>
> Tom
>
.


Loading