This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
mibac138's profile picture

Problem with arrow keys (up and down)

Started by mibac138, 01 January 2013 - 10:18 AM
mibac138 #1
Posted 01 January 2013 - 11:18 AM
Code:


term.clear()
do
charSelected = ">"
selected = 1
exit = false
end
repeat
term.clearLine(2)
term.clearLine(4)
configText = fs.open("configText", "r")



term.setBackgroundColor(colors.white)
drawImage("Config/test.nfp")

term.setTextColor(colors.lightGray)
term.setCursorPos(3,2)
term.setBackgroundColor(colors.white)
print(configText.readLine())
term.setTextColor(colors.lightGray)

term.setCursorPos(3,4)
term.setBackgroundColor(colors.white)
print(configText.readLine())


event, param1 = os.pullEvent("key")
if param1 == 200 then selected = selected + 1
elseif param1 == 208 then selected = selected - 1
elseif param1 == 16 then exit = true end

if selected == 3 then selected = 1
elseif selected == 0 then selected = 2 end

  if selected == 1 then
    term.setCursorPos(2,2)
    print(charSelected)
  elseif selected == 2 then
   term.setCursorPos(2,4)
   print(charSelected)
end
until exit == true

DrawImage() code:


function drawImage( img, y, x)
img = "images/"..img
x = x or 1
y = y or 1
local ImageToDraw = paintutils.loadImage(img)
paintutils.drawImage(ImageToDraw, y, x)
end

Question:

Why it print ">" only when i press q? (q = exit)
remiX #2
Posted 01 January 2013 - 01:12 PM
What exactly does it do when you run it? Nothing? Or what?

Also, you opened the file but you never close it and If I'm not mistaken, term.clearLine() does not take any arguments, it's term.clearLine() and it will clear the line it's currently on.

Read my comments and try this?

term.clear()

charSelected = ">"
selected = 1
running = true

function drawImage(img, y, x)
    img = "images/"..img
    if not fs.exists(img) then error("File " .. img .. " does not exist.") end -- To check if it exists.
    x = x or 1
    y = y or 1
    local ImageToDraw = paintutils.loadImage(img)
    paintutils.drawImage(ImageToDraw, y, x)
end

function clearLine(yLine) -- Custom function to clear a specefic line
    term.setCursorPos(1, yLine)
    term.clearLine()
end

while running do
    configText = fs.open("configText", "r")
    line_one = configText.readLine() -- It's good practice to save the lines into variables
    line_two = configText.readLine()
    configText.close() -- You need to close the file!
    term.setBackgroundColor(colors.white)
    drawImage("Config/test.nfp")

    term.setTextColor(colors.lightGray)
    term.setCursorPos(3,2)
    term.setBackgroundColor(colors.white)
    print(line_one)
    
    term.setTextColor(colors.lightGray)
    term.setCursorPos(3,4)
    term.setBackgroundColor(colors.white)
    print(line_two)

    event, param1 = os.pullEvent("key")
    clearLine(2) -- Only clear line after it pulls the event else it will print ">" and clear it again.
    clearLine(4) -- If term.clearLine() does accept arguments now, then this was probably your problem.
    if param1 == 200 then selected = selected + 1
    elseif param1 == 208 then selected = selected - 1
    elseif param1 == 16 then running = false end

    if selected == 3 then selected = 1
    elseif selected == 0 then selected = 2 end

    if selected == 1 then
        term.setCursorPos(2,2)
        write(charSelected)
    elseif selected == 2 then
        term.setCursorPos(2,4)
        write(charSelected)
    end
end
mibac138 #3
Posted 01 January 2013 - 01:41 PM
Thanks! :)/>