Advertisement
Qivex

QKeyboard-API

Mar 28th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.83 KB | None | 0 0
  1. -------
  2. --Init:
  3. -------
  4.  
  5. --API-Test
  6. if ( qbutton == nil ) then
  7.     error("The QKeyboard API requires the QButton API! (Reboot to fix half-loaded APIs)", 0)
  8. end
  9.  
  10. --Local Functions
  11. local getKeyboard, replaceSpecial
  12.  
  13. --Local Vars
  14. local valid_attributes = {"layout", "position", "size", "offset", "color", "side"}
  15. local keyboard = {}
  16.  
  17.  
  18. ----------
  19. --Methods:
  20. ----------
  21.  
  22. setKeyboard = function(...)
  23.     --Input
  24.     local input = {...}
  25.     local default = {"default", {"1234567890", "qwertzuiop", "asdfghjkl/", "yxcvbnm,.-"}, {2, 2}, {3, 3}, {1, 1}, {"d", "a"}, qmain.getScreen()}
  26.     --Parameters
  27.     local name = input[1] or default[1]
  28.     --Method
  29.     keyboard[name] = {
  30.         layout = input[2] or default[2],
  31.         position = input[3] or default[3],
  32.         size = input[4] or default[4],
  33.         offset = input[5] or default[5],
  34.         color = input[6] or default[6],
  35.         side = input[7] or default[7],
  36.         input = ""
  37.     }
  38. end
  39.  
  40.  
  41. changeKeyboard = function(...)
  42.     --Input
  43.     local input = {...}
  44.     local default = {"default", "color", {"a", "j"}}
  45.     --Parameters
  46.     local keyboard_name = input[1] or default[1]
  47.     local value_name = input[2] or default[2]
  48.     local new_value = input[3] or default[3]
  49.     --Method
  50.     local data = getKeyboard(keyboard_name)
  51.     if not( qmain.isIn(value_name, valid_attributes) ) then
  52.         error(value_name.." is not a valid value for keyboard "..keyboard_name..".", 0)
  53.     else
  54.         data[value_name] = new_value
  55.     end
  56. end
  57.  
  58.  
  59. drawKeyboard = function(...)
  60.     --Input
  61.     local input = {...}
  62.     local default = {"default"}
  63.     --Parameters
  64.     local name = input[1] or default[1]
  65.     local data = getKeyboard(name)
  66.     --Method
  67.     for row = 1, #data.layout do
  68.         local row_data = data.layout[row]
  69.         for column = 1, row_data:len() do
  70.             local key_symbol = row_data:sub(column, column)
  71.             local button_name = "keyboard_"..name.."_key_"..replaceSpecial(key_symbol)
  72.             qbutton.setButton(
  73.                 button_name,
  74.                 key_symbol,
  75.                 qkeyboard.setInput,
  76.                 {name, key_symbol},
  77.                 {
  78.                     data.position[1] + (column - 1) * (data.size[1] + data.offset[1]),
  79.                     data.position[2] + (row - 1) * (data.size[2] + data.offset[2])
  80.                 },
  81.                 data.size,
  82.                 data.color,
  83.                 data.side
  84.             )
  85.             qbutton.drawButton(button_name)
  86.         end
  87.     end
  88. end
  89.  
  90.  
  91. getInput = function(...)
  92.     --Input
  93.     local input = {...}
  94.     local default = {"default"}
  95.     --Parameters
  96.     local name = input[1] or default[1]
  97.     --Method
  98.     return getKeyboard(name).input
  99. end
  100.  
  101.  
  102. ----------
  103. --Private:
  104. ----------
  105.  
  106. setInput = function(...)
  107.     --Input
  108.     local input = {...}
  109.     local default = {"default", "Text"}
  110.     --Parameters
  111.     local name = input[1] or default[1]
  112.     local new_value = input[2] or default[2]
  113.     --Method
  114.     getKeyboard(name).input = new_value
  115. end
  116.  
  117.  
  118. getKeyboard = function(name)
  119.     if (name == nil) then
  120.         return keyboard
  121.     elseif (keyboard[name] == nil) then
  122.         error("The Keyboard "..name.." has not been set. Use qkeyboard.setKeyboard(...) first", 0)
  123.     else
  124.         return keyboard[name]
  125.     end
  126. end
  127.  
  128.  
  129. replaceSpecial = function(symbol)
  130.     replacement = {
  131.         ["^"] = "caret",
  132.         ["!"] = "exclamation",
  133.         ["$"] = "dollar",
  134.         ["%"] = "percent",
  135.         ["&"] = "and",
  136.         ["/"] = "slash",
  137.         ["("] = "bracket_open",
  138.         [")"] = "bracket_close",
  139.         ["["] = "squarebracket_open",
  140.         ["]"] = "squarebracket_close",
  141.         ["{"] = "curlybracket_open",
  142.         ["}"] = "curlybracket_close",
  143.         ["<"] = "less_than",
  144.         [">"] = "greater_than",
  145.         ["="] = "equal",
  146.         ["?"] = "question",
  147.         ["+"] = "plus",
  148.         ["*"] = "star",
  149.         ["~"] = "tilde",
  150.         ["#"] = "hash",
  151.         ["-"] = "line",
  152.         ["_"] = "underline",
  153.         ["."] = "dot",
  154.         [":"] = "colon",
  155.         [","] = "comma",
  156.         [";"] = "semicolon",
  157.         ["@"] = "at",
  158.         ["|"] = "pipe",
  159.         [" "] = "space",
  160.         ["'"] = "apostrophe"
  161.     }
  162.     if (replacement[symbol] == nil) then
  163.         if (symbol:match("%w") == nil) then
  164.             error("This is no valid symbol for a key: "..symbol, 0)
  165.         else
  166.             return symbol
  167.         end
  168.     else
  169.         return replacement[symbol]
  170.     end
  171. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement