Record and Loop

@rebind / record-and-loopv0.1.2Public

Record your keys and clicks once, then replay them on a loop with the same timing.

Files

Older release. Installs get 0.1.2.

main.luau1.6 KB

-- Record and Loop: the key starts recording, then starts the loop, then
-- stops everything.

local cfg = UI.Schema({
  key = UI.Keybind("F6", { label = "Record, loop, stop" }),
})

local mode, loop = "idle", nil

-- The take also holds the presses of the record key itself. Drop them and
-- carry their delays forward so the rest of the timing stays exact; the
-- returned tail is the pause before the final key press.
local function withoutKey(take, key)
  local kept, carry = {}, 0
  for _, action in take do
    if action.code == key then
      carry += action.delay or 0
    else
      local copy = table.clone(action)
      copy.delay = (copy.delay or 0) + carry
      carry = 0
      table.insert(kept, copy)
    end
  end
  return kept, carry
end

Bind(cfg.key, function()
  if mode == "idle" then
    Macro.Record()
    mode = "recording"
    UI.Notify("Recording", "info")
  elseif mode == "recording" then
    local route, tail = withoutKey(Macro.Finish(), cfg.key)
    if #route == 0 then
      mode = "idle"
      UI.Notify("Nothing was recorded", "warning")
      return
    end
    loop = Run(function()
      while true do
        Macro.Play(route):Wait()
        Sleep(tail)
      end
    end)
    mode = "looping"
    UI.Notify("Looping", "success")
  else
    if loop then
      loop:Cancel()
      loop = nil
    end
    Macro.StopAll()
    mode = "idle"
    UI.Notify("Stopped", "info")
  end
end)

function OnStop()
  if loop then
    loop:Cancel()
  end
  if mode == "recording" then
    Macro.Finish()
  end
  Macro.StopAll()
end

3 files, 3.2 KB.