wait till CTRL-C

Welcome Forums General PowerShell Q&A wait till CTRL-C

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #105211

    Participant
    Points: 0
    Rank: Member

    hello everybody 🙂

    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. 🙂

    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:

    while(forever)
    {
    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? 🙂
    and if yes... how? XD...

    thank you already in advance 🙂

    greetings...
    commandrer

  • #105241

    Participant
    Points: 213
    Helping Hand
    Rank: Participant

    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...

    'amazon.com/s/ref=nb_sb_ss_i_4_14?url=search-alias%3Daps&field-keywords=google+hacking+johnny+long+2nd+edition&sprefix=google+hacking%2Caps%2C216&crid=1V6C52I5XBZWH&rh=i%3Aaps%2Ck%3Agoogle+hacking+johnny+long+2nd+edition'

    ... 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.

    Examples:
    Is there a way to catch ctrl-c and ask the user to confirm?
    'stackoverflow.com/questions/10733718/is-there-a-way-to-catch-ctrl-c-and-ask-the-user-to-confirm'

    PowerShell : Using Try / Finally block to catch a Ctrl-C and cleanup
    'kb.kaminskiengineering.com/node/341'

    Trapping CTRL+C in Powershell V2
    'social.technet.microsoft.com/Forums/windowsserver/en-US/fb409048-a607-4895-8ab3-08c2ec656c7a/trapping-ctrlc-in-powershell-v2'

    Handle keyboard interrupt, execute end block
    'stackoverflow.com/questions/20708685/handle-keyboard-interrupt-execute-end-block'

    PowerShell GUIs – How do you make a form "listen" for Ctrl-C sequence?
    'social.technet.microsoft.com/Forums/en-US/a0fdcefe-23a3-4e1b-a182-a7f768a6ee0b/powershell-guis-how-do-you-make-a-form-listen-for-ctrlc-sequence'

  • #105248

    Participant
    Points: 0
    Rank: Member

    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:

    $form.Add_KeyDown({
    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?

  • #105272

    Participant
    Points: 213
    Helping Hand
    Rank: Participant

    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)
    {
        "Processing..."
    
        if ([console]::KeyAvailable)
        {
            $key = [system.console]::readkey($true)
    
            if (($key.modifiers -band [consolemodifiers]"control") -and ($key.key -eq "C"))
            {
                "Terminating..."
                break
            }
        }
    }
    
    # Results - upon pressing CRTL+C you get the terminating message
    Processing...
    Processing...
    Processing...
    Processing...
    Terminating...
    

    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).

    'msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.aspx'

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

    'msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.shift.aspx'

    E.g.:

    $Textbox.Add_Keydown({
        if ($_.KeyCode -eq "Enter" -and $_.Shift) {
            "Working"
        }
    })
    

The topic ‘wait till CTRL-C’ is closed to new replies.