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

Key Event help?

Started by IsaacTBeast, 29 May 2014 - 06:56 AM
IsaacTBeast #1
Posted 29 May 2014 - 08:56 AM
Hello guys :D/> can you tell me how to make my logic?

here is my logic..

This is the screen or print:

LOLS
EXIST

When you press Up arrow key

This will show up

LOLS <–

when you press down this will show up

EXISTS <–

by default you will see this

LOLS <–
Exists.

Please help me :(/>
sEi #2
Posted 29 May 2014 - 10:54 AM
Maybe i should give you a fishing rod and not just the fish. hmmm.

Here is a basic expandable menu, i don't remember where i found the code.

pastebin get sYgbxW5S menu

http://pastebin.com/sYgbxW5S

Hope this helps

PS: I guess you wanted "exit" and NOT "exist" else you can change it easy

Have a nice day

/sEi
TheOddByte #3
Posted 29 May 2014 - 02:48 PM
Okay, you should go into the wiki and check out os.pullEvent

local menu_options = {
    "Option 1";
    "Option 2";
}
local menu_index = 1
while true do
    --# Start with drawing the menu
    term.clear()
    term.setCursorPos( 1,1 )
    -- Loop through all menu options
    for i = 1, #menu_options do
        if menu_index == i then -- Check if the variable menu_index is equal to the current index
            print( ">" .. menu_options[i] ) -- if it is, then print it with '>'
        else
            print( menu_options[i] ) -- if it isn't then print it regulary
        end
    end

    --# Handle key input
    --[[
        os.pullEvent first returns the event that was pulled, then the key.
        by putting "key" as an argument in the os.pullEvent function we
        limit it to pull that event, so in this case it isn't needed to check
        which event that was pulled
    --]]
    local event, param1 = os.pullEvent( "key" )
    if param1 == keys.up then
        if menu_index > 1 then
            menu_index = menu_index - 1
        end
    elseif param1 == keys.down then
        if menu_index < #menu_options then
            menu_index = menu_index + 1
        end
    end
end
Hope this helped
IsaacTBeast #4
Posted 29 May 2014 - 04:16 PM
Thank you guys :)/>