Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("/ccConfig")
- local bgColor = 1
- local keyBindings = {}
- local config = ccConfig.new("keybindings_example.cfg", "Keybinding Example")
- local function loadKeybindings(settings)
- config:load()
- -- this is the Q key
- keyBindings[config:getNumber("program.terminate", 16)] = {desc = "Closes the program", func = function() term.clear() term.setCursorPos(1,1) print("Quit") error() end}
- config:addCommentForKey("program.terminate", "Key to terminate the program")
- -- this is the / or ? key (they have same key code)
- keyBindings[config:getNumber("program.help", 53)] = {desc = "Shows the help screen, but you know that", func = drawHelp }
- config:addCommentForKey("program.help", "Key to show the programs help screen")
- -- this is the C key
- keyBindings[config:getNumber("program.changeColor", 46)] = {desc = "Changes the screens background color", func = function() bgColor = (bgColor + 1) % 15 end}
- config:addCommentForKey("program.help", "Key to show the programs help screen")
- -- more key bindings would be added in here
- config:save()
- end
- local function cwrite(msg, y, offset)
- local sw,sh = term.getSize()
- term.setCursorPos(sw/2-#msg/2, (y or (sh/2)) + (offset or 0))
- write(msg)
- end
- local function drawHelp()
- local sw,sh = term.getSize()
- local desc
- local function drawScrn(insert, key, name, desc)
- term.clear()
- cwrite("Press any key (except ESC) to see what it does",2)
- cwrite("Press enter to go back",sh-1)
- term.setCursorPos(4,sh/2-2)
- write("Key Code: "..(key or insert))
- term.setCursorPos(4,sh/2)
- write("Key Name: "..(name or insert))
- term.setCursorPos(4,sh/2+2)
- write("Key Function: "..(desc or insert))
- end
- drawScrn("No key pressed")
- while true do
- local e,k=os.pullEventRaw("key")
- if k == 28 or k == 156 then
- break
- else
- desc = "No binding to that key"
- if keyBindings[k] then -- this checks the key bindings table to see if the key is in there
- desc = keyBindings[k].desc
- end
- end
- drawScrn("Unknown", k, keys.getName(k), desc)
- end
- end
- loadKeybindings()
- -- main program loop
- while true do
- term.setBackgroundColor(2^bgColor)
- term.clear()
- term.setCursorPos(1,1)
- print("Hello, please press ? key")
- local event, key = os.pullEvent()
- if event == "key" then
- if keyBindings[key] then -- checks to see if the key that was pressed is in the key bindings table
- keyBindings[key].func() -- when it is, call the function associated with it
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement