Search code examples
powershelldetect

kill a process if Esc key pressed using powershell


is possible to kill a process if Esc key pressed ? I've seen someone doing a key detection but for another approach and i don't know how to modify it to serve my purpose

    $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
    }    
}

Solution

  • $key = [console]::ReadKey()
    write-host $key.key
    if ($key.Key -eq '27') {
      "Pressed Esc"
       Do something 
    }