--------------------[ KEYBOARD FUNCTIONS ]--------------------------------------------

function keyDown(K)
	local Key = string.lower(K)
	
	if Key == S.Keys.lowerStance and S.canChangeStance then
		if (not Running) then
			if Stance == 0 then
				if S.stanceSettings.Stances.Crouch then
					Crouch()
				elseif S.stanceSettings.Stances.Prone then
					Prone()
				end
			elseif Stance == 1 then
				if S.stanceSettings.Stances.Prone then
					Prone()
				end
			end
		elseif S.dolphinDive then
			wait()
			if Humanoid:GetState() ~= Enum.HumanoidStateType.Freefall and (not UIS:IsKeyDown("Space")) and runReady then
				local tempConnection = Humanoid.Changed:connect(function()
					Humanoid.Jump = false
				end)
				runReady = false
				Dive()
				Running = false
				wait(S.diveSettings.rechargeTime)
				tempConnection:disconnect()
				runReady = true
			end
		end
	end
	
	if Key == S.Keys.raiseStance and S.canChangeStance then
		if (not Running) then
			if Stance == 2 then
				if S.stanceSettings.Stances.Crouch then
					Crouch()
				else
					Stand()
				end
			elseif Stance == 1 then
				Stand()
			end
		end
	end
	
	if Key == S.Keys.ADS then
		if S.aimSettings.holdToADS then
			if (not AimingIn) and (not Aimed) then
				AimingIn = true
				aimGun()
				AimingIn = false
			end
		else
			if Aimed then
				unAimGun()
			else
				aimGun()
			end
		end
	end
	
	if Key == S.Keys.selectFire and S.selectFire then
		if canSelectFire then
			canSelectFire = false
			rawFireMode = rawFireMode + 1
			modeGUI.Text = Modes[((rawFireMode - 1) % numModes) + 1]
			if modeGUI.Text == "AUTO" then
				fireFunction = autoFire
			elseif modeGUI.Text == "BURST" then
				fireFunction = burstFire
			elseif modeGUI.Text == "SEMI" then
				fireFunction = semiFire
			else
				fireFunction = nil
			end
			local speedAlpha = S.selectFireSettings.animSpeed / 0.6
			if S.selectFireSettings.GUI then
				spawn(function()
					fireSelect.Visible = true
					local prevRawFireMode = rawFireMode
					local Increment = 1.5 / (speedAlpha * 0.25)
					local X = 0
					wait(speedAlpha * 0.1)
					while true do
						RS.RenderStepped:wait()
						local newX = X + Increment
						X = (newX > 90 and 90 or newX)
						if prevRawFireMode ~= rawFireMode then break end
						updateModeLabels(rawFireMode - 1, rawFireMode, X)
						if X == 90 then break end
					end
					wait(speedAlpha * 0.25)
					fireSelect.Visible = false
				end)
			end
			if S.selectFireSettings.Animation and (not Aimed) and (not isRunning) and (not isCrawling) then
				spawn(function()
					local sequenceTable = {
						function()
							tweenJoint(RWeld2, nil, CFANG(0, RAD(5), 0), Sine, speedAlpha * 0.15)
							tweenJoint(LWeld, armC0[1], CF(0.1, 1, -0.3) * CFANG(RAD(-7), 0, RAD(-65)), Linear, speedAlpha * 0.15)
							wait(speedAlpha * 0.2)
						end;
						
						function()
							tweenJoint(LWeld, armC0[1], CF(0.1, 1, -0.3) * CFANG(RAD(-10), 0, RAD(-65)), Linear, speedAlpha * 0.1)
							wait(speedAlpha * 0.2)
						end;
						
						function()
							tweenJoint(RWeld2, nil, CF(), Sine, speedAlpha * 0.2)
							tweenJoint(LWeld, armC0[1], S.unAimedC1.leftArm, Sine, speedAlpha * 0.2)
							wait(speedAlpha * 0.2)
						end;
					}
					
					for _, F in pairs(sequenceTable) do
						if Aimed or isRunning or isCrawling or Reloading then
							break
						end
						F()
					end
				end)
			end
			if S.selectFireSettings.Animation or S.selectFireSettings.GUI then
				wait(S.selectFireSettings.animSpeed)
			end
			canSelectFire = true
		end
	end
	
	if Key == S.Keys.Reload then
		if (not Reloading) and (not isCrawling) then
			Reload()
		end
	end
	
	if Key == S.Keys.Sprint then
		runKeyPressed = true
		if runReady then
			if (not Idling) and Walking and (not Running) and (not Knifing) and (not (Aimed and S.guiScope and S.Keys.Sprint == S.Keys.scopeSteady)) then
				monitorStamina()
			end
		end
	end
	
	if Key == S.Keys.scopeSteady then
		steadyKeyPressed = true
		if Aimed and (not Aiming) then
			takingBreath = false
			steadyCamera()
		end
	end
	
	for _, PTable in pairs(Plugins.KeyDown) do
		if Key == string.lower(PTable.Key) then
			spawn(function()
				PTable.Plugin()
			end)
		end
	end
end

function keyUp(K)
	local Key = string.lower(K)
	
	if Key == S.Keys.ADS then
		if S.aimSettings.holdToADS then
			if (not AimingOut) and Aimed then
				AimingOut = true
				unAimGun()
				AimingOut = false
			end
		end
	end
	
	if Key == S.Keys.Sprint then
		runKeyPressed = false
		Running = false
		if (not chargingStamina) then
			rechargeStamina()
		end
	end
	
	if Key == S.Keys.scopeSteady then
		steadyKeyPressed = false
	end
	
	for _, PTable in pairs(Plugins.KeyUp) do
		if Key == string.lower(PTable.Key) then
			spawn(function()
				PTable.Plugin()
			end)
		end
	end
end