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
endHow it works:
- The script uses
UI.Schemato create a configuration option to enable or disable the mouse wrap feature. - The
OnMovefunction is triggered whenever the mouse moves. - It checks if the mouse wrap is enabled. If not, it returns
trueto allow the default mouse movement. - It gets the current mouse position (
mx,my) and screen dimensions (sw,sh). - It calculates the potential new mouse position after the movement (
mx + dx). - If the mouse would move past the right edge of the screen, it teleports the mouse to the left edge.
- If the mouse would move past the left edge of the screen, it teleports the mouse to the right edge.
- It returns
falseto 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.