local z=game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")local a=script.Parent.Parent local h=require(a.Creator)local n=h.New local P=a.Components local f={}f.__index=f f.__type="\075\101\121\098\105\110\100"function f.New(M,a,f)local B=M.Library assert(f.Title,"\075\101\121\066\105\110\100 - \077\105\115\115\105\110\103 \084\105\116\108\101")assert(f.Default,"\075\101\121\066\105\110\100 - \077\105\115\115\105\110\103 \100\101\102\097\117\108\116 \118\097\108\117\101\046")local s={Value=f.Default;Toggled=false;Mode=f.Mode or"\084\111\103\103\108\101";Type="\075\101\121\098\105\110\100";Callback=f.Callback or function(z) end;ChangedCallback=f.ChangedCallback or function(z) end}local U=false local S=(require(P.Element))(f.Title,f.Description,M.Container,true)s.SetTitle=S.SetTitle s.SetDesc=S.SetDesc local F=n("\084\101\120\116\076\097\098\101\108",{FontFace=Font.new("\114\098\120\097\115\115\101\116\058\047\047\102\111\110\116\115\047\102\097\109\105\108\105\101\115\047\071\111\116\104\097\109\083\083\109\046\106\115\111\110",Enum.FontWeight.Regular,Enum.FontStyle.Normal);Text=f.Default,TextColor3=Color3.fromRGB(240,240,240);TextSize=13;TextXAlignment=Enum.TextXAlignment.Center;Size=UDim2.new(0,0,0,14);Position=UDim2.new(0,0,.5,0),AnchorPoint=Vector2.new(0,.5),BackgroundColor3=Color3.fromRGB(255,255,255),AutomaticSize=Enum.AutomaticSize.X;BackgroundTransparency=1;ThemeTag={TextColor3="\084\101\120\116"}})local k=n("\084\101\120\116\066\117\116\116\111\110",{Size=UDim2.fromOffset(0,30);Position=UDim2.new(1,-10,.5,0);AnchorPoint=Vector2.new(1,.5);BackgroundTransparency=.9,Parent=S.Frame,AutomaticSize=Enum.AutomaticSize.X;ThemeTag={BackgroundColor3="\075\101\121\098\105\110\100"}},{n("\085\073\067\111\114\110\101\114",{CornerRadius=UDim.new(0,5)});n("\085\073\080\097\100\100\105\110\103",{PaddingLeft=UDim.new(0,8);PaddingRight=UDim.new(0,8)}),n("\085\073\083\116\114\111\107\101",{Transparency=.5,ApplyStrokeMode=Enum.ApplyStrokeMode.Border;ThemeTag={Color="\073\110\069\108\101\109\101\110\116\066\111\114\100\101\114"}}),F})function s.GetState(a)if z:GetFocusedTextBox()and s.Mode~="\065\108\119\097\121\115"then return false end if s.Mode=="\065\108\119\097\121\115"then return true elseif s.Mode=="\072\111\108\100"then if s.Value=="\078\111\110\101"then return false end local a=s.Value if a=="\077\111\117\115\101\076\101\102\116"or a=="\077\111\117\115\101\082\105\103\104\116"then return a=="\077\111\117\115\101\076\101\102\116"and z:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)or a=="\077\111\117\115\101\082\105\103\104\116"and z:IsMouseButtonPressed(Enum.UserInputType.MouseButton2)else return z:IsKeyDown(Enum.KeyCode[s.Value])end else return s.Toggled end end function s.SetValue(h,z,a)z=z or s.Key a=a or s.Mode F.Text=z s.Value=z s.Mode=a end function s.OnClick(a,z)s.Clicked=z end function s.OnChanged(a,z)s.Changed=z z(s.Value)end function s.DoClick(z)B:SafeCallback(s.Callback,s.Toggled)B:SafeCallback(s.Clicked,s.Toggled)end function s.Destroy(z)S:Destroy()B.Options[a]=nil end h.AddSignal(k.InputBegan,function(a)if a.UserInputType==Enum.UserInputType.MouseButton1 or a.UserInputType==Enum.UserInputType.Touch then U=true F.Text="\046\046\046"wait(.2)local a a=z.InputBegan:Connect(function(h)local n if h.UserInputType==Enum.UserInputType.Keyboard then n=h.KeyCode.Name elseif h.UserInputType==Enum.UserInputType.MouseButton1 then n="\077\111\117\115\101\076\101\102\116"elseif h.UserInputType==Enum.UserInputType.MouseButton2 then n="\077\111\117\115\101\082\105\103\104\116"end local P P=z.InputEnded:Connect(function(z)if z.KeyCode.Name==n or n=="\077\111\117\115\101\076\101\102\116"and z.UserInputType==Enum.UserInputType.MouseButton1 or n=="\077\111\117\115\101\082\105\103\104\116"and z.UserInputType==Enum.UserInputType.MouseButton2 then U=false F.Text=n s.Value=n B:SafeCallback(s.ChangedCallback,z.KeyCode or z.UserInputType)B:SafeCallback(s.Changed,z.KeyCode or z.UserInputType)a:Disconnect()P:Disconnect()end end)end)end end)h.AddSignal(z.InputBegan,function(a)if not U and not z:GetFocusedTextBox()then if s.Mode=="\084\111\103\103\108\101"then local z=s.Value if z=="\077\111\117\115\101\076\101\102\116"or z=="\077\111\117\115\101\082\105\103\104\116"then if z=="\077\111\117\115\101\076\101\102\116"and a.UserInputType==Enum.UserInputType.MouseButton1 or z=="\077\111\117\115\101\082\105\103\104\116"and a.UserInputType==Enum.UserInputType.MouseButton2 then s.Toggled=not s.Toggled s:DoClick()end elseif a.UserInputType==Enum.UserInputType.Keyboard then if a.KeyCode.Name==z then s.Toggled=not s.Toggled s:DoClick()end end end end end)B.Options[a]=s return s end return f
local UserInputService = game:GetService("UserInputService")

