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).
‘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’
E.g.:
$Textbox.Add_Keydown({
if ($_.KeyCode -eq "Enter" -and $_.Shift) {
"Working"
}
})