function Window.LoseFocus(d,s)local h=Entry.TextBox.Text d:ClearHistoryState()if Gui.Visible and not GuiService.MenuIsOpen then Entry.TextBox:CaptureFocus()elseif GuiService.MenuIsOpen and Gui.Visible then d:Hide()end if s and d.Valid then wait()d:SetEntryText("")d.ProcessEntry(h)elseif s then d:AddLine(d._errorText,Color3.fromRGB(255,153,153))end end function Window.TraverseHistory(d,s)local h=d.Cmdr.Dispatcher:GetHistory()if d.HistoryState==nil then d.HistoryState={Position=#h+1;InitialText=d:GetEntryText()}end d.HistoryState.Position=math.clamp(d.HistoryState.Position+s,1,#h+1)d:SetEntryText(d.HistoryState.Position==#h+1 and d.HistoryState.InitialText or h[d.HistoryState.Position])end function Window.ClearHistoryState(s)s.HistoryState=nil end function Window.SelectVertical(d,s)if d.AutoComplete:IsVisible()and not d.HistoryState then d.AutoComplete:Select(s)else d:TraverseHistory(s)end end local s=0 local d=0
--- Event handler for text box focus lost
function Window:LoseFocus(submit)
	local text = Entry.TextBox.Text

	self:ClearHistoryState()

	if Gui.Visible and not GuiService.MenuIsOpen then
		-- self:SetEntryText("")
		Entry.TextBox:CaptureFocus()
	elseif GuiService.MenuIsOpen and Gui.Visible then
		self:Hide()
	end

	if submit and self.Valid then
		wait()
		self:SetEntryText("")
		self.ProcessEntry(text)
	elseif submit then
		self:AddLine(self._errorText, Color3.fromRGB(255, 153, 153))
	end
end

function Window:TraverseHistory(delta)
	local history = self.Cmdr.Dispatcher:GetHistory()

	if self.HistoryState == nil then
		self.HistoryState = {
			Position = #history + 1,
			InitialText = self:GetEntryText(),
		}
	end

	self.HistoryState.Position = math.clamp(self.HistoryState.Position + delta, 1, #history + 1)

	self:SetEntryText(
		self.HistoryState.Position == #history + 1 and self.HistoryState.InitialText
			or history[self.HistoryState.Position]
	)
end

function Window:ClearHistoryState()
	self.HistoryState = nil
end

function Window:SelectVertical(delta)
	if self.AutoComplete:IsVisible() and not self.HistoryState then
		self.AutoComplete:Select(delta)
	else
		self:TraverseHistory(delta)
	end
end

local lastPressTime = 0
local pressCount = 0
