Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------
- --Init:
- -------
- --API-Test
- if ( qbutton == nil ) then
- error("The QKeyboard API requires the QButton API! (Reboot to fix half-loaded APIs)", 0)
- end
- --Local Functions
- local getKeyboard, replaceSpecial
- --Local Vars
- local valid_attributes = {"layout", "position", "size", "offset", "color", "side"}
- local keyboard = {}
- ----------
- --Methods:
- ----------
- setKeyboard = function(...)
- --Input
- local input = {...}
- local default = {"default", {"1234567890", "qwertzuiop", "asdfghjkl/", "yxcvbnm,.-"}, {2, 2}, {3, 3}, {1, 1}, {"d", "a"}, qmain.getScreen()}
- --Parameters
- local name = input[1] or default[1]
- --Method
- keyboard[name] = {
- layout = input[2] or default[2],
- position = input[3] or default[3],
- size = input[4] or default[4],
- offset = input[5] or default[5],
- color = input[6] or default[6],
- side = input[7] or default[7],
- input = ""
- }
- end
- changeKeyboard = function(...)
- --Input
- local input = {...}
- local default = {"default", "color", {"a", "j"}}
- --Parameters
- local keyboard_name = input[1] or default[1]
- local value_name = input[2] or default[2]
- local new_value = input[3] or default[3]
- --Method
- local data = getKeyboard(keyboard_name)
- if not( qmain.isIn(value_name, valid_attributes) ) then
- error(value_name.." is not a valid value for keyboard "..keyboard_name..".", 0)
- else
- data[value_name] = new_value
- end
- end
- drawKeyboard = function(...)
- --Input
- local input = {...}
- local default = {"default"}
- --Parameters
- local name = input[1] or default[1]
- local data = getKeyboard(name)
- --Method
- for row = 1, #data.layout do
- local row_data = data.layout[row]
- for column = 1, row_data:len() do
- local key_symbol = row_data:sub(column, column)
- local button_name = "keyboard_"..name.."_key_"..replaceSpecial(key_symbol)
- qbutton.setButton(
- button_name,
- key_symbol,
- qkeyboard.setInput,
- {name, key_symbol},
- {
- data.position[1] + (column - 1) * (data.size[1] + data.offset[1]),
- data.position[2] + (row - 1) * (data.size[2] + data.offset[2])
- },
- data.size,
- data.color,
- data.side
- )
- qbutton.drawButton(button_name)
- end
- end
- end
- getInput = function(...)
- --Input
- local input = {...}
- local default = {"default"}
- --Parameters
- local name = input[1] or default[1]
- --Method
- return getKeyboard(name).input
- end
- ----------
- --Private:
- ----------
- setInput = function(...)
- --Input
- local input = {...}
- local default = {"default", "Text"}
- --Parameters
- local name = input[1] or default[1]
- local new_value = input[2] or default[2]
- --Method
- getKeyboard(name).input = new_value
- end
- getKeyboard = function(name)
- if (name == nil) then
- return keyboard
- elseif (keyboard[name] == nil) then
- error("The Keyboard "..name.." has not been set. Use qkeyboard.setKeyboard(...) first", 0)
- else
- return keyboard[name]
- end
- end
- replaceSpecial = function(symbol)
- replacement = {
- ["^"] = "caret",
- ["!"] = "exclamation",
- ["$"] = "dollar",
- ["%"] = "percent",
- ["&"] = "and",
- ["/"] = "slash",
- ["("] = "bracket_open",
- [")"] = "bracket_close",
- ["["] = "squarebracket_open",
- ["]"] = "squarebracket_close",
- ["{"] = "curlybracket_open",
- ["}"] = "curlybracket_close",
- ["<"] = "less_than",
- [">"] = "greater_than",
- ["="] = "equal",
- ["?"] = "question",
- ["+"] = "plus",
- ["*"] = "star",
- ["~"] = "tilde",
- ["#"] = "hash",
- ["-"] = "line",
- ["_"] = "underline",
- ["."] = "dot",
- [":"] = "colon",
- [","] = "comma",
- [";"] = "semicolon",
- ["@"] = "at",
- ["|"] = "pipe",
- [" "] = "space",
- ["'"] = "apostrophe"
- }
- if (replacement[symbol] == nil) then
- if (symbol:match("%w") == nil) then
- error("This is no valid symbol for a key: "..symbol, 0)
- else
- return symbol
- end
- else
- return replacement[symbol]
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement