

bkspc 	 	= false
keysLocked 	= false
keyTime = 0
local downTimes ={}

function keyboard.pressed(k)
	-- print("keypresed",k)
	if k=="enter" then k="return" end
	-- print(k)
	if editingLevel and editor.editingCode and scriptsPanel.focused then 
		polywell.keypressed(k)
			
		if  cmdDown  then 
			if k=="return" then 
				editor.playLevel()
			elseif k=="s" then 
				local success, err = pcall(require,LINK_DIR..scriptsPanel.script.filename)
				if success then 
					pcall(lume.hotswap,LINK_DIR..scriptsPanel.script.filename)
				else 
					tempTip(err,3)
				end
			elseif k=="a" then
				scriptsPanel.b.point 				=0
				scriptsPanel.b.point_line 	=1
				scriptsPanel.b.mark 				=#lume.last(scriptsPanel.b.lines)
				scriptsPanel.b.mark_line 		=#scriptsPanel.b.lines
			elseif k=="-" then
				scriptsPanel.fontSize=scriptsPanel.fontSize>scriptsPanel.minFontSize and scriptsPanel.fontSize-1 or scriptsPanel.fontSize
			elseif k=="=" then
				scriptsPanel.fontSize=scriptsPanel.fontSize<scriptsPanel.maxFontSize and scriptsPanel.fontSize+1 or scriptsPanel.fontSize
			elseif k=="up" then
				local temp = scriptsPanel.b.lines[scriptsPanel.b.point_line]
				scriptsPanel.b.lines[scriptsPanel.b.point_line] = scriptsPanel.b.lines[scriptsPanel.b.point_line-1]
				scriptsPanel.b.lines[scriptsPanel.b.point_line-1] = temp
				polywell.word_wrap()
			elseif k=="down" then
				local temp = scriptsPanel.b.lines[scriptsPanel.b.point_line]
				scriptsPanel.b.lines[scriptsPanel.b.point_line] = scriptsPanel.b.lines[scriptsPanel.b.point_line+1]
				scriptsPanel.b.lines[scriptsPanel.b.point_line+1] = temp
				polywell.word_wrap()
			end
		end
		if k=="escape" then
			editor.dockPanel(scriptsPanel, function()
				scriptsPanel.x=9999
				scriptsPanel.border=nil
				editor.editingCode =false
				scriptsPanel.focused=false
				mouse.pushRelease(editor.release )
				mouse.pushReleaseR(editor.release )
			end)
		end
	else
		table.add(keysPressed,k)

		table.add(keysDown,k)
		downTimes[k] = 0
		if cmdDown and altDown and shiftDown and k == 'escape' then
      love.event.quit("restart")
   	end
	end
end

function keyboard.released(k)
	if editingLevel and editor.editingCode and scriptsPanel.focused then 
		polywell.keyreleased(k)
	else
		lume.remove(keysDown,k)
		table.add(keysReleased,k)
		--downTimes[k] = 0
	end
end

function keypressed(k) 

	if keysLocked==true  or #keysPressed==0 then return false end
	if type(k)=="table" then 

	else
		return (lume.find(keysPressed,k))
	end
end

function anykeypressed(t) 
	for k,key in ipairs(t) do 
		if keypressed(key) then 
			return true 
		end
	end
	return false
end


function keyreleased(k,t1,t2) 
	if keysLocked==true  or #keysReleased==0 then return false end
	if downTimes[k] then 
		return downTimes[k]>(t1 or -1) and downTimes[k]<(t2 or 999999)  and (lume.find(keysReleased,k))
	else
		return  (lume.find(keysReleased,k))
	end
end

function keytapped(k)
	return keyreleased(k,0,10) 
end

function holdKeys()
	local wasLocked = keysLocked
	unlockKeys() 	
	for k,keyTime in pairs(downTimes) do 
		downTimes[k] = keyDown(k) and downTimes[k]+1 or downTimes[k]
	end
	if wasLocked then
		lockKeys() 	
	end 
end

function keyDown(k,frames)
	if keysLocked==true  or #keysDown==0 then return false end
	local heldEnough
	if frames then
		heldEnough=downTimes[k] and downTimes[k]>frames
	else
		heldEnough=true
	end
	return (lume.find(keysDown,k) and heldEnough)
end

function anyKeyDown (keys)
	local any = false
	for _,k in ipairs(keys) do 
		if keyDown(k) then any=true end
	end
	return any
end

function keyboard.textinput(t)
	if editingLevel and editor.editingCode and scriptsPanel.focused then 
		polywell.textinput(t)
	else
		if namer.open and namer.mode~="warning" then 
			if #namer.text<(namer.target.limit or 9999) then
				local canType = true 
				if namer.num then 
					canType=false
					for i=0,9,1 do 
						if t==tostring(i) then 
							canType=true
						end
					end
				end
				if canType then 
					namer.text = namer.text..t
				end
			end
		end
	end
end

function keyboard:reset()
	lume.clear(keysPressed)
	lume.clear(keysReleased)
	keyboard.active = #keysDown>0
end

function keyboard:modsTouched()
	return keyboard:modsReleased() or keyboard:modsPressed()
end

function keyboard:modsReleased()
	return keyreleased("lgui") or keyreleased("rgui") or keyreleased("lshift") or keyreleased("rshift") or keyreleased("lctrl") or keyreleased("rctrl") or keyreleased("lalt") or keyreleased("ralt") 
end

function keyboard:modsPressed()
	return keypressed("lgui") or keypressed("rgui") or keypressed("lshift") or keypressed("rshift") or keypressed("lctrl") or keypressed("rctrl") or keypressed("lalt") or keypressed("ralt") 
end

-----------------------------------------------------------
-- LOCK KEYS
-----------------------------------------------------------
function lockKeys() 	
	keysLocked = true
end
function unlockKeys() 	
	keysLocked = false
end