05-07-26

Key Hold to Mouse Movement Script

This Lua script for Rebind hardware input allows you to move your mouse with key holds (W, A, S, D). Choose between continuous or accelerated movement, and customize speed and keys. Learn how to creat

rebindlua scriptmouse movementkey holdhardware inputscriptW A S D

Question

Key hold to mouse movement converter

Rebind AI

luau
-- rebind: name=Key Hold to Mouse Movement
-- rebind: description=Hold keys to move mouse in cardinal directions

local cfg = UI.Schema({
    enabled = UI.Toggle(true, { label = "Enable" }),
    
    up_key = UI.Keybind("W", { label = "Move Up", group = "Keys" }),
    down_key = UI.Keybind("S", { label = "Move Down", group = "Keys" }),
    left_key = UI.Keybind("A", { label = "Move Left", group = "Keys" }),
    right_key = UI.Keybind("D", { label = "Move Right", group = "Keys" }),
    
    speed = UI.Slider(5, { min = 1, max = 50, label = "Speed (px/tick)", group = "Movement" }),
    mode = UI.Select("Continuous", { "Continuous", "Acceleration" }, { label = "Mode", group = "Movement" }),
    accel_rate = UI.Slider(1.2, { min = 1, max = 3, step = 0.1, label = "Accel Rate", group = "Movement", showIf = "mode:Acceleration" }),
})

local velocity = { x = 0, y = 0 }

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

    local dx = 0
    local dy = 0

    -- read cardinal directions
    if Input.IsDown(cfg.up_key) then dy = dy - 1 end
    if Input.IsDown(cfg.down_key) then dy = dy + 1 end
    if Input.IsDown(cfg.left_key) then dx = dx - 1 end
    if Input.IsDown(cfg.right_key) then dx = dx + 1 end

    if cfg.mode == "Continuous" then
        -- direct movement
        local speed = cfg.speed
        if dx ~= 0 or dy ~= 0 then
            HID.Move(dx * speed, dy * speed)
        end
    else
        -- acceleration mode: velocity builds up
        local speed = cfg.speed
        local accel = cfg.accel_rate

        if dx ~= 0 or dy ~= 0 then
            velocity.x = velocity.x + dx * speed * 0.1
            velocity.y = velocity.y + dy * speed * 0.1
            
            -- cap velocity
            local maxVel = speed * 2
            if math.abs(velocity.x) > maxVel then
                velocity.x = maxVel * (velocity.x > 0 and 1 or -1)
            end
            if math.abs(velocity.y) > maxVel then
                velocity.y = maxVel * (velocity.y > 0 and 1 or -1)
            end
            
            HID.Move(velocity.x, velocity.y)
        else
            -- decelerate when no input
            velocity.x = velocity.x * 0.8
            velocity.y = velocity.y * 0.8
            if math.abs(velocity.x) < 0.1 then velocity.x = 0 end
            if math.abs(velocity.y) < 0.1 then velocity.y = 0 end
        end
    end
end

function OnDown(key)
    if cfg.enabled and (key == cfg.up_key or key == cfg.down_key or key == cfg.left_key or key == cfg.right_key) then
        return false
    end
    return true
end

function OnUp(key)
    if cfg.enabled and (key == cfg.up_key or key == cfg.down_key or key == cfg.left_key or key == cfg.right_key) then
        return false
    end
    return true
end

function OnBlur()
    velocity.x = 0
    velocity.y = 0
end

This script converts key holds into mouse movement:

How it works:

  • Hold W/A/S/D (configurable) to move the mouse up/left/down/right
  • Continuous mode: Instant movement at a constant speed
  • Acceleration mode: Movement builds velocity and gradually decelerates when keys are released (smoother, more natural feel)
  • All four directions can be pressed simultaneously for diagonal movement
  • The original keys are blocked from reaching the PC

UI Controls:

  • Enable/disable the entire feature
  • Assign any key to each cardinal direction
  • Adjust movement speed (1–50 pixels per tick)
  • Choose between instant or accelerated movement
  • Acceleration rate controls how quickly velocity builds and decays

Learn more