Classe do mecanismo
TextBox
*Este conteúdo é traduzido por IA (Beta) e pode conter erros. Para ver a página em inglês, clique aqui.
Resumo
Propriedades
Métodos
CaptureFocus():() |
ReleaseFocus(submitted: boolean):() |
Eventos
FocusLost(enterPressed: boolean,inputThatCausedFocusLoss: InputObject):RBXScriptSignal |
Amostras de código
Caixa de Texto Senha Secreta
-- Coloque este código em um LocalScript dentro de uma Caixa de Texto
local textBox = script.Parent
local secretWord = "roblox"
local colorNormal = Color3.new(1, 1, 1) -- branco
local colorWrong = Color3.new(1, 0, 0) -- vermelho
local colorCorrect = Color3.new(0, 1, 0) -- verde
-- Inicialize o estado da textBox
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
textBox.PlaceholderText = "Qual é a palavra 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 = "ACESSO CONCEDIDO"
textBox.BackgroundColor3 = colorCorrect
else
textBox.Text = "ACESSO NEGADO"
textBox.BackgroundColor3 = colorWrong
end
else
-- O jogador parou de editar sem pressionar Enter
textBox.Text = ""
textBox.BackgroundColor3 = colorNormal
end
end
textBox.FocusLost:Connect(onFocusLost)
textBox.Focused:Connect(onFocused)Referência API
Propriedades
CursorPosition
Amostras de código
Seleções de TextBox
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("Sem seleção")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('A seleção é:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)FontSize
SelectionStart
Amostras de código
Seleções de TextBox
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("Sem seleção")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('A seleção é:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)TextColor
TextColor3
Amostras de código
Detector de Vogais
local textBox = script.Parent
local function hasVowels(str)
return str:lower():find("[aeiou]")
end
local function onTextChanged()
local text = textBox.Text
-- Verifique se há vogais
if hasVowels(text) then
textBox.TextColor3 = Color3.new(0, 0, 0) -- Preto
else
textBox.TextColor3 = Color3.new(1, 0, 0) -- Vermelho
end
end
textBox:GetPropertyChangedSignal("Text"):Connect(onTextChanged)TextWrap
Métodos
CaptureFocus
TextBox:CaptureFocus():()
Devolução
()
Amostras de código
Capturar Foco do 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 Padrão: false |
Devolução
()
Amostras de código
Liberar o Foco do 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
Amostras de código
Foco da Caixa de Texto
local textBox = script.Parent
textBox.Focused:Connect(function()
print("Focado!")
end)
textBox.FocusLost:Connect(function()
print("Foco perdido!")
end)FocusLost
Parâmetros
Amostras de código
Foco da Caixa de Texto
local textBox = script.Parent
textBox.Focused:Connect(function()
print("Focado!")
end)
textBox.FocusLost:Connect(function()
print("Foco perdido!")
end)PerdaDeFoco
local textBox = script.Parent
local function onFocusLost(enterPressed, inputThatCausedFocusLost)
if enterPressed then
print("Jogador pressionou Enter")
else
print("Jogador pressionou", 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("O foco foi perdido porque a tecla enter foi pressionada!")
else
print("O foco foi perdido sem a tecla enter ser pressionada")
end
end
textBox.FocusLost:Connect(focusLost)