local Root = script.Parent.Parent
local Creator = require(Root.Creator)

local New = Creator.New
local Components = Root.Components

local Element = {}
Element.__index = Element
Element.__type = "Keybind"

function Element:New(Idx, Config)
	local Library = self.Library
	assert(Config.Title, "KeyBind - Missing Title")
	assert(Config.Default, "KeyBind - Missing default value.")

	local Keybind = {
		Value = Config.Default,
		Toggled = false,
		Mode = Config.Mode or "Toggle",
		Type = "Keybind",
		Callback = Config.Callback or function(Value) end,
		ChangedCallback = Config.ChangedCallback or function(New) end,
	}

	local Picking = false

	local KeybindFrame = require(Components.Element)(Config.Title, Config.Description, self.Container, true)

	Keybind.SetTitle = KeybindFrame.SetTitle
	Keybind.SetDesc = KeybindFrame.SetDesc

	local KeybindDisplayLabel = New("TextLabel", {
		FontFace = Font.new("rbxasset://fonts/families/GothamSSm.json", Enum.FontWeight.Regular, Enum.FontStyle.Normal),
		Text = Config.Default,
		TextColor3 = Color3.fromRGB(240, 240, 240),
		TextSize = 13,
		TextXAlignment = Enum.TextXAlignment.Center,
		Size = UDim2.new(0, 0, 0, 14),
		Position = UDim2.new(0, 0, 0.5, 0),
		AnchorPoint = Vector2.new(0, 0.5),
		BackgroundColor3 = Color3.fromRGB(255, 255, 255),
		AutomaticSize = Enum.AutomaticSize.X,
		BackgroundTransparency = 1,
		ThemeTag = {
			TextColor3 = "Text",
		},
	})

	local KeybindDisplayFrame = New("TextButton", {
		Size = UDim2.fromOffset(0, 30),
		Position = UDim2.new(1, -10, 0.5, 0),
		AnchorPoint = Vector2.new(1, 0.5),
		BackgroundTransparency = 0.9,
		Parent = KeybindFrame.Frame,
		AutomaticSize = Enum.AutomaticSize.X,
		ThemeTag = {
			BackgroundColor3 = "Keybind",
		},
	}, {
		New("UICorner", {
			CornerRadius = UDim.new(0, 5),
		}),
		New("UIPadding", {
			PaddingLeft = UDim.new(0, 8),
			PaddingRight = UDim.new(0, 8),
		}),
		New("UIStroke", {
			Transparency = 0.5,
			ApplyStrokeMode = Enum.ApplyStrokeMode.Border,
			ThemeTag = {
				Color = "InElementBorder",
			},
		}),
		KeybindDisplayLabel,
	})

	function Keybind:GetState()
		if UserInputService:GetFocusedTextBox() and Keybind.Mode ~= "Always" then
			return false
		end

		if Keybind.Mode == "Always" then
			return true
		elseif Keybind.Mode == "Hold" then
			if Keybind.Value == "None" then
				return false
			end

			local Key = Keybind.Value

			if Key == "MouseLeft" or Key == "MouseRight" then
				return Key == "MouseLeft" and UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
					or Key == "MouseRight"
						and UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton2)
			else
				return UserInputService:IsKeyDown(Enum.KeyCode[Keybind.Value])
			end
		else
			return Keybind.Toggled
		end
	end

	function Keybind:SetValue(Key, Mode)
		Key = Key or Keybind.Key
		Mode = Mode or Keybind.Mode

		KeybindDisplayLabel.Text = Key
		Keybind.Value = Key
		Keybind.Mode = Mode
	end

	function Keybind:OnClick(Callback)
		Keybind.Clicked = Callback
	end

	function Keybind:OnChanged(Callback)
		Keybind.Changed = Callback
		Callback(Keybind.Value)
	end

	function Keybind:DoClick()
		Library:SafeCallback(Keybind.Callback, Keybind.Toggled)
		Library:SafeCallback(Keybind.Clicked, Keybind.Toggled)
	end

	function Keybind:Destroy()
		KeybindFrame:Destroy()
		Library.Options[Idx] = nil
	end

	Creator.AddSignal(KeybindDisplayFrame.InputBegan, function(Input)
		if
			Input.UserInputType == Enum.UserInputType.MouseButton1
			or Input.UserInputType == Enum.UserInputType.Touch
		then
			Picking = true
			KeybindDisplayLabel.Text = "..."

			wait(0.2)

			local Event
			Event = UserInputService.InputBegan:Connect(function(Input)
				local Key

				if Input.UserInputType == Enum.UserInputType.Keyboard then
					Key = Input.KeyCode.Name
				elseif Input.UserInputType == Enum.UserInputType.MouseButton1 then
					Key = "MouseLeft"
				elseif Input.UserInputType == Enum.UserInputType.MouseButton2 then
					Key = "MouseRight"
				end

				local EndedEvent
				EndedEvent = UserInputService.InputEnded:Connect(function(Input)
					if
						Input.KeyCode.Name == Key
						or Key == "MouseLeft" and Input.UserInputType == Enum.UserInputType.MouseButton1
						or Key == "MouseRight" and Input.UserInputType == Enum.UserInputType.MouseButton2
					then
						Picking = false

						KeybindDisplayLabel.Text = Key
						Keybind.Value = Key

						Library:SafeCallback(Keybind.ChangedCallback, Input.KeyCode or Input.UserInputType)
						Library:SafeCallback(Keybind.Changed, Input.KeyCode or Input.UserInputType)

						Event:Disconnect()
						EndedEvent:Disconnect()
					end
				end)
			end)
		end
	end)

	Creator.AddSignal(UserInputService.InputBegan, function(Input)
		if not Picking and not UserInputService:GetFocusedTextBox() then
			if Keybind.Mode == "Toggle" then
				local Key = Keybind.Value

				if Key == "MouseLeft" or Key == "MouseRight" then
					if
						Key == "MouseLeft" and Input.UserInputType == Enum.UserInputType.MouseButton1
						or Key == "MouseRight" and Input.UserInputType == Enum.UserInputType.MouseButton2
					then
						Keybind.Toggled = not Keybind.Toggled
						Keybind:DoClick()
					end
				elseif Input.UserInputType == Enum.UserInputType.Keyboard then
					if Input.KeyCode.Name == Key then
						Keybind.Toggled = not Keybind.Toggled
						Keybind:DoClick()
					end
				end
			end
		end
	end)

	Library.Options[Idx] = Keybind
	return Keybind
end

return Element
