如果使用powershell按下Esc键,则终止进程

如果使用powershell按下Esc键,则终止进程,powershell,detect,Powershell,Detect,如果按Esc键,是否可能终止进程? 我见过有人做了一个关键的检测,但另一种方法,我不知道如何修改它来满足我的目的 $continue = $true while($continue) { if ([console]::KeyAvailable) { echo "Toggle with F12"; $x = [System.Console]::ReadKey() switch ( $x.key) {

如果按Esc键,是否可能终止进程? 我见过有人做了一个关键的检测,但另一种方法,我不知道如何修改它来满足我的目的

    $continue = $true
while($continue)
{

    if ([console]::KeyAvailable)
    {
        echo "Toggle with F12";
        $x = [System.Console]::ReadKey() 

        switch ( $x.key)
        {
            F12 { $continue = $false }
        }
    } 
    else
    {
        $wsh = New-Object -ComObject WScript.Shell
        $wsh.SendKeys('{CAPSLOCK}')
        sleep 1
        [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wsh)| out-null
        Remove-Variable wsh
    }    
}

感谢HariHaran的快速回答! 下面是如何在powershell中使用Esc键关闭记事本

    $key = [console]::ReadKey()
write-host $key.key
if ($key.Key -eq '27') {
  "Pressed Esc"
Stop-Process -Name "notepad"
}
    $key = [console]::ReadKey()
write-host $key.key
if ($key.Key -eq '27') {
  "Pressed Esc"
Stop-Process -Name "notepad"
}