[PR #414] ReadConsoleInputStream demo #24113

Open
opened 2026-01-31 09:01:28 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/microsoft/terminal/pull/414

State: closed
Merged: Yes


This is a demo that shows how we can have a stream-oriented view of characters from the console
while also listening to console events like mouse, menu, focus, buffer/viewport(1) resize events.

This has always been tricky to do because ReadConsoleW/A doesn't allow retrieving events.
Only ReadConsoleInputW/A returns events, but isn't stream-oriented. Using both doesn't work because
ReadConsoleW/A flushes the input queue, meaning calls to ReadConsoleInputW/A will wait forever.
I do this by deriving a new Stream class which wraps ReadConsoleInputW and accepts a provider/consumer implementation of BlockingCollection<Kernel32.INPUT_RECORD>. This allows asynchronous monitoring of console events while simultaneously streaming the character input. I also use Mark Gravell's great System.IO.Pipelines utility classes (2) and David Hall's excellent P/Invoke wrappers (3) to make this demo cleaner to read; both are pulled from NuGet.

(1) in versions of windows 10 prior to 1809, the buffer resize event only fires for enlarging
the viewport, as this would cause the buffer to be enlarged too. Now it fires even when
shrinking the viewport, which won't change the buffer size.

(2) https://github.com/mgravell/Pipelines.Sockets.Unofficial
https://www.nuget.org/packages/Pipelines.Sockets.Unofficial

(3) https://github.com/dahall/Vanara
https://www.nuget.org/packages/Vanara.Pinvoke.Kernel32

Oisin Grehan - 2019/4/21
https://twitter.com/oising
https://github.com/oising

**Original Pull Request:** https://github.com/microsoft/terminal/pull/414 **State:** closed **Merged:** Yes --- This is a demo that shows how we can have a stream-oriented view of characters from the console while also listening to console events like mouse, menu, focus, buffer/viewport(1) resize events. This has always been tricky to do because ReadConsoleW/A doesn't allow retrieving events. Only ReadConsoleInputW/A returns events, but isn't stream-oriented. Using both doesn't work because ReadConsoleW/A flushes the input queue, meaning calls to ReadConsoleInputW/A will wait forever. I do this by deriving a new Stream class which wraps ReadConsoleInputW and accepts a provider/consumer implementation of BlockingCollection<Kernel32.INPUT_RECORD>. This allows asynchronous monitoring of console events while simultaneously streaming the character input. I also use Mark Gravell's great System.IO.Pipelines utility classes (2) and David Hall's excellent P/Invoke wrappers (3) to make this demo cleaner to read; both are pulled from NuGet. (1) in versions of windows 10 prior to 1809, the buffer resize event only fires for enlarging the viewport, as this would cause the buffer to be enlarged too. Now it fires even when shrinking the viewport, which won't change the buffer size. (2) https://github.com/mgravell/Pipelines.Sockets.Unofficial https://www.nuget.org/packages/Pipelines.Sockets.Unofficial (3) https://github.com/dahall/Vanara https://www.nuget.org/packages/Vanara.Pinvoke.Kernel32 Oisin Grehan - 2019/4/21 https://twitter.com/oising https://github.com/oising
claunia added the pull-request label 2026-01-31 09:01:28 +00:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#24113