PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

GetFocusedTextBox

This function returns the TextBox the client is currently focused on. A TextBox can be manually selected by the user, or selection can be forced using the TextBox/CaptureFocus function. If no TextBox is selected, this function will return nil.

As UserInputService is client-side only, this function can only be used in a LocalScript.

See also

  • UserInputService/TextBoxFocused
  • UserInputService/TextBoxFocusReleased
  • TextBox/CaptureFocus
  • TextBox/IsFocused
  • TextBox/ReleaseFocus

Returns

Return Type Summary

The TextBox currently in focus, or nil if none are selected


Code Samples


Ignore User Input When a TextBox Is Focused

This example demonstrates how to ignore user input when any TextBox is focused. When a player is not focusing on any TextBox, pressing the jumpKey (Enum/KeyCode|J Key) change’s their humanoid’s Enum/HumanoidStateType|state to Jumping using the Humanoid/ChangeState function to make their character jump.

When the player is focusing on any TextBox, the player will not jump. The example checks if any TextBox is focused by checking if the UserInputService/GetFocusedTextbox function returns nil instead of a TextBox instance.

This connects to the Humanoid/StateChanged event to determine when the player is jumping and prevent the player from using the jumpKey to jump while already jumping.

local UserInputService = game:GetService("UserInputService")

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local jumpKey = Enum.KeyCode.J
local isJumping = false

local function InputBegan(input, gameProcessedEvent)
	local TextBoxFocused = UserInputService:GetFocusedTextBox()
	
	-- Ignore input event if player is focusing on a TextBox
	if TextBoxFocused then return end
	
	-- Make player jump when user presses jumpKey Key on Keyboard
	if (input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == jumpKey) then
		if not isJumping then
			isJumping = true
			humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end
	end
end

local function StateChanged(oldState, newState)
	-- Prevent player from jumping again using jumpKey if already jumping
	if (newState == Enum.HumanoidStateType.Jumping) then
		isJumping = true
	-- Allow player to jump again after landing
	elseif (newState == Enum.HumanoidStateType.Landed) then
		isJumping = false
	end
end

UserInputService.InputBegan:Connect(InputBegan)
humanoid.StateChanged:Connect(StateChanged)