local b=require(script.Parent:WaitForChild("\066\097\115\101\067\104\097\114\097\099\116\101\114\067\111\110\116\114\111\108\108\101\114"))local W=setmetatable({},b)W.__index=W local X,A=pcall(function()return(UserSettings()):IsUserFeatureEnabled("\085\115\101\114\080\108\097\121\101\114\083\099\114\105\112\116\115\066\105\110\100\065\116\080\114\105\111\114\105\116\121")end)local y=X and A function W.new(X)local A=setmetatable(b.new(),W)A.CONTROL_ACTION_PRIORITY=X A.textFocusReleasedConn=nil A.textFocusGainedConn=nil A.windowFocusReleasedConn=nil A.forwardValue=0 A.backwardValue=0 A.leftValue=0 A.rightValue=0 return A end function W.Enable(W,b)if not UserInputService.KeyboardEnabled then return false end if b==W.enabled then return true end W.forwardValue=0 W.backwardValue=0 W.leftValue=0 W.rightValue=0 W.moveVector=ZERO_VECTOR3 W.isJumping=false if b then W:BindContextActions()W:ConnectFocusEventListeners()else W:UnbindContextActions()W:DisconnectFocusEventListeners()end W.enabled=b return true end function W.UpdateMovement(W,b)if b==Enum.UserInputState.Cancel then W.moveVector=ZERO_VECTOR3 else W.moveVector=Vector3.new(W.leftValue+W.rightValue,0,W.forwardValue+W.backwardValue)end end function W.BindContextActions(b)local W=function(W,X,A)b.forwardValue=X==Enum.UserInputState.Begin and-1 or 0 b:UpdateMovement(X)if y then return Enum.ContextActionResult.Sink end end local X=function(W,X,A)b.backwardValue=X==Enum.UserInputState.Begin and 1 or 0 b:UpdateMovement(X)if y then return Enum.ContextActionResult.Sink end end local A=function(W,X,A)b.leftValue=X==Enum.UserInputState.Begin and-1 or 0 b:UpdateMovement(X)if y then return Enum.ContextActionResult.Sink end end local T=function(W,X,A)b.rightValue=X==Enum.UserInputState.Begin and 1 or 0 b:UpdateMovement(X)if y then return Enum.ContextActionResult.Sink end end local m=function(W,X,A)b.isJumping=X==Enum.UserInputState.Begin if y then return Enum.ContextActionResult.Sink end end if y then ContextActionService:BindActionAtPriority("\109\111\118\101\070\111\114\119\097\114\100\065\099\116\105\111\110",W,false,b.CONTROL_ACTION_PRIORITY,Enum.PlayerActions.CharacterForward)ContextActionService:BindActionAtPriority("\109\111\118\101\066\097\099\107\119\097\114\100\065\099\116\105\111\110",X,false,b.CONTROL_ACTION_PRIORITY,Enum.PlayerActions.CharacterBackward)ContextActionService:BindActionAtPriority("\109\111\118\101\076\101\102\116\065\099\116\105\111\110",A,false,b.CONTROL_ACTION_PRIORITY,Enum.PlayerActions.CharacterLeft)ContextActionService:BindActionAtPriority("\109\111\118\101\082\105\103\104\116\065\099\116\105\111\110",T,false,b.CONTROL_ACTION_PRIORITY,Enum.PlayerActions.CharacterRight)ContextActionService:BindActionAtPriority("\106\117\109\112\065\099\116\105\111\110",m,false,b.CONTROL_ACTION_PRIORITY,Enum.PlayerActions.CharacterJump)else ContextActionService:BindAction("\109\111\118\101\070\111\114\119\097\114\100\065\099\116\105\111\110",W,false,Enum.PlayerActions.CharacterForward)ContextActionService:BindAction("\109\111\118\101\066\097\099\107\119\097\114\100\065\099\116\105\111\110",X,false,Enum.PlayerActions.CharacterBackward)ContextActionService:BindAction("\109\111\118\101\076\101\102\116\065\099\116\105\111\110",A,false,Enum.PlayerActions.CharacterLeft)ContextActionService:BindAction("\109\111\118\101\082\105\103\104\116\065\099\116\105\111\110",T,false,Enum.PlayerActions.CharacterRight)ContextActionService:BindAction("\106\117\109\112\065\099\116\105\111\110",m,false,Enum.PlayerActions.CharacterJump)end end function W.UnbindContextActions(b)ContextActionService:UnbindAction("\109\111\118\101\070\111\114\119\097\114\100\065\099\116\105\111\110")ContextActionService:UnbindAction("\109\111\118\101\066\097\099\107\119\097\114\100\065\099\116\105\111\110")ContextActionService:UnbindAction("\109\111\118\101\076\101\102\116\065\099\116\105\111\110")ContextActionService:UnbindAction("\109\111\118\101\082\105\103\104\116\065\099\116\105\111\110")ContextActionService:UnbindAction("\106\117\109\112\065\099\116\105\111\110")end function W.ConnectFocusEventListeners(b)local function W()b.moveVector=ZERO_VECTOR3 b.forwardValue=0 b.backwardValue=0 b.leftValue=0 b.rightValue=0 b.isJumping=false end local function X(W)b.isJumping=false end b.textFocusReleasedConn=UserInputService.TextBoxFocusReleased:Connect(W)b.textFocusGainedConn=UserInputService.TextBoxFocused:Connect(X)b.windowFocusReleasedConn=UserInputService.WindowFocused:Connect(W)end function W.DisconnectFocusEventListeners(b)if b.textFocusReleasedCon then b.textFocusReleasedCon:Disconnect()b.textFocusReleasedCon=nil end if b.textFocusGainedConn then b.textFocusGainedConn:Disconnect()b.textFocusGainedConn=nil end if b.windowFocusReleasedConn then b.windowFocusReleasedConn:Disconnect()b.windowFocusReleasedConn=nil end end return W
--
local BaseCharacterController = require(script.Parent:WaitForChild("BaseCharacterController"))
local Keyboard = setmetatable({}, BaseCharacterController)
Keyboard.__index = Keyboard

