Discussion:
V2 CTP3: How can I intercept Control-C using the new eventing feature?
(too old to reply)
Chuck Heatherly
2009-05-26 15:29:35 UTC
Permalink
For PowerShell 1.0, it was advised to use Oisin Grehan's PSEventing
toolkit to capture Control-C events. The page for this toolkit,
http://www.codeplex.com/PSEventing, now says that it is intended for
use with PowerShell 1.0 only, that PowerShell 2.0 CTP2 has introduced
support for eventing.

Could someone provide a code sample for V2 CTP2 or CTP3 that shows how
to intercept Control-C? I want my script to be able to do some final
operations in the event that Control-C is pressed, like properly
finalize XML files (write the closing tags).

Thanks,
Chuck
Bob Landau
2009-05-27 15:54:01 UTC
Permalink
Documentation is sparse however this is the cmdlet to intercept CTLR-C

It will not swallow the key but that doesn't appear to be what you're after

Register-ObjectEvent -InputObject ([Console]) -EventName CancelKeyPress
-Action { write-host 'CTRL-C pressed'}

To remove this use UnregisterEvent -SubscriptionId <id>

where the id is returned from the above call, Get-EventSubscriber or if a
variable.

Here is some more info

http://technet.microsoft.com/en-us/library/dd347672.aspx

as well as Lee's blog

http://blogs.msdn.com/powershell/archive/2008/06/11/powershell-eventing-quickstart.aspx


As Lee said the parameters have changed. If you're cursious I can did up
some old code I wrote showing how this works in CTP3.

bob
Post by Chuck Heatherly
For PowerShell 1.0, it was advised to use Oisin Grehan's PSEventing
toolkit to capture Control-C events. The page for this toolkit,
http://www.codeplex.com/PSEventing, now says that it is intended for
use with PowerShell 1.0 only, that PowerShell 2.0 CTP2 has introduced
support for eventing.
Could someone provide a code sample for V2 CTP2 or CTP3 that shows how
to intercept Control-C? I want my script to be able to do some final
operations in the event that Control-C is pressed, like properly
finalize XML files (write the closing tags).
Thanks,
Chuck
Bob Landau
2009-06-03 15:29:15 UTC
Permalink
Chuck you can however the problem is events are asynchronous so you really
don't know where you are at when CTRL-C is pressed.

Here is another example. When you run this the variables "a" and "b" are at
scope 0

However when CTRL-C is pressed you'll be within the scope of the action
block which pushes "a", "b" to scope 1

Try the below code when Powershell is waiting for input press CTLR-C which
will put you at a prompt.

From there use Get-Variable to access these variables at scope 1 and 0.

The behavior you're seeing is true for ALL cmdlets that accept the -Action
parmaeter. Its confusing I know this is referenced in the Set-PSBreakpoint
cmdlet now but in hindsight perhaps it would be better to have an
About_Action_Scope or something.


=================
$a = 1
$b = 'hello world'

Register-ObjectEvent -InputObject ([Console]) -EventName CancelKeyPress
-Action {$Host.EnterNestedPrompt()}

Get-Variable -Scope 0 -Name a,b
Read-Host
=====================
On Wed, 27 May 2009 08:54:01 -0700, Bob Landau
Post by Bob Landau
Documentation is sparse however this is the cmdlet to intercept CTLR-C
It will not swallow the key but that doesn't appear to be what you're after
Register-ObjectEvent -InputObject ([Console]) -EventName CancelKeyPress
-Action { write-host 'CTRL-C pressed'}
To remove this use UnregisterEvent -SubscriptionId <id>
where the id is returned from the above call, Get-EventSubscriber or if a
variable.
Bob, thanks for the response.
I'm having trouble using this from within a script. When the job is
created with the scriptblock value of the -Action option, I can't seem
to reference variables or functions in my script. Is there some way to
do that?
Chuck
Loading...