142 lines
4.1 KiB
AutoHotkey
142 lines
4.1 KiB
AutoHotkey
; WinXKill - KDE-style "kill window" mode for Windows (AutoHotkey v2)
|
|
; Ctrl+Alt+Esc enters kill mode. Left-click a window to end its process.
|
|
; Esc or right-click cancels.
|
|
#Requires AutoHotkey v2.0
|
|
#SingleInstance Force
|
|
Persistent
|
|
|
|
global KillMode := false
|
|
global SelfPID := ProcessExist()
|
|
global ProtectedExes := Map("explorer.exe", 1, "winxkill.exe", 1)
|
|
global ShellClasses := Map("Shell_TrayWnd", 1, "Shell_SecondaryTrayWnd", 1, "Progman", 1, "WorkerW", 1)
|
|
|
|
A_IconTip := "WinXKill - Ctrl+Alt+Esc to kill a window"
|
|
try TraySetIcon("shell32.dll", 132)
|
|
A_TrayMenu.Delete()
|
|
A_TrayMenu.Add("Enter kill mode`tCtrl+Alt+Esc", (*) => EnterKillMode())
|
|
A_TrayMenu.Add()
|
|
A_TrayMenu.Add("Exit", (*) => ExitApp())
|
|
A_TrayMenu.Default := "Enter kill mode`tCtrl+Alt+Esc"
|
|
OnExit((*) => RestoreCursors())
|
|
|
|
$^!Esc:: EnterKillMode()
|
|
|
|
#HotIf KillMode
|
|
*LButton:: KillUnderCursor()
|
|
*RButton:: LeaveKillMode("Cancelled")
|
|
*Esc:: LeaveKillMode("Cancelled")
|
|
*LButton Up:: return
|
|
*RButton Up:: return
|
|
#HotIf
|
|
|
|
EnterKillMode() {
|
|
global KillMode
|
|
if KillMode
|
|
return
|
|
KillMode := true
|
|
SetCrossCursor()
|
|
Log("enter kill mode")
|
|
SetTimer(UpdateTip, 50)
|
|
}
|
|
|
|
LeaveKillMode(msg := "") {
|
|
global KillMode
|
|
KillMode := false
|
|
SetTimer(UpdateTip, 0)
|
|
ToolTip()
|
|
RestoreCursors()
|
|
Log("leave kill mode " msg)
|
|
if (msg != "") {
|
|
ToolTip(msg)
|
|
SetTimer(() => ToolTip(), -1500)
|
|
}
|
|
}
|
|
|
|
GetTarget(&hwnd, &pid, &exe, &title, &cls) {
|
|
MouseGetPos(, , &hwnd)
|
|
pid := 0, exe := "", title := "", cls := ""
|
|
if !hwnd
|
|
return false
|
|
try {
|
|
pid := WinGetPID(hwnd)
|
|
exe := WinGetProcessName(hwnd)
|
|
title := WinGetTitle(hwnd)
|
|
cls := WinGetClass(hwnd)
|
|
}
|
|
return true
|
|
}
|
|
|
|
IsProtected(pid, exe, cls) {
|
|
return pid = 0 || pid = SelfPID || ShellClasses.Has(cls)
|
|
|| ProtectedExes.Has(StrLower(exe)) || pid = 4
|
|
}
|
|
|
|
ShowTip(text) {
|
|
static last := ""
|
|
CoordMode("Mouse", "Screen"), CoordMode("ToolTip", "Screen")
|
|
MouseGetPos(&mx, &my)
|
|
ToolTip(text, mx + 24, my + 24)
|
|
if (text != last)
|
|
Log("tip: " StrReplace(text, "`n", " | ")), last := text
|
|
}
|
|
|
|
Log(msg) {
|
|
static on := (A_Args.Length && A_Args[1] = "/debug")
|
|
if on
|
|
FileAppend(FormatTime(, "HH:mm:ss") " " msg "`n", A_Temp "\WinXKill-debug.log")
|
|
}
|
|
|
|
UpdateTip() {
|
|
if !GetTarget(&hwnd, &pid, &exe, &title, &cls) {
|
|
ShowTip("KILL MODE: no window")
|
|
return
|
|
}
|
|
t := StrLen(title) > 60 ? SubStr(title, 1, 57) "..." : title
|
|
if IsProtected(pid, exe, cls)
|
|
ShowTip("KILL MODE (protected): " exe " - " t "`nEsc / right-click to cancel")
|
|
else
|
|
ShowTip("KILL MODE: click to kill`n" exe " (PID " pid ")`n" t "`nEsc / right-click to cancel")
|
|
}
|
|
|
|
KillUnderCursor() {
|
|
SetTimer(UpdateTip, 0)
|
|
ToolTip()
|
|
Sleep 30
|
|
ok := GetTarget(&hwnd, &pid, &exe, &title, &cls)
|
|
Log("click: hwnd=" hwnd " pid=" pid " exe=" exe " class=" cls " title=" title)
|
|
if !ok || IsProtected(pid, exe, cls) {
|
|
LeaveKillMode()
|
|
Log("refused (protected)")
|
|
Notify("Not killed", (exe != "" ? exe : "That window") " is protected (desktop/taskbar/WinXKill).")
|
|
return
|
|
}
|
|
LeaveKillMode()
|
|
ProcessClose(pid)
|
|
if ProcessWaitClose(pid, 2) {
|
|
Log("kill FAILED pid=" pid)
|
|
Notify("Could not kill " exe, "PID " pid " is still running. It is probably elevated (run as administrator); WinXKill runs unelevated and cannot end it.")
|
|
return
|
|
}
|
|
Log("killed pid=" pid)
|
|
ToolTip("Killed " exe " (PID " pid ")")
|
|
SetTimer(() => ToolTip(), -1500)
|
|
}
|
|
|
|
Notify(title, text) {
|
|
TrayTip(text, "WinXKill: " title, 0x2)
|
|
ToolTip(title ": " text)
|
|
SetTimer(() => ToolTip(), -4000)
|
|
}
|
|
|
|
SetCrossCursor() {
|
|
static ids := [32512, 32513, 32514, 32515, 32516, 32642, 32643, 32644, 32645, 32646, 32648, 32649, 32650, 32651]
|
|
cross := DllCall("LoadCursor", "Ptr", 0, "Ptr", 32515, "Ptr")
|
|
for id in ids {
|
|
copy := DllCall("CopyImage", "Ptr", cross, "UInt", 2, "Int", 0, "Int", 0, "UInt", 0, "Ptr")
|
|
DllCall("SetSystemCursor", "Ptr", copy, "UInt", id)
|
|
}
|
|
}
|
|
|
|
RestoreCursors() {
|
|
DllCall("SystemParametersInfo", "UInt", 0x57, "UInt", 0, "Ptr", 0, "UInt", 0) ; SPI_SETCURSORS
|
|
}
|