Clase de motor
TextBox
*Este contenido se traduce usando la IA (Beta) y puede contener errores. Para ver esta página en inglés, haz clic en aquí.
Resumen
Propiedades
Métodos
CaptureFocus():() |
ReleaseFocus(submitted: boolean):() |
Eventos
FocusLost(enterPressed: boolean,inputThatCausedFocusLoss: InputObject):RBXScriptSignal |
Muestras de código
Palabra Secreta del TextBox
-- Coloca este código en un LocalScript dentro de un TextBox
local textBox = script.Parent
local secretWord = "roblox"
local colorNormal = Color3.new(1, 1, 1) -- blanco
local colorWrong = Color3.new(1, 0, 0) -- rojo
local colorCorrect = Color3.new(0, 1, 0) -- verde
-- Inicializa el estado del textBox
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
textBox.PlaceholderText = "¿Cuál es la palabra secreta?"
textBox.BackgroundColor3 = colorNormal
local function onFocused()
textBox.BackgroundColor3 = colorNormal
end
local function onFocusLost(enterPressed, _inputObject)
if enterPressed then
local guess = textBox.Text
if guess == secretWord then
textBox.Text = "ACCESO CONCEDIDO"
textBox.BackgroundColor3 = colorCorrect
else
textBox.Text = "ACCESO DENEGADO"
textBox.BackgroundColor3 = colorWrong
end
else
-- El jugador dejó de editar sin presionar Enter
textBox.Text = ""
textBox.BackgroundColor3 = colorNormal
end
end
textBox.FocusLost:Connect(onFocusLost)
textBox.Focused:Connect(onFocused)Referencia API
Propiedades
CursorPosition
Muestras de código
Selecciones de TextBox
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("No hay selección")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('La selección es:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)FontSize
SelectionStart
Muestras de código
Selecciones de TextBox
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("No hay selección")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('La selección es:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)TextColor
TextColor3
Muestras de código
Detector de Vocales
local textBox = script.Parent
local function hasVowels(str)
return str:lower():find("[aeiou]")
end
local function onTextChanged()
local text = textBox.Text
-- Comprobar si hay vocales
if hasVowels(text) then
textBox.TextColor3 = Color3.new(0, 0, 0) -- Negro
else
textBox.TextColor3 = Color3.new(1, 0, 0) -- Rojo
end
end
textBox:GetPropertyChangedSignal("Text"):Connect(onTextChanged)TextWrap
Métodos
CaptureFocus
TextBox:CaptureFocus():()
Devuelve
()
Muestras de código
Capturar el enfoque del TextBox
local UserInputService = game:GetService("UserInputService")
local textBox = script.Parent
local input = UserInputService.InputBegan:Connect(function(input, processed)
if input.KeyCode == Enum.KeyCode.Q then
textBox:CaptureFocus()
end
end)ReleaseFocus
Parámetros
| Valor predeterminado: false |
Devuelve
()
Muestras de código
Liberar el enfoque del TextBox
local UserInputService = game:GetService("UserInputService")
local textBox = script.Parent
local input = UserInputService.InputBegan:Connect(function(input, processed)
if input.KeyCode == Enum.KeyCode.Escape and textBox.IsFocused then
textBox:ReleaseFocus()
end
end)Eventos
Focused
Muestras de código
Enfoque del Cuadro de Texto
local textBox = script.Parent
textBox.Focused:Connect(function()
print("¡Enfocado!")
end)
textBox.FocusLost:Connect(function()
print("¡Enfoque perdido!")
end)FocusLost
Parámetros
Muestras de código
Enfoque del Cuadro de Texto
local textBox = script.Parent
textBox.Focused:Connect(function()
print("¡Enfocado!")
end)
textBox.FocusLost:Connect(function()
print("¡Enfoque perdido!")
end)PerdidaDeFoco
local textBox = script.Parent
local function onFocusLost(enterPressed, inputThatCausedFocusLost)
if enterPressed then
print("El jugador presionó Enter")
else
print("El jugador presionó", inputThatCausedFocusLost.KeyCode)
end
end
textBox.FocusLost:Connect(onFocusLost)TextBox.FocusLost1
local gui = script.Parent
local textBox = gui.TextBox
local function focusLost(enterPressed)
if enterPressed then
print("¡La atención se ha perdido porque se presionó enter!")
else
print("La atención se ha perdido sin que se presione enter")
end
end
textBox.FocusLost:Connect(focusLost)