05-03-26

Mouse Cursor Wrap Around Script

This Lua script for Rebind hardware input wraps the mouse cursor around the screen edges. When the cursor moves off the right side, it reappears on the left, and vice versa. This improves usability an

rebindlua scriptmouse wrapcursor wraphardware inputluascript

Question

I need a Rebind Lua script to wrap the mouse cursor. When the cursor exits the right side of the screen, it should reappear on the left, and vice versa.

Rebind AI

Here's a Rebind Lua script that implements mouse wrapping:

lua
-- rebind: name=Mouse Wrap
-- rebind: mouse_block=true

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

function OnMove(dx, dy)
    if not cfg.enabled then
        return true
    end

    local mx, my = System.Mouse()
    local sw, sh = System.Screen()

    -- if cursor moved past right edge, wrap to left
    if mx + dx > sw then
        HID.MoveTo(0, my)
        return false
    end

    -- if cursor moved past left edge, wrap to right
    if mx + dx < 0 then
        HID.MoveTo(sw - 1, my)
        return false
    end

    return true
end

How it works:

  1. The script uses UI.Schema to create a configuration option to enable or disable the mouse wrap feature.
  2. The OnMove function is triggered whenever the mouse moves.
  3. It checks if the mouse wrap is enabled. If not, it returns true to allow the default mouse movement.
  4. It gets the current mouse position (mx, my) and screen dimensions (sw, sh).
  5. It calculates the potential new mouse position after the movement (mx + dx).
  6. If the mouse would move past the right edge of the screen, it teleports the mouse to the left edge.
  7. If the mouse would move past the left edge of the screen, it teleports the mouse to the right edge.
  8. It returns false to block the original mouse movement, preventing double movement.

Important: The mouse_block=true modeline is essential to enable blocking mouse input. Without it, OnMove fires asynchronously, and the return value is ignored.