Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Overriding Ctrl-C or Ctrl-Break

Reply
 
Old 01-14-2008   #1 (permalink)
Jon


 
 

Overriding Ctrl-C or Ctrl-Break


As you are probably aware you can halt a script in progress by pressing
ctrl-c, or exit powershell completely by pressing ctrl-break.


What would also be useful would be if you could override that default
behaviour, eg adding say a routine to save command history on exit. I tried
overriding the behaviour with


#--------------------------------------
#Uncomment this next line to see the error message
#powershell

$a = {[console]::WriteLine("Hello")}
[Console]::add_CancelKeyPress({$a})

#Then press ctrl-C or ctrl-break
#--------------------------------------


Pressing ctrl-C or ctrl-break then crashes powershell with an error about
runspaces.


Have also tried a few experiments involving runspaces, although I may well
have missed a few basic concepts here, but still end up with the same error
message.


Any ideas on how to achieve this in Powershell?


Any thoughts or general pointers appreciated. Thanks.

--
Jon



My System SpecsSystem Spec
Old 01-14-2008   #2 (permalink)
Jon


 
 

Re: Overriding Ctrl-C or Ctrl-Break

Correction - included script should have been

#--------------------------------------
#Uncomment this next line to see the error message
#powershell

$a = {[console]::WriteLine("Hello")}
[Console]::add_CancelKeyPress($a)

#Then press ctrl-C or ctrl-break
#--------------------------------------

--
Jon

My System SpecsSystem Spec
Old 01-14-2008   #3 (permalink)
Shay Levi


 
 

Re: Overriding Ctrl-C or Ctrl-Break

Jon,

There is no add_CancelKeyPress static method available on the Console class.
You need to find a way
to interact with console events. Check:

Console.CancelKeyPress Event
http://msdn2.microsoft.com/en-us/lib...lkeypress.aspx

The example code is what you're looking for.



Also, check Oisin's PSEventing project on CodePlex:
http://www.codeplex.com/PSEventing

Start-KeyHandler : Start generating PSEvent objects for break (cltr+c) and/or
keydown events.
Start-KeyHandler [-CaptureCtrlC [<SwitchParameter>]] [-CaptureKeys [<SwitchParameter>]]
[<CommonParameters>]

Stop-KeyHandler : Stop generating PSEvent objects for break (ctrl+c) and/or
keydown events.
Stop-KeyHandler [<CommonParameters>]



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Correction - included script should have been
>
> #--------------------------------------
> #Uncomment this next line to see the error message
> #powershell
> $a = {[console]::WriteLine("Hello")}
> [Console]::add_CancelKeyPress($a)
> #Then press ctrl-C or ctrl-break
> #--------------------------------------

My System SpecsSystem Spec
Old 01-14-2008   #4 (permalink)
Jon


 
 

Re: Overriding Ctrl-C or Ctrl-Break

Good links. Thanks Shay.

I'll have a play with the code, and explore the work that Oisin has done, to
see what can be done with it.

Certainly looks promising....

--
Jon


"Shay Levi" <no@xxxxxx> wrote in message
news:8766a944198398ca24e1d625086e@xxxxxx
Quote:

> Jon,
>
> There is no add_CancelKeyPress static method available on the Console
> class. You need to find a way
> to interact with console events. Check:
>
> Console.CancelKeyPress Event
> http://msdn2.microsoft.com/en-us/lib...lkeypress.aspx
>
> The example code is what you're looking for.
>
>
>
> Also, check Oisin's PSEventing project on CodePlex:
> http://www.codeplex.com/PSEventing
>
> Start-KeyHandler : Start generating PSEvent objects for break (cltr+c)
> and/or keydown events.
> Start-KeyHandler [-CaptureCtrlC [<SwitchParameter>]] [-CaptureKeys
> [<SwitchParameter>]] [<CommonParameters>]
> Stop-KeyHandler : Stop generating PSEvent objects for break (ctrl+c)
> and/or keydown events.
> Stop-KeyHandler [<CommonParameters>]
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
Quote:

