05-03-26

Key Guard: Confirm Dangerous Shortcuts

Protect yourself from accidental key presses with this Lua script for hardware input. It requires confirmation for dangerous shortcuts, preventing unwanted actions. Customize the confirmation time and

rebindlua scriptkey guardshortcutconfirmationhardware inputscript

Question

Key guard: require confirmation before dangerous shortcuts

Rebind AI

luau
-- rebind: min_sdk=0.1.0
-- rebind: name=Key Guard

local cfg = UI.Schema({
    enabled = UI.Toggle(true, { label = "Enable Key Guard" }),
    confirmation_time = UI.Slider(2000, {
        min = 500,
        max = 5000,
        step = 100,
        suffix = "ms",
        label = "Confirmation Window",
        group = "Settings",
    }),
})

-- Define dangerous shortcuts that require confirmation
local dangerous_keys = {
    ["LCtrl+LShift+Delete"] = "Permanent Delete",
    ["LCtrl+W"] = "Close Tab/Window",
    ["LAlt+F4"] = "Close Application",
    ["LCtrl+LShift+Esc"] = "Task Manager",
    ["LWin+D"] = "Show Desktop",
}

local pending_action = nil
local pending_timer = nil

local function clearPending()
    if pending_timer and pending_timer:IsRunning() then
        pending_timer:Cancel()
    end
    pending_action = nil
    pending_timer = nil
end

function OnDown(key)
    if not cfg.enabled then
        return true
    end

    local danger = dangerous_keys[key:upper()]
    if not danger then
        clearPending()
        return true
    end

    -- If this is a repeat of the pending action within the window, allow it
    if pending_action == key then
        UI.Notify(danger .. " - Confirmed!", "success")
        clearPending()
        return true -- pass through
    end

    -- New dangerous key pressed
    clearPending()
    pending_action = key
    UI.Notify(danger .. " - Press again to confirm", "warning")

    -- Set a timer to clear the pending state
    pending_timer = Timer.After(cfg.confirmation_time, function()
        if pending_action == key then
            UI.Notify(danger .. " - Confirmation expired", "info")
            clearPending()
        end
    end)

    return false -- block the first attempt
end

function OnStop()
    clearPending()
end

function OnBlur()
    clearPending()
end

How it works:

This script requires you to press a dangerous key combination twice within a configurable window (default 2 seconds) before it actually executes. The first press is blocked and shows a warning notification. If you press the same key again within the time window, it's confirmed and allowed through. If you don't confirm, the pending state expires and the action is cancelled.

Example flow:

  1. You accidentally hit Ctrl+W to close a tab
  2. The first press is blocked → notification: "Close Tab/Window - Press again to confirm"
  3. You either press Ctrl+W again within 2 seconds to confirm (key goes through), or wait and it expires
  4. If you press a different dangerous key, the old one is forgotten and the new one becomes pending

Customization:

  • Edit dangerous_keys table to add more combinations or remove ones you trust
  • Adjust confirmation_time via the UI slider (500ms to 5 seconds)
  • Add more keys by following the pattern: ["key_combo"] = "Description"

Learn more