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

kill a process if Esc key pressed using powershell

如果按下 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
    }    
}
$key = [console]::ReadKey()
write-host $key.key
if ($key.Key -eq '27') {
  "Pressed Esc"
   Do something 
}

感谢 HariHaran 的快速回答! 这是在 powershell

中使用 Esc 键关闭记事本的方法
    $key = [console]::ReadKey()
write-host $key.key
if ($key.Key -eq '27') {
  "Pressed Esc"
Stop-Process -Name "notepad"
}