>> Correction - included script should have been
>>
>> #--------------------------------------
>> #Uncomment this next line to see the error message
>> #powershell
>> $a = {[console]::WriteLine("Hello")}
>> [Console]::add_CancelKeyPress($a)
>> #Then press ctrl-C or ctrl-break
>> #--------------------------------------
>
>
My System SpecsSystem Spec
Old 01-14-2008   #5 (permalink)
Karl Prosser[MVP]


 
 

Re: Overriding Ctrl-C or Ctrl-Break

This is a difficult subject as the interception of those keys is much
deeper than the average windows window. COnsole windows are strange
beats, and powershell gives total control of the input loop over to one
API until a user presses enter...

You could use Oisins eventing. I know he has hotkey interception going
on, but i presume, and hopefully remember *correctly* that it uses
global hotkey hooks which aren't beneficial

This is something we can intercept in Powershell Plus (in fact we do,
i.e in powershell plus, when nothing is running in the pipeline, CTRL-C
suddenly means Copy , and Ctrl-V always means paste.. However we are
planning on allowing users control over registering hotkeys etc.

Karl Prosser
http://www.powershell.com

Jon wrote:
Quote:

>
> As you are probably aware you can halt a script in progress by pressing
> ctrl-c, or exit powershell completely by pressing ctrl-break.
>
>
> What would also be useful would be if you could override that default
> behaviour, eg adding say a routine to save command history on exit. I
> tried overriding the behaviour with
>
>
> #--------------------------------------
> #Uncomment this next line to see the error message
> #powershell
>
> $a = {[console]::WriteLine("Hello")}
> [Console]::add_CancelKeyPress({$a})
>
> #Then press ctrl-C or ctrl-break
> #--------------------------------------
>
>
> Pressing ctrl-C or ctrl-break then crashes powershell with an error
> about runspaces.
>
>
> Have also tried a few experiments involving runspaces, although I may
> well have missed a few basic concepts here, but still end up with the
> same error message.
>
>
> Any ideas on how to achieve this in Powershell?
>
>
> Any thoughts or general pointers appreciated. Thanks.
>
My System SpecsSystem Spec
Old 01-15-2008   #6 (permalink)
Jon


 
 

Re: Overriding Ctrl-C or Ctrl-Break

"Karl Prosser[MVP]" <karl@xxxxxx_o_w_e_r_s_h_e_l_l.com> wrote in message
news:eKONlXxVIHA.4808@xxxxxx
Quote:

> This is a difficult subject as the interception of those keys is much
> deeper than the average windows window. COnsole windows are strange beats,
> and powershell gives total control of the input loop over to one API until
> a user presses enter...
>
> You could use Oisins eventing. I know he has hotkey interception going on,
> but i presume, and hopefully remember *correctly* that it uses global
> hotkey hooks which aren't beneficial
>
> This is something we can intercept in Powershell Plus (in fact we do, i.e
> in powershell plus, when nothing is running in the pipeline, CTRL-C
> suddenly means Copy , and Ctrl-V always means paste.. However we are
> planning on allowing users control over registering hotkeys etc.
>
> Karl Prosser
> http://www.powershell.com
>



Thanks Karl.

I did finally manage to get it implemented, and you're correct - it proved
to be more complicated than the two-liner I'd originally anticipated - which
isn't to say someone wouldn't be able to come up a far more succinct version
than I did.

Oisin's pages did prove useful and led to me to consider native functions,
principally this one, which also proved to be the key

SetConsoleCtrlHandler Function
http://msdn2.microsoft.com/en-us/library/ms686016.aspx

I believe that that's also the function he uses for the ctrl-c handling in
his cmdlets, with global hotkey hooks for the others.

I got it 50% working based on that (it would crash on every second ctrl-c
press ;-) ), before a bit of Google-searching led me to a few useful dotnet
code examples, which I then modified slightly + translated into PowerShell
script (inline VB) code.

Your hotkeys idea sounds like a good one.

--
Jon



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Remote desktop does not sent ctrl+alt+left or ctrl+alt+right Network & Sharing
Ctrl+C Vista General
Copy (Ctrl+C) and Paste (Ctrl+V) not working Live Mail
Ctrl + Alt + Del Vista General
Ctrl-Alt-Delete / Ctrl-Shift-Esc--------Task Manager Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46