Fix MouseLock for PC Control for delta
Key RequiredUniversal

Fix MouseLock for PC Control for delta

Universal ScriptNPC_PlayersNoobNPC_PlayersNoob 915 views 246 executesUpdated Apr 18, 2026
Unrated
00
Lua Script•74 lines (2339 chars)
local VirtualInputManager = game:GetService("VirtualInputManager")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

-- 【常量预存】
local MOUSE_BUTTON_2 = 1
local isHolding = false

-- 【环境检测函数】
-- 如果设备支持键盘且不支持纯触摸,或者有鼠标,则判定为PC环境
local function IsDesktopEnvironment()
    -- TouchEnabled 虽然在带触摸屏的电脑上也是 true,
    -- 但 KeyboardEnabled 只有在有实体/虚拟键盘映射时才为 true
    -- 最稳妥的判断:有鼠标且有键盘
    return UserInputService.KeyboardEnabled and UserInputService.MouseEnabled
end

-- 【中心点动态缓存】
local cx, cy = 0, 0
local function updateCache()
    local size = workspace.CurrentCamera.ViewportSize
    cx, cy = size.X / 2, size.Y / 2
end
updateCache()
workspace.CurrentCamera:GetPropertyChangedSignal("ViewportSize"):Connect(updateCache)

-- 【强制接管函数】
local function ForceTakeover()
    if not isHolding then
        VirtualInputManager:SendMouseButtonEvent(cx, cy, MOUSE_BUTTON_2, false, game, 0)
        VirtualInputManager:SendMouseButtonEvent(cx, cy, MOUSE_BUTTON_2, true, game, 0)
        isHolding = true
    end
    UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
end

local function ForceRelease()
    if isHolding then
        UserInputService.MouseBehavior = Enum.MouseBehavior.Default
        VirtualInputManager:SendMouseButtonEvent(cx, cy, MOUSE_BUTTON_2, false, game, 0)
        isHolding = false
    end
end

-- 【核心:强制同步逻辑】
RunService:BindToRenderStep("MandatoryShiftLockSync", Enum.RenderPriority.Camera.Value + 1, function()
    -- 动态检测:如果当前不是电脑环境,直接释放并跳过逻辑
    if not IsDesktopEnvironment() then 
        if isHolding then ForceRelease() end
        return 
    end

    -- 严格按照你的判断条件:只有系统/第一人称处于中心锁定时才干预
    local isLocked = (UserInputService.MouseBehavior == Enum.MouseBehavior.LockCenter)
    local isFirstPerson = (workspace.CurrentCamera.Focus.Position - workspace.CurrentCamera.CFrame.Position).Magnitude < 1
    
    if isLocked or isFirstPerson then
        ForceTakeover()
    else
        ForceRelease()
    end
end)

-- 【防篡改补丁】
UserInputService:GetPropertyChangedSignal("MouseBehavior"):Connect(function()
    if not IsDesktopEnvironment() then return end -- 移动端无视
    
    local current = UserInputService.MouseBehavior
    if current == Enum.MouseBehavior.LockCenter and not isHolding then
        ForceTakeover()
    end
end)

print("修正版已启动:已启用环境自适应(移动端自动禁用)。")

Features & Information

Fix MouseLock for Delta UI bug. I hope it is you want.
#Fix MouseLock#MouseLock

Community Comments (0)

No comments yet for this script.

More Scripts for Universal Scripts