local bindAtPriorityFlagExists, bindAtPriorityFlagEnabled = pcall(function()
	return UserSettings():IsUserFeatureEnabled("UserPlayerScriptsBindAtPriority")
end)
local FFlagPlayerScriptsBindAtPriority = bindAtPriorityFlagExists and bindAtPriorityFlagEnabled

function Keyboard.new(CONTROL_ACTION_PRIORITY)
	local self = setmetatable(BaseCharacterController.new(), Keyboard)
	
	self.CONTROL_ACTION_PRIORITY = CONTROL_ACTION_PRIORITY
	
	self.textFocusReleasedConn = nil
	self.textFocusGainedConn = nil
	self.windowFocusReleasedConn = nil
	
	self.forwardValue  = 0
	self.backwardValue = 0
	self.leftValue = 0
	self.rightValue = 0
	return self
end

function Keyboard:Enable(enable)
	if not UserInputService.KeyboardEnabled then
		return false
	end
	
	if enable == self.enabled then
		-- Module is already in the state being requested. True is returned here since the module will be in the state
		-- expected by the code that follows the Enable() call. This makes more sense than returning false to indicate
		-- no action was necessary. False indicates failure to be in requested/expected state.
		return true
	end
	
	self.forwardValue  = 0
	self.backwardValue = 0
	self.leftValue = 0
	self.rightValue = 0
	self.moveVector = ZERO_VECTOR3
	self.isJumping = false
	
	if enable then
		self:BindContextActions()
		self:ConnectFocusEventListeners()
	else
		self:UnbindContextActions()
		self:DisconnectFocusEventListeners()
	end
	
	self.enabled = enable
	return true
end

function Keyboard:UpdateMovement(inputState)
	if inputState == Enum.UserInputState.Cancel then
		self.moveVector = ZERO_VECTOR3
	else
		self.moveVector = Vector3.new(self.leftValue + self.rightValue, 0, self.forwardValue + self.backwardValue)
	end
end

