white = Color.new(255,255,255) 
red = Color.new(255,0,0)
yellow = Color.new (255,255,0)

characters = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", " ", "", "", "", "", "", "", "", "", "", "" }

selector = Image.createEmpty(10,10)
selector:clear(red)

charX = 170
charY= 120

currentX = 167
currentY = 118



currentLetter = 1 


uppercase = true 
name = ""
oldpad = Controls.read() 



addX = 0 
addY = -10
addRow = -9 



function drawLetters() 
for a = 1, 3 do 
addX = -10 
addY = addY + 10 
addRow = addRow + 9 
for b = 1, 9 do 
addX = addX + 10
if uppercase == true then 
screen:print(charX + addX,charY + addY, characters[b + addRow],white) 
else 
screen:print(charX + addX,charY + addY,string.upper(characters[b + addRow]),white) 
end 
end 
end 
addX = 0 
addY = -10
addRow = -9
end 





while true do



pad = Controls.read() 

screen:clear() 
screen:blit(0,0,fond)
screen:print(130,80,"Entrez votre code d'erreur:",white)
screen:print(195,100,""..name,yellow) 
screen:print(130,160,"Appuyez sur O pour valider",white)
screen:print(130,180,"Appuyez sur [] pour effacer",white)
screen:print(130,200,"Appuyez sur ^ pour acceder",white)
screen:print(150,210,"directement aux pages",white)
screen:blit(currentX,currentY, selector) 

drawLetters() 


if pad:cross() and oldpad:cross() ~= pad:cross() then 
if uppercase == true then 
name = name .. characters[currentLetter] 
else 
name = name .. string.upper(characters[currentLetter]) 
end 
end 


if pad:square() and oldpad:square() ~= pad:square() then 
name = string.sub(name, 1, string.len(name) - 1) 
end 


if pad:circle() then

file = io.open("Cache/num.txt", "w")
file:write(name)
file:write("\n")
file:close()
dofile("recherche2.lua")

end

if pad:start() then
screen.flip() 
screen.waitVblankStart(7) 
dofile("menu.lua")
end

if pad:triangle() then
screen.flip() 
screen.waitVblankStart(7) 
dofile("Pages/1.lua")
end

--MOVE SELECTOR RIGHT :

if pad:right() and currentLetter ~= 9 and currentLetter ~= 18 and currentLetter ~= 27 and oldpad:right() ~= pad:right() then 
currentLetter = currentLetter + 1 
currentX = currentX + 10 
elseif pad:right() and oldpad:right() ~= pad:right() then 
if currentLetter == 8 or currentLetter == 17 or currentLetter == 26 then 
currentLetter = currentLetter - 9
currentX = 177 
end 
end 



-- MOVE SELECTOR LEFT 
if pad:left() and currentLetter ~= 1 and currentLetter ~= 10 and currentLetter ~= 19 and oldpad:left() ~= pad:left() then 
currentLetter = currentLetter - 1 
currentX = currentX - 10 
elseif pad:left() and oldpad:left() ~= pad:left() then 
if currentLetter == 0 or currentLetter == 9 or currentLetter == 18 then 
currentX = 177 + 70 
currentLetter = currentLetter + 9
end 
end 

-- MOVE SELECTOR DOWN 
if pad:down() and currentLetter < 19 and currentLetter > 9 and oldpad:down() ~= pad:down() then 
currentLetter = currentLetter - 9 
currentY = currentY - 10 
elseif pad:down() and currentLetter < 8 and oldpad:down() ~= pad:down() then 
currentY = 128 
currentLetter = currentLetter + 9 
end 

-- MOVE SELECTOR UP 
if pad:up() and currentLetter < 19 and currentLetter > 9 and oldpad:up() ~= pad:up() then 
currentLetter = currentLetter - 9 
currentY = currentY - 10 
elseif pad:up() and currentLetter < 8 and oldpad:up() ~= pad:up() then 
currentY = 128 
currentLetter = currentLetter + 9
end 







 

screen.flip() 
screen.waitVblankStart() 
oldpad = pad 
end