wait till CTRL-C

hello everybody :slight_smile:

i am really looking forword for this post, since i searched google for round about 6 hours now with no match or find.
first off: i am not very good with powershell (or english XD)
so if you could keep your answers as simple as possible, that would be great. :slight_smile:

what i am looking for:
i need a code, that waits until the key combination “ctrl” and “c” is pressed.

i will try to make a quick example what i am trying to create:

wait for ctrl+c
print “hello world!”

i know i know print isn’t rigth… but hopefully you get the idea of the programm.
what i would like to archive is, that everytime i hit ctrl+c, the programm prints out “hello world!”

is this possible? :slight_smile:
and if yes… how? XD…

thank you already in advance :slight_smile:


If you did not find any hits, you are not asking the search engine the right question(s) / the right way. 8^}

For example, using the search string ‘powershell catch crtl+c’ even in Bing brings back a long list of hits of exactly what you are after.

You can always find an answer to almost any question online, but often you need to change the way you ask the question or break it into separate search strings. Since you appear to be a Google fan, this is a book…


… you want to get and leverage until stuff in it is a habit, regardless of search engine you use. It says for pen testing, just ignore that as the techniques are good for searching in general.

Is there a way to catch ctrl-c and ask the user to confirm?
powershell - Is there a way to catch ctrl-c and ask the user to confirm? - Stack Overflow

PowerShell : Using Try / Finally block to catch a Ctrl-C and cleanup

Trapping CTRL+C in Powershell V2
Trapping CTRL+C in Powershell V2

Handle keyboard interrupt, execute end block
powershell - Handle keyboard interrupt, execute end block - Stack Overflow

PowerShell GUIs - How do you make a form “listen” for Ctrl-C sequence?
PowerShell GUIs - How do you make a form "listen" for Ctrl-C sequence?

i will simply ignore the very rude answer from postanote and ask away:
i have managed to get some kind of code to get what i want…
but i am still missing a final thing… the code rigth now is this:

if($_.KeyCode -eq “0x11”)

but it only reacts, when i push “ctrl” and not “ctrl+c”
does anybody know, how i can add another key or know the keycode for the combined ctrl+c order?

Heyyy… wait a minute. What was rude about my response?

I make no effort o be rude to anyone and anytime. It serves no purpose, even if there is cause to be rude. All it dose is unnecessarily escalates a scenario that is not worth the energy. Life is way to short for that kind of nonsense.

Your first post was just a scriptblock and now your response is you are trying this via a form action.

Either way, the links provided, show code examples to do either one.
Secondly, CRTL+C are separate ASCII key combinations, so, you have to pass both as shown in the links provide.

Taking your first post, and the info from the links, if you are in the PS consolehost, is just this…

[console]::TreatControlCAsInput = $true
while ($true)

    if ([console]::KeyAvailable)
        $key = [system.console]::readkey($true)

        if (($key.modifiers -band [consolemodifiers]"control") -and ($key.key -eq "C"))

# Results - upon pressing CRTL+C you get the terminating message

The last link shows how you would approach this using a PS GUI.

Also, verses going the ASCII route, you could also, take a look at the KeyEventArgs Class (that’s the type of argument you’re dealing with here).

KeyEventArgs Class (System.Windows.Forms) | Microsoft Docs

You can use the KeyCode and Shift properties to detect which keys are pushed, and if they’re shifted.

KeyEventArgs.Shift Property (System.Windows.Forms) | Microsoft Docs


    if ($_.KeyCode -eq "Enter" -and $_.Shift) {