05-19-26

Color Detection Auto-Click with Hold Key

This Lua script uses hardware input to detect a specific color on your screen and automatically click when the color is found. The script includes adjustable settings for color tolerance, detection bo

rebindlua scriptcolor detectionauto clickhardware inputscreenpixel

Question

Create a script which detects pixels within the center of the screen. It should be a reasonable size. Make it so I can adjust the x and y size of the box it can be detected.

Then make it detect R, G, B values (with a tolerance that works in a +- tolerance).

If it detects the colour it should send mouse1 input and hold for 60ms before rescanning

Rebind AI

I'll create a color detection script that monitors a configurable box at the center of your screen and clicks when it detects a matching color.

lua
-- rebind: min_sdk=0.1.0
-- rebind: name=Color Detection Auto-Click

local cfg = UI.Schema({
    enabled = UI.Toggle(true, { label = "Enable" }),

    -- Detection Box Size
    box_width = UI.Slider(200, {
        min = 50,
        max = 800,
        suffix = "px",
        group = "Detection Box",
    }),
    box_height = UI.Slider(200, {
        min = 50,
        max = 800,
        suffix = "px",
        group = "Detection Box",
    }),

    -- Color Target (RGB)
    target_r = UI.Slider(255, {
        min = 0,
        max = 255,
        label = "Red",
        group = "Target Color",
    }),
    target_g = UI.Slider(0, {
        min = 0,
        max = 255,
        label = "Green",
        group = "Target Color",
    }),
    target_b = UI.Slider(0, {
        min = 0,
        max = 255,
        label = "Blue",
        group = "Target Color",
    }),

    -- Tolerance
    tolerance = UI.Slider(20, {
        min = 0,
        max = 100,
        suffix = "±",
        group = "Detection",
    }),

    -- Cooldown
    cooldown = UI.Slider(100, {
        min = 50,
        max = 1000,
        suffix = "ms",
        label = "Rescan Cooldown",
        group = "Detection",
    }),
})

local lastClickTime = 0

local function hexToRgb(hex)
    if not hex or #hex < 6 then
        return 0, 0, 0
    end
    return tonumber(hex:sub(1, 2), 16), tonumber(hex:sub(3, 4), 16), tonumber(hex:sub(5, 6), 16)
end

local function isColorMatch(pixelHex, targetR, targetG, targetB, tolerance)
    local pr, pg, pb = hexToRgb(pixelHex)

    local rDiff = math.abs(pr - targetR)
    local gDiff = math.abs(pg - targetG)
    local bDiff = math.abs(pb - targetB)

    return rDiff <= tolerance and gDiff <= tolerance and bDiff <= tolerance
end

function OnTick()
    if not cfg.enabled then
        return
    end

    local now = System.Time()
    if now - lastClickTime < cfg.cooldown then
        return
    end

    local screenW, screenH = System.Screen()
    local centerX = screenW / 2
    local centerY = screenH / 2

    local x1 = math.floor(centerX - cfg.box_width / 2)
    local y1 = math.floor(centerY - cfg.box_height / 2)
    local x2 = math.floor(centerX + cfg.box_width / 2)
    local y2 = math.floor(centerY + cfg.box_height / 2)

    -- Sample center pixel
    local pixelHex = Screen.GetPixelColor(math.floor(centerX), math.floor(centerY))

    if pixelHex and isColorMatch(pixelHex, cfg.target_r, cfg.target_g, cfg.target_b, cfg.tolerance) then
        Run(function()
            HID.Down("Mouse1")
            Sleep(60)
            HID.Up("Mouse1")
        end)
        lastClickTime = now
    end
end

function OnBlur()
    -- Release mouse button if script loses focus
    HID.Up("Mouse1")
end

function OnStop()
    -- Clean up on script stop
    HID.Up("Mouse1")
end

How it works

Detection Box: The script defines a configurable rectangle centered on your screen. You control its width and height independently.

Color Matching: You set target R, G, B values (0-255 each). The tolerance slider controls how much variance from that exact color is accepted — it applies a ± tolerance to each channel independently. For example, if you target pure red (255, 0, 0) with tolerance 20, it will match pixels in the range:

  • Red: 235–255
  • Green: 0–20
  • Blue: 0–20

