Recently, I had a requirement to update my script console output
depending on user key-press. Since calculation was done by
background threads, I also wanted to prevent CTRL+C from stopping
the script without proper cleanup.
However, looks like seamless handling of key-press along with
handling special sequences is not available in Powershell. This
was to be expected given that [system.console]::readkey is
designed for accepting the user input which is mostly answers to
flow control questions. Register-EngineEvent does not help
either.
Anyway, let's tackle both problems one by one:
1) Disable CTRL+C from stopping the script:
[console]::TreatControlCAsInput = $true
Note that CTRL+BREAK will end the session entirely and do the …
[Read more]