function Keyboard:BindContextActions()
	
	-- Note: In the previous version of this code, the movement values were not zeroed-out on UserInputState. Cancel, now they are,
	-- which fixes them from getting stuck on.
	local handleMoveForward = function(actionName, inputState, inputObject)			
		self.forwardValue = (inputState == Enum.UserInputState.Begin) and -1 or 0
		self:UpdateMovement(inputState)
		if FFlagPlayerScriptsBindAtPriority then
			return Enum.ContextActionResult.Sink
		end
	end
	
	local handleMoveBackward = function(actionName, inputState, inputObject)	
		self.backwardValue = (inputState == Enum.UserInputState.Begin) and 1 or 0
		self:UpdateMovement(inputState)
		if FFlagPlayerScriptsBindAtPriority then
			return Enum.ContextActionResult.Sink
		end
	end
	
	local handleMoveLeft = function(actionName, inputState, inputObject)	
		self.leftValue = (inputState == Enum.UserInputState.Begin) and -1 or 0
		self:UpdateMovement(inputState)
		if FFlagPlayerScriptsBindAtPriority then
			return Enum.ContextActionResult.Sink
		end
	end
	
	local handleMoveRight = function(actionName, inputState, inputObject)	
		self.rightValue = (inputState == Enum.UserInputState.Begin) and 1 or 0
		self:UpdateMovement(inputState)
		if FFlagPlayerScriptsBindAtPriority then
			return Enum.ContextActionResult.Sink
		end
	end
	
	local handleJumpAction = function(actionName, inputState, inputObject)
		self.isJumping = (inputState == Enum.UserInputState.Begin)
		if FFlagPlayerScriptsBindAtPriority then
			return Enum.ContextActionResult.Sink
		end
	end
	
	-- TODO: Revert to KeyCode bindings so that in the future the abstraction layer from actual keys to
	-- movement direction is done in Lua
	if FFlagPlayerScriptsBindAtPriority then
		ContextActionService:BindActionAtPriority("moveForwardAction", handleMoveForward, false,
			self.CONTROL_ACTION_PRIORITY, Enum.PlayerActions.CharacterForward)
		ContextActionService:BindActionAtPriority("moveBackwardAction", handleMoveBackward, false,
			self.CONTROL_ACTION_PRIORITY, Enum.PlayerActions.CharacterBackward)
		ContextActionService:BindActionAtPriority("moveLeftAction", handleMoveLeft, false,
			self.CONTROL_ACTION_PRIORITY, Enum.PlayerActions.CharacterLeft)
		ContextActionService:BindActionAtPriority("moveRightAction", handleMoveRight, false,
			self.CONTROL_ACTION_PRIORITY, Enum.PlayerActions.CharacterRight)
		ContextActionService:BindActionAtPriority("jumpAction", handleJumpAction, false,
			self.CONTROL_ACTION_PRIORITY, Enum.PlayerActions.CharacterJump)
	else 
		ContextActionService:BindAction("moveForwardAction", handleMoveForward, false, Enum.PlayerActions.CharacterForward)
		ContextActionService:BindAction("moveBackwardAction", handleMoveBackward, false, Enum.PlayerActions.CharacterBackward)
		ContextActionService:BindAction("moveLeftAction", handleMoveLeft, false, Enum.PlayerActions.CharacterLeft)
		ContextActionService:BindAction("moveRightAction", handleMoveRight, false, Enum.PlayerActions.CharacterRight)
		ContextActionService:BindAction("jumpAction",handleJumpAction,false,Enum.PlayerActions.CharacterJump)
	end
end

function Keyboard:UnbindContextActions()
	ContextActionService:UnbindAction("moveForwardAction")
	ContextActionService:UnbindAction("moveBackwardAction")
	ContextActionService:UnbindAction("moveLeftAction")
	ContextActionService:UnbindAction("moveRightAction")
	ContextActionService:UnbindAction("jumpAction")
end

function Keyboard:ConnectFocusEventListeners()
	local function onFocusReleased()
		self.moveVector = ZERO_VECTOR3
		self.forwardValue  = 0
		self.backwardValue = 0
		self.leftValue = 0
		self.rightValue = 0
		self.isJumping = false
	end
	
	local function onTextFocusGained(textboxFocused)
		self.isJumping = false
	end
	
	self.textFocusReleasedConn = UserInputService.TextBoxFocusReleased:Connect(onFocusReleased)
	self.textFocusGainedConn = UserInputService.TextBoxFocused:Connect(onTextFocusGained)
	self.windowFocusReleasedConn = UserInputService.WindowFocused:Connect(onFocusReleased)
end

function Keyboard:DisconnectFocusEventListeners()
	if self.textFocusReleasedCon then
		self.textFocusReleasedCon:Disconnect()
		self.textFocusReleasedCon = nil
	end
	if self.textFocusGainedConn then
		self.textFocusGainedConn:Disconnect()
		self.textFocusGainedConn = nil
	end
	if self.windowFocusReleasedConn then
		self.windowFocusReleasedConn:Disconnect()
		self.windowFocusReleasedConn = nil
	end
end

return Keyboard