Click Behavior: When a matching pixel is detected at the screen center:

  1. Hold Mouse1 down
  2. Wait 60ms
  3. Release Mouse1
  4. Wait for the cooldown period before rescanning

Cooldown: The rescan cooldown prevents spam-clicking. Adjust it based on how responsive you need the detection to be.

Cleanup: On script stop or window blur, any held mouse button is released to prevent getting stuck.

Tips

  • Start with your target color by sampling a pixel using Screen.GetPixelColor() and noting its hex value
  • The tolerance value is a ± range per channel, so higher tolerance = more permissive matching
  • Keep the detection box reasonably sized — too large and you may get false positives
  • If detection feels too slow, lower the cooldown or increase tick_rate in the modeline

Learn more:

Question

I forgot to mention, the scanning should only happen when the button 'v' is held

Rebind AI

I'll update the script so it only scans when you're holding the V key:

lua
-- rebind: min_sdk=0.1.0
-- rebind: name=Color Detection Auto-Click

local cfg = UI.Schema({
    enabled = UI.Toggle(true, { label = "Enable" }),

    -- Detection Box Size
    box_width = UI.Slider(200, {
        min = 50,
        max = 800,
        suffix = "px",
        group = "Detection Box",
    }),
    box_height = UI.Slider(200, {
        min = 50,
        max = 800,
        suffix = "px",
        group = "Detection Box",
    }),

    -- Color Target (RGB)
    target_r = UI.Slider(255, {
        min = 0,
        max = 255,
        label = "Red",
        group = "Target Color",
    }),
    target_g = UI.Slider(0, {
        min = 0,
        max = 255,
        label = "Green",
        group = "Target Color",
    }),
    target_b = UI.Slider(0, {
        min = 0,
        max = 255,
        label = "Blue",
        group = "Target Color",
    }),

    -- Tolerance
    tolerance = UI.Slider(20, {
        min = 0,
        max = 100,
        suffix = "±",
        group = "Detection",
    }),

    -- Cooldown
    cooldown = UI.Slider(100, {
        min = 50,
        max = 1000,
        suffix = "ms",
        label = "Rescan Cooldown",
        group = "Detection",
    }),
})

local lastClickTime = 0

local function hexToRgb(hex)
    if not hex or #hex < 6 then
        return 0, 0, 0
    end
    return tonumber(hex:sub(1, 2), 16), tonumber(hex:sub(3, 4), 16), tonumber(hex:sub(5, 6), 16)
end

local function isColorMatch(pixelHex, targetR, targetG, targetB, tolerance)
    local pr, pg, pb = hexToRgb(pixelHex)

    local rDiff = math.abs(pr - targetR)
    local gDiff = math.abs(pg - targetG)
    local bDiff = math.abs(pb - targetB)

    return rDiff <= tolerance and gDiff <= tolerance and bDiff <= tolerance
end

function OnTick()
    if not cfg.enabled then
        return
    end

    -- Only scan if V is held
    if not Input.IsDown("V") then
        return
    end

    local now = System.Time()
    if now - lastClickTime < cfg.cooldown then
        return
    end

    local screenW, screenH = System.Screen()
    local centerX = screenW / 2
    local centerY = screenH / 2

    -- Sample center pixel
    local pixelHex = Screen.GetPixelColor(math.floor(centerX), math.floor(centerY))

    if pixelHex and isColorMatch(pixelHex, cfg.target_r, cfg.target_g, cfg.target_b, cfg.tolerance) then
        Run(function()
            HID.Down("Mouse1")
            Sleep(60)
            HID.Up("Mouse1")
        end)
        lastClickTime = now
    end
end

function OnBlur()
    -- Release mouse button if script loses focus
    HID.Up("Mouse1")
end

function OnStop()
    -- Clean up on script stop
    HID.Up("Mouse1")
end

The key change is in OnTick(): it now checks if not Input.IsDown("V") then return at the top, which means the color detection loop only runs while you're physically holding the V key. The moment you release V, scanning stops.