Skip to content

Instantly share code, notes, and snippets.

@gustavomdsantos
Last active July 17, 2025 13:51
Show Gist options
  • Select an option

  • Save gustavomdsantos/7528fdcdc6e52a7fb8ef33def4f4858b to your computer and use it in GitHub Desktop.

Select an option

Save gustavomdsantos/7528fdcdc6e52a7fb8ef33def4f4858b to your computer and use it in GitHub Desktop.
AHKv2 Panic Button - Creates a black and "always-on-top" full screen toogled by hotkey "Ctrl + Win + B". This "panic mode" hides the mouse cursor and disables common keyboard shortcuts such as "Alt + F4", "Esc" and "Alt + Tab".
#Requires AutoHotkey v2.0
#SingleInstance Force
; --- Keyboard shortcut to toggle panic mode ---
^#b::TogglePanic()
; Array to store the black windows for each monitor
global panicWindows := []
; Variable to track the panic mode state
global isPanicActive := false
TogglePanic() {
global isPanicActive
; Toggles the state: if active, deactivate; if inactive, activate.
isPanicActive := !isPanicActive
if (isPanicActive) {
ActivatePanic()
} else {
DeactivatePanic()
}
}
ActivatePanic() {
global panicWindows
; Disables common shortcuts and cursor to protect background windows
BlockShortcuts(true)
SystemCursor("Off")
; Gets the number of monitors
monitorCount := MonitorGetCount()
; Loop through each monitor to create a black window
Loop monitorCount {
; Get the coordinates of the current monitor
MonitorGet(A_Index, &monLeft, &monTop, &monRight, &monBottom)
; Create a new window (GUI)
panicGui := Gui("-Caption +AlwaysOnTop +ToolWindow", "Panic" . A_Index)
panicGui.BackColor := "000000" ; Set background color to black
; Show the black window in fullscreen on the corresponding monitor
panicGui.Show("X" . monLeft . " Y" . monTop . " W" . (monRight - monLeft) . " H" . (monBottom - monTop) . " NoActivate")
; Add the created window to our array for later reference
panicWindows.Push(panicGui)
}
}
DeactivatePanic() {
global panicWindows
; Re-enables the shortcuts and cursor that were blocked
BlockShortcuts(false)
SystemCursor("On")
; Destroys all black windows that were created
for gui in panicWindows {
gui.Destroy()
}
; Clears the array
panicWindows := []
}
; Function to block or unblock system shortcuts
BlockShortcuts(shouldBlock) {
; List of shortcuts to manage
local shortcuts := ["!F4", "Esc", "!Tab", "#Tab", "<#", ">#", "AppsKey"]
if (shouldBlock) {
; Blocks the shortcuts by assigning them to a no-op function
for key in shortcuts {
; Redirect the shortcuts to the DoNothing function
Hotkey(key, DoNothing, "On")
}
} else {
; Unblocks the shortcuts, restoring their default behavior
for key in shortcuts {
Hotkey(key, "Off")
}
}
}
; Function that does nothing, used to "swallow" blocked shortcuts
DoNothing(*) {
Return
}
SystemCursor(OnOff := 1) {
static AndMask, XorMask, handleMap := Map()
static c := [32512,32513,32514,32515,32516,32642,32643,32644,32645,32646,32648,32649,32650]
if (OnOff = "Init" || OnOff = "I" || handleMap.Count = 0) {
; Initialize default and blank cursors
AndMask := Buffer(32 * 4, 0xFF)
XorMask := Buffer(32 * 4, 0x00)
for i, cursorId in c {
h_cursor := DllCall("LoadCursor", "ptr", 0, "uint", cursorId, "ptr")
h_copy := DllCall("CopyImage", "ptr", h_cursor, "uint", 2, "int", 0, "int", 0, "uint", 0, "ptr")
h_blank := DllCall("CreateCursor", "ptr", 0, "int", 0, "int", 0, "int", 32, "int", 32, "ptr", AndMask, "ptr", XorMask, "ptr")
handleMap[cursorId] := Map("default", h_copy, "blank", h_blank, "active", "default")
}
return
}
useBlank := (OnOff = 0 || OnOff = "Off" || (OnOff = "Toggle" || OnOff = "T" || OnOff = -1) && handleMap[c[1]].active = "default")
active := useBlank ? "blank" : "default"
for _, cursorId in c {
cursorObj := handleMap[cursorId]
h_cursor := DllCall("CopyImage", "ptr", cursorObj[active], "uint", 2, "int", 0, "int", 0, "uint", 0, "ptr")
DllCall("SetSystemCursor", "ptr", h_cursor, "uint", cursorId)
cursorObj["active"] := active
}
}
; SystemCursor("Init")
; SetTimer(CheckIdle, 250)
; CheckIdle() {
; global
; timeIdle := A_TimeIdlePhysical // 1000
; if (timeIdle >= 3) {
; SystemCursor("Off")
; } else {
; SystemCursor("On")
; }
; }
ShowCursor(*) {
SystemCursor("On")
ExitApp
}
OnExit(ShowCursor) ; Ensure the cursor is made visible when the script exits.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment