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

How To Use Arrow Keys To Scroll Through Menu?

Started by Agoldfish, 31 October 2013 - 05:10 PM
Agoldfish #1
Posted 31 October 2013 - 06:10 PM
Hi, I was going to make a menu for a program I am working on, but i'm not sure how to make the arrow keys scroll down the menu. If someone could help that would be great. Thank you.

-Agoldfish
Lyqyd #2
Posted 31 October 2013 - 07:01 PM
When you receive a key event, re-draw the menu cursor one entry lower or higher than it was, or redraw the menu one line higher or lower, as appropriate. Show us the code you have now.
Agoldfish #3
Posted 31 October 2013 - 07:23 PM
Here is the code. I don't have the menu in place yet..

local password = "Gamma"
os.pullEvent = os.pullEventRaw
print"######################"
print"@Gold System Security@"
print"######################"
sleep(1)
print"----------------------"
print"Please log in"
pass = read("*")
if pass == password then
print"Access Granted"
else
print"Access Denied"
sleep(1.5)
os.reboot()
end

I plan to have a menu for admin bypass, creating an account, and for a main menu which leads to the code I have now.
spdkils #4
Posted 31 October 2013 - 11:46 PM
If you're going to write some type of menu system, I did it by having a while true loop pulling events.

This you have to react to each and every keystroke. I just had one IF that pulled chars, and another for key events, so you can capture the arrow keys, and react…

So, for the password stuff. You're going to need to prompt, assume any keystrokes are for that, check, then draw your menu, then redraw on arrow keystroks… etc.
spdkils #5
Posted 01 November 2013 - 12:17 AM
Or start the while loop only after the login… I suppose.
Zudo #6
Posted 01 November 2013 - 02:22 AM

local o = 1

while true do
 term.clear()
 term.setCursorPos(1,1)
 if o == 1 then
  print("Login <")
  print("Register")
  print("Admin")
 elseif o == 2 then
  print("Login")
  print("Register <")
  print("Admin")
 elseif o == 3 then
  print("Login")
  print("Register")
  print("Admin <")
 end
 _, key = os.pullEvent("key")
 if key == keys.up and o > 1 then
  o = o - 1
 elseif key == keys.down and o < 3 then
  o = o + 1
 elseif key == keys.enter then
  break
 end
end

if o == 1 then
 -- Login
elseif o == 2 then
 -- Register
elseif o == 3 then
 -- Admin override
end

That should work, I haven't tested it though.
jay5476 #7
Posted 04 November 2013 - 01:49 AM

local o = 1

while true do
 term.clear()
 term.setCursorPos(1,1)
 if o == 1 then
  print("Login <")
  print("Register")
  print("Admin")
 elseif o == 2 then
  print("Login")
  print("Register <")
  print("Admin")
 elseif o == 3 then
  print("Login")
  print("Register")
  print("Admin <")
 end
 _, key = os.pullEvent("key")
 if key == keys.up and o > 1 then
  o = o - 1
 elseif key == keys.down and o < 3 then
  o = o + 1
 elseif key == keys.enter then
  break
 end
end

if o == 1 then
 -- Login
elseif o == 2 then
 -- Register
elseif o == 3 then
 -- Admin override
end

That should work, I haven't tested it though.
It would be better to not do it like this

 if o == 1 then
  print("Login <")
  print("Register")
  print("Admin")
 elseif o == 2 then
  print("Login")
  print("Register <")
  print("Admin")
 elseif o == 3 then
  print("Login")
  print("Register")
  print("Admin <")
 end
But to have your options in a table like

options ={"login","register","admin"}
and then loop through like so

for k,v in pairs(options) do
  if k == o then 
    print(v.." <")
  else
    print(v)
  end
end
Sgt.Mike #8
Posted 05 November 2013 - 11:40 AM
The code you posted,in the 3rd post is right.You should just add – end – on the new,blank line right after the line which has – print"Access Denied" –
Written on it….
Agoldfish #9
Posted 05 November 2013 - 11:42 AM
Thanks for the tips guys! Got it figured out.