05-03-26

Horizontal Mouse Jitter with Toggle and Speed Control

This Lua script for Rebind hardware input allows you to toggle horizontal mouse movement with a button, and adjust the speed via the UI. It's a useful tool for automating mouse movement.

rebindlua scriptmouse jitterhorizontal movementtogglehardware inputscript

Question

Can I achieve the same functionality as the provided AutoIt script in Rebind? Here's the AutoIt script:

lua
#include <WinAPI.au3>

HotKeySet("{F12}", "Stop")

Global $enabled = False
Global $toggleLock = False

; ツールチップ管理
Global $tooltipVisible = False
Global $tooltipStart = 0

; 横方向の往復管理
Global $xDirection = 1 ; 1=右, -1=左

While 1

    ; =========================
    ; 戻るボタンで ON/OFF 切替
    ; =========================
    If BitAND(_WinAPI_GetAsyncKeyState(0x05), 0x8000) Then

        If Not $toggleLock Then
            $enabled = Not $enabled
            $toggleLock = True

            If $enabled Then
                ToolTip("ON", 0, 0)
            Else
                ToolTip("OFF", 0, 0)
            EndIf

            $tooltipVisible = True
            $tooltipStart = TimerInit()
        EndIf

    Else
        $toggleLock = False
    EndIf


    ; =========================
    ; 1秒後ツールチップ消去
    ; =========================
    If $tooltipVisible And TimerDiff($tooltipStart) >= 1000 Then
        ToolTip("")
        $tooltipVisible = False
    EndIf


    ; =========================
    ; ON中なら動作
    ; =========================
    If $enabled Then

        ; 横方向:行ったら必ず戻る
        Local $dx = 2 * $xDirection

        ; Y方向固定
        Local $dy = 0

        ; 相対移動
        MouseMoveRelative($dx, $dy)

        ; 次回逆方向へ
        $xDirection *= -1

    EndIf

    Sleep(0)

WEnd


Func MouseMoveRelative($x, $y)

    Local $tagINPUT = _
        "dword type;" & _
        "long dx;" & _
        "long dy;" & _
        "dword mouseData;" & _
        "dword dwFlags;" & _
        "dword time;" & _
        "ptr dwExtraInfo"

    Local $tInput = DllStructCreate($tagINPUT)

    DllStructSetData($tInput, "type", 0)
    DllStructSetData($tInput, "dx", $x)
    DllStructSetData($tInput, "dy", $y)
    DllStructSetData($tInput, "dwFlags", 0x0001)

    DllCall("user32.dll", _
        "uint", "SendInput", _
        "uint", 1, _
        "struct*", $tInput, _
        "int", DllStructGetSize($tInput))

EndFunc


Func Stop()
    ToolTip("")
    Exit
EndFunc

Rebind AI

Yes, the functionality of the provided AutoIt script can be achieved in Rebind. This script toggles ON/OFF using Mouse5 (back button) and executes horizontal mouse movements when enabled.

Here's the Rebind implementation:

lua
-- rebind: name=Horizontal Mouse Jitter
-- rebind: description=Mouse5で ON/OFF切替、横方向に往復移動

local cfg = UI.Schema({
    enabled = UI.Toggle(false, { label = "Enable" }),
    speed = UI.Slider(2, { min = 1, max = 10, label = "Movement Speed", suffix = "px" }),
})

local xDirection = 1 -- 1=右, -1=左
local moveTask = nil

function OnStart()
    Log.Info("Horizontal mouse jitter ready. Press Mouse5 to toggle.")
end

function OnDown(key)
    if key == "Mouse5" then
        cfg.enabled = not cfg.enabled
        local status = cfg.enabled and "ON" or "OFF"
        UI.Notify(status, "info")
        Log.Info("Jitter: " .. status)
        
        if cfg.enabled then
            startMovement()
        else
            stopMovement()
        end
        
        return false -- ブロック(Mouse5を通さない)
    end
    return true
end

function startMovement()
    if moveTask and moveTask:IsRunning() then
        return
    end
    
    moveTask = Run(function()
        while cfg.enabled do
            local dx = cfg.speed * xDirection
            HID.Move(dx, 0)
            xDirection = xDirection * -1
            
            -- タイミング調整(オリジナルは Sleep(0)だが、適度な間隔を設定)
            Sleep(16) -- ~60fps相当
        end
    end)
end

function stopMovement()
    if moveTask and moveTask:IsRunning() then
        moveTask:Cancel()
        moveTask = nil
    end
end

function OnBlur()
    cfg.enabled = false
    stopMovement()
end

function OnStop()
    stopMovement()
end

-- F12で完全終了
Bind("F12", function()
    Script.Exit("User terminated")
end)

Key differences and features:

  1. Toggles ON/OFF with Mouse5 (back button)
  2. Horizontal oscillationxDirection manages the direction.
  3. UI Panel — Allows speed adjustment.
  4. UI.Notify() instead of tooltip — Displays ON/OFF at the top of the screen.
  5. Exits with F12 — Safely terminates with Script.Exit().
  6. OnBlur cleanup — Automatically stops when switching to another window.

For further customization:

lua
-- rebind: name=Advanced Horizontal Jitter

local cfg = UI.Schema({
    enabled = UI.Toggle(false, { label = "Enable" }),
    speed = UI.Slider(2, { min = 1, max = 10, label = "Movement Speed", suffix = "px" }),
    interval = UI.Slider(16, { min = 5, max = 100, label = "Interval", suffix = "ms" }),
    toggle_key = UI.Keybind("Mouse5", { label = "Toggle Key" }),
})

local xDirection = 1
local moveTask = nil

Bind(cfg.toggle_key, function()
    cfg.enabled = not cfg.enabled
    UI.Notify(cfg.enabled and "ON" or "OFF", "info")
    
    if cfg.enabled then
        if moveTask and moveTask:IsRunning() then
            moveTask:Cancel()
        end
        
        moveTask = Run(function()
            while cfg.enabled do
                HID.Move(cfg.speed * xDirection, 0)
                xDirection = xDirection * -1
                Sleep(cfg.interval)
            end
        end)
    elseif moveTask and moveTask:IsRunning() then
        moveTask:Cancel()
    end
    
    return false
end)

function OnBlur()
    cfg.enabled = false
    if moveTask and moveTask:IsRunning() then
        moveTask:Cancel()
    end
end

function OnStop()
    if moveTask and moveTask:IsRunning() then
        moveTask:Cancel()
    end
end

This version allows you to adjust both speed and interval from the UI.