Skip to content

Instantly share code, notes, and snippets.

@hyrious
Created July 13, 2020 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyrious/7f37c366e50f2ad9671777df62029ff8 to your computer and use it in GitHub Desktop.
Save hyrious/7f37c366e50f2ad9671777df62029ff8 to your computer and use it in GitHub Desktop.
function Rescue-Interrupt([ScriptBlock]$action = { "^C" }) {
try {
[Console]::TreatControlCAsInput = $true
while ($true) {
if ([Console]::KeyAvailable) {
$key = [Console]::ReadKey($true)
if ($key.Modifiers -band [ConsoleModifiers]::Control) {
if ($key.key -eq "C") {
break
}
}
}
Start-Sleep -Milliseconds 16
}
} finally {
& $action
[console]::TreatControlCAsInput = $false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment