Advertisement
theoriginalbit

ccConfig: Example — Key Bindings

Feb 6th, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.54 KB | None | 0 0
  1. os.loadAPI("/ccConfig")
  2.  
  3. local bgColor = 1
  4. local keyBindings = {}
  5. local config = ccConfig.new("keybindings_example.cfg", "Keybinding Example")
  6.  
  7. local function loadKeybindings(settings)
  8.   config:load()
  9.  
  10.   -- this is the Q key
  11.   keyBindings[config:getNumber("program.terminate", 16)] = {desc = "Closes the program", func = function() term.clear() term.setCursorPos(1,1) print("Quit") error() end}
  12.   config:addCommentForKey("program.terminate", "Key to terminate the program")
  13.  
  14.   -- this is the / or ? key (they have same key code)
  15.   keyBindings[config:getNumber("program.help", 53)] = {desc = "Shows the help screen, but you know that", func = drawHelp }
  16.   config:addCommentForKey("program.help", "Key to show the programs help screen")
  17.  
  18.   -- this is the C key
  19.   keyBindings[config:getNumber("program.changeColor", 46)] = {desc = "Changes the screens background color", func = function() bgColor = (bgColor + 1) % 15 end}
  20.   config:addCommentForKey("program.help", "Key to show the programs help screen")
  21.  
  22.   -- more key bindings would be added in here
  23.  
  24.   config:save()
  25. end
  26.  
  27. local function cwrite(msg, y, offset)
  28.   local sw,sh = term.getSize()
  29.   term.setCursorPos(sw/2-#msg/2, (y or (sh/2)) + (offset or 0))
  30.   write(msg)
  31. end
  32.  
  33. local function drawHelp()
  34.   local sw,sh = term.getSize()
  35.   local desc
  36.  
  37.   local function drawScrn(insert, key, name, desc)
  38.     term.clear()
  39.     cwrite("Press any key (except ESC) to see what it does",2)
  40.     cwrite("Press enter to go back",sh-1)
  41.     term.setCursorPos(4,sh/2-2)
  42.     write("Key Code: "..(key or insert))
  43.     term.setCursorPos(4,sh/2)
  44.     write("Key Name: "..(name or insert))
  45.     term.setCursorPos(4,sh/2+2)
  46.     write("Key Function: "..(desc or insert))
  47.   end
  48.  
  49.   drawScrn("No key pressed")
  50.  
  51.   while true do
  52.    
  53.     local e,k=os.pullEventRaw("key")
  54.    
  55.     if k == 28 or k == 156 then
  56.       break
  57.     else
  58.       desc = "No binding to that key"
  59.       if keyBindings[k] then -- this checks the key bindings table to see if the key is in there
  60.         desc = keyBindings[k].desc
  61.       end
  62.     end
  63.     drawScrn("Unknown", k, keys.getName(k), desc)
  64.   end
  65. end
  66.  
  67. loadKeybindings()
  68.  
  69. -- main program loop
  70. while true do
  71.   term.setBackgroundColor(2^bgColor)
  72.   term.clear()
  73.   term.setCursorPos(1,1)
  74.   print("Hello, please press ? key")
  75.  
  76.   local event, key = os.pullEvent()
  77.  
  78.   if event == "key" then
  79.     if keyBindings[key] then -- checks to see if the key that was pressed is in the key bindings table
  80.       keyBindings[key].func() -- when it is, call the function associated with it
  81.     end
  82.   end
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement