Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- if not term.isColor() then
- error("Keyboard requires an advanced monitor")
- end
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- local chars = {}
- local Key = {}
- chars[true] = {}
- chars[false] = {}
- local lShiftPressed = false
- local rShiftPressed = false
- local capsLkPressed = false
- local scrlLkPressed = false
- local numLkPressed = true
- local lSuperPressed = false
- local rSuperPressed = false
- local lAltPressed = false
- local rALtPressed = false
- local lCtrlPressed = false
- local rCtrlPressed = false
- local terminateProgress = 0
- local rebootProgress = 0
- local shutdownProgress = 0
- --uppercase
- chars[true][2] = "!"
- chars[true][3] = "@"
- chars[true][4] = "#"
- chars[true][5] = "$"
- chars[true][6] = "%"
- chars[true][7] = "^"
- chars[true][8] = "&"
- chars[true][9] = "*"
- chars[true][10] = "("
- chars[true][11] = ")"
- chars[true][12] = "_"
- chars[true][13] = "+"
- chars[true][16] = "Q"
- chars[true][17] = "W"
- chars[true][18] = "E"
- chars[true][19] = "R"
- chars[true][20] = "T"
- chars[true][21] = "Y"
- chars[true][22] = "U"
- chars[true][23] = "I"
- chars[true][24] = "O"
- chars[true][25] = "P"
- chars[true][26] = "{"
- chars[true][27] = "}"
- chars[true][30] = "A"
- chars[true][31] = "S"
- chars[true][32] = "D"
- chars[true][33] = "F"
- chars[true][34] = "G"
- chars[true][35] = "H"
- chars[true][36] = "J"
- chars[true][37] = "K"
- chars[true][38] = "L"
- chars[true][39] = ":"
- chars[true][40] = "\""
- chars[true][41] = "~"
- chars[true][43] = "|"
- chars[true][44] = "Z"
- chars[true][45] = "X"
- chars[true][46] = "C"
- chars[true][47] = "V"
- chars[true][48] = "B"
- chars[true][49] = "N"
- chars[true][50] = "M"
- chars[true][51] = "<"
- chars[true][52] = ">"
- chars[true][53] = "?"
- --lowercase
- chars[false][2] = "1"
- chars[false][3] = "2"
- chars[false][4] = "3"
- chars[false][5] = "4"
- chars[false][6] = "5"
- chars[false][7] = "6"
- chars[false][8] = "7"
- chars[false][9] = "8"
- chars[false][10] = "9"
- chars[false][11] = "0"
- chars[false][12] = "-"
- chars[false][13] = "="
- chars[false][16] = "q"
- chars[false][17] = "w"
- chars[false][18] = "e"
- chars[false][19] = "r"
- chars[false][20] = "t"
- chars[false][21] = "y"
- chars[false][22] = "u"
- chars[false][23] = "i"
- chars[false][24] = "o"
- chars[false][25] = "p"
- chars[false][26] = "["
- chars[false][27] = "]"
- chars[false][30] = "a"
- chars[false][31] = "s"
- chars[false][32] = "d"
- chars[false][33] = "f"
- chars[false][34] = "g"
- chars[false][35] = "h"
- chars[false][36] = "j"
- chars[false][37] = "k"
- chars[false][38] = "l"
- chars[false][39] = ";"
- chars[false][40] = "'"
- chars[false][41] = "`"
- chars[false][43] = "\\"
- chars[false][44] = "z"
- chars[false][45] = "x"
- chars[false][46] = "c"
- chars[false][47] = "v"
- chars[false][48] = "b"
- chars[false][49] = "n"
- chars[false][50] = "m"
- chars[false][51] = ","
- chars[false][52] = "."
- chars[false][53] = "/"
- Key.meta = {}
- Key.meta.__index = {}
- local function xor(a, b)
- return (not a and b) or (not b and a)
- end
- function Key.meta.__index:draw()
- term.setBackgroundColor(colors.red)
- term.setCursorPos(self.x, self.y)
- term.write(self:text())
- term.setBackgroundColor(colors.black)
- end
- function Key.meta.__index:click(x, y)
- if self.y == y and self.x <= x and self.x + self:text():len() > x then
- os.queueEvent("key", self.id)
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- end
- end
- function Key.meta.__index:text()
- local t = self.textRaw
- if type(t) == "function" then
- t = t()
- end
- return t
- end
- function Key.meta.__index:char()
- return nil
- end
- function Key.new(x, y, id, text)
- local t = {["x"] = x, ["y"] = y, ["id"] = id, ["textRaw"] = text}
- setmetatable(t, Key.meta)
- return t
- end
- function Key.capital(x, y, id)
- local t = Key.new(x, y, id,
- function()
- return(chars[xor(lShiftPressed or rShiftPressed, capsLkPressed)][id])
- end
- )
- function t:click(x, y)
- if self.y == y and self.x <= x and self.x + self:text():len() > x then
- os.queueEvent("key", self.id)
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- lShiftPressed, rShiftPressed = false, false
- end
- end
- function t:char()
- return self:text()
- end
- return t
- end
- function Key.numpad(x, y, id, text)
- local t = Key.new(x, y, id, text)
- function t:click(x, y)
- if numLkPressed and self.y == y and self.x <= x and self.x + self:text():len() > x then
- os.queueEvent("key", self.id)
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- end
- end
- function t:char()
- if self:text() == "Enter" then return nil end return self:text()
- end
- return t
- end
- local Esc = Key.new(1, 1, 1, "Esc")
- local Key1 = Key.capital(3, 3, 2)
- local Key2 = Key.capital(5, 3, 3)
- local Key3 = Key.capital(7, 3, 4)
- local Key4 = Key.capital(9, 3, 5)
- local Key5 = Key.capital(11, 3, 6)
- local Key6 = Key.capital(13, 3, 7)
- local Key7 = Key.capital(15, 3, 8)
- local Key8 = Key.capital(17, 3, 9)
- local Key9 = Key.capital(19, 3, 10)
- local Key0 = Key.capital(21, 3, 11)
- local Minus = Key.capital(23, 3, 12)
- local Equals = Key.capital(25, 3, 13)
- local Backspace = Key.new(27, 3, 14, "Backspace")
- local Tab = Key.new(1, 5, 15, "Tab")
- local Q = Key.capital(5, 5, 16)
- local W = Key.capital(7, 5, 17)
- local E = Key.capital(9, 5, 18)
- local R = Key.capital(11, 5, 19)
- function R:click(x, y)
- if self.y == y and self.x <= x and self.x + self:text():len() > x then
- os.queueEvent("key", self.id)
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- lShiftPressed, rShiftPressed = false, false
- rebootProgress = rebootProgress + 1
- end
- end
- local T = Key.capital(13, 5, 20)
- function T:click(x, y)
- if self.y == y and self.x <= x and self.x + self:text():len() > x then
- os.queueEvent("key", self.id)
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- lShiftPressed, rShiftPressed = false, false
- terminateProgress = terminateProgress + 1
- end
- end
- local Y = Key.capital(15, 5, 21)
- local U = Key.capital(17, 5, 22)
- local I = Key.capital(19, 5, 23)
- local O = Key.capital(21, 5, 24)
- local P = Key.capital(23, 5, 25)
- local OpenSquareBracket = Key.capital(25, 5, 26)
- local ClosedSquareBracket = Key.capital(27, 5, 27)
- local Enter = Key.new(31, 7, 28, "Enter")
- local LCtrl = Key.new(1, 11, 29, "Ctrl")
- function LCtrl:click(x, y)
- if self.y == y and self.x <= x and self.x + self:text():len() > x then
- if not lCtrlPressed then os.queueEvent("key", self.id) end
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- lCtrlPressed = not lCtrlPressed
- end
- end
- function LCtrl:draw()
- if(not lCtrlPressed)then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.lime) end
- term.setCursorPos(self.x, self.y)
- term.write(self:text())
- term.setBackgroundColor(colors.black)
- end
- local A = Key.capital(9, 7, 30)
- local S = Key.capital(11, 7, 31)
- function S:click(x, y)
- if self.y == y and self.x <= x and self.x + self:text():len() > x then
- os.queueEvent("key", self.id)
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- lShiftPressed, rShiftPressed = false, false
- shutdownProgress = shutdownProgress + 1
- end
- end
- local D = Key.capital(13, 7, 32)
- local F = Key.capital(15, 7, 33)
- local G = Key.capital(17, 7, 34)
- local H = Key.capital(19, 7, 35)
- local J = Key.capital(21, 7, 36)
- local K = Key.capital(23, 7, 37)
- local L = Key.capital(25, 7, 38)
- local SemiColon = Key.capital(27, 7, 39)
- local Quote = Key.capital(29, 7, 40)
- local Grave = Key.capital(1, 3, 41)
- local LShift = Key.new(1, 9, 42, "Shift")
- function LShift:click(x, y)
- if self.y == y and self.x <= x and self.x + self:text():len() > x then
- if not lShiftPressed then os.queueEvent("key", self.id) end
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- lShiftPressed = not lShiftPressed
- end
- end
- function LShift:draw()
- if(not lShiftPressed)then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.lime) end
- term.setCursorPos(self.x, self.y)
- term.write(self:text())
- term.setBackgroundColor(colors.black)
- end
- local Backslash = Key.capital(29, 5, 43)
- local Z = Key.capital(7, 9, 44)
- local X = Key.capital(9, 9, 45)
- local C = Key.capital(11, 9, 46)
- local V = Key.capital(13, 9, 47)
- local B = Key.capital(15, 9, 48)
- local N = Key.capital(17, 9, 49)
- local M = Key.capital(19, 9, 50)
- local Comma = Key.capital(21, 9, 51)
- local Period = Key.capital(23, 9, 52)
- local Slash = Key.capital(25, 9, 53)
- local RShift = Key.new(27, 9, 54, "Shift")
- local NumAsterisk = Key.numpad(63, 3, 55, "*")
- local LCtrl = Key.new(1, 11, 29, "Ctrl")
- function LCtrl:click(x, y)
- if self.y == y and self.x <= x and self.x + self:text():len() > x then
- if not lCtrlPressed then os.queueEvent("key", self.id) end
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- lCtrlPressed = not lCtrlPressed
- end
- end
- function LCtrl:draw()
- if(not lCtrlPressed)then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.lime) end
- term.setCursorPos(self.x, self.y)
- term.write(self:text())
- term.setBackgroundColor(colors.black)
- end
- local A = Key.capital(9, 7, 30)
- local S = Key.capital(11, 7, 31)
- local D = Key.capital(13, 7, 32)
- local F = Key.capital(15, 7, 33)
- local G = Key.capital(17, 7, 34)
- local H = Key.capital(19, 7, 35)
- local J = Key.capital(21, 7, 36)
- local K = Key.capital(23, 7, 37)
- local L = Key.capital(25, 7, 38)
- local SemiColon = Key.capital(27, 7, 39)
- local Quote = Key.capital(29, 7, 40)
- local Grave = Key.capital(1, 3, 41)
- local LAlt = Key.new(8, 11, 56, "Alt")
- function LAlt:click(x, y)
- if self.y == y and self.x <= x and self.x + self:text():len() > x then
- if not lALtPressed then os.queueEvent("key", self.id) end
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- lAltPressed = not lAltPressed
- end
- end
- function LAlt:draw()
- if(not lAltPressed)then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.lime) end
- term.setCursorPos(self.x, self.y)
- term.write(self:text())
- term.setBackgroundColor(colors.black)
- end
- local Space = Key.new(12, 11, 57, " ")
- function Space:char()
- return " "
- end
- local CapsLk = Key.new(1, 7, 58, "Caps Lk")
- function CapsLk:click(x, y)
- if self.y == y and self.x <= x and self.x + self:text():len() > x then
- os.queueEvent("key", self.id)
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- capsLkPressed = not capsLkPressed
- end
- end
- function CapsLk:draw()
- if(not capsLkPressed)then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.lime) end
- term.setCursorPos(self.x, self.y)
- term.write(self:text())
- term.setBackgroundColor(colors.black)
- end
- local F1 = Key.new(5, 1, 59, "F1")
- local F2 = Key.new(8, 1, 60, "F2")
- local F3 = Key.new(11, 1, 61, "F3")
- local F4 = Key.new(14, 1, 62, "F4")
- local F5 = Key.new(17, 1, 63, "F5")
- local F6 = Key.new(20, 1, 64, "F6")
- local F7 = Key.new(23, 1, 65, "F7")
- local F8 = Key.new(26, 1, 66, "F8")
- local F9 = Key.new(29, 1, 67, "F9")
- local F10 = Key.new(32, 1, 68, "F10")
- local NumLk = Key.new(54, 3, 69, "Num Lk")
- function NumLk:click(x, y)
- if self.y == y and self.x <= x and self.x + self:text():len() > x then
- os.queueEvent("key", self.id)
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- numLkPressed = not numLkPressed
- end
- end
- function NumLk:draw()
- if(not numLkPressed)then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.lime) end
- term.setCursorPos(self.x, self.y)
- term.write(self:text())
- term.setBackgroundColor(colors.black)
- end
- local ScrollLk = Key.new(53, 1, 70, "Scrl Lk")
- function ScrollLk:click(x, y)
- if self.y == y and self.x <= x and self.x + self:text():len() > x then
- os.queueEvent("key", self.id)
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- scrlLkPressed = not scrlLkPressed
- end
- end
- function ScrollLk:draw()
- if(not scrlLkPressed)then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.lime) end
- term.setCursorPos(self.x, self.y)
- term.write(self:text())
- term.setBackgroundColor(colors.black)
- end
- local Num7 = Key.numpad(59, 5, 71, "7")
- local Num8 = Key.numpad(61, 5, 72, "8")
- local Num9 = Key.numpad(63, 5, 73, "9")
- local NumMinus = Key.numpad(65, 3, 74, "-")
- local Num4 = Key.numpad(59, 7, 75, "4")
- local Num5 = Key.numpad(61, 7, 76, "5")
- local Num6 = Key.numpad(63, 7, 77, "6")
- local NumPlus = Key.numpad(65, 5, 78, "+")
- local Num1 = Key.numpad(59, 9, 79, "1")
- local Num2 = Key.numpad(61, 9, 80, "2")
- local Num3 = Key.numpad(63, 9, 81, "3")
- local Num0 = Key.numpad(59, 11, 82, "0")
- local NumPeriod = Key.numpad(61, 11, 83, ".")
- local NumEnter = Key.numpad(63, 11, 28, "Enter")
- local F11 = Key.new(36, 1, 87, "F11")
- local F12 = Key.new(40, 1, 88, "F12")
- local RCtrl = Key.new(33, 11, 157, "Ctrl")
- function RCtrl:click(x, y)
- if self.y == y and self.x <= x and self.x + self:text():len() > x then
- if not rCtrlPressed then os.queueEvent("key", self.id) end
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- rCtrlPressed = not rCtrlPressed
- end
- end
- function RCtrl:draw()
- if(not rCtrlPressed)then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.lime) end
- term.setCursorPos(self.x, self.y)
- term.write(self:text())
- term.setBackgroundColor(colors.black)
- end
- local NumSlash = Key.numpad(61, 3, 181, "/")
- local PrntScrn = Key.new(44, 1, 183, "PrntScrn")
- local RAlt = Key.new(22, 11, 184, "Alt")
- function RAlt:click(x, y)
- if self.y == y and self.x <= x and self.x + self:text():len() > x then
- if not rALtPressed then os.queueEvent("key", self.id) end
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- rAltPressed = not rAltPressed
- end
- end
- function RAlt:draw()
- if(not rAltPressed)then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.lime) end
- term.setCursorPos(self.x, self.y)
- term.write(self:text())
- term.setBackgroundColor(colors.black)
- end
- local Pause = Key.new(61, 1, 197, "Pause")
- local Home = Key.new(41, 3, 199, "Home")
- local Up = Key.new(47, 9, 200, "^")
- local PgUp = Key.new(46, 3, 201, "Pg Up")
- local Left = Key.new(45, 11, 203, "<")
- local Right = Key.new(49, 11, 205, ">")
- local End = Key.new(41, 5, 207, "End")
- local Down = Key.new(47, 11, 208, "v")
- local PgDown = Key.new(46, 5, 209, "Pg Dn")
- local Ins = Key.new(37, 3, 210, "Ins")
- local Del = Key.new(37, 5, 211, "Del")
- local LSuper = Key.new(6, 11, 219, "#")
- function LSuper:click(x, y)
- if self.y == y and self.x <= x and self.x + self:text():len() > x then
- if not lSuperPressed then os.queueEvent("key", self.id) end
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- lSuperPressed = not lSuperPressed
- end
- end
- function LSuper:draw()
- if(not lSuperPressed)then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.lime) end
- term.setCursorPos(self.x, self.y)
- term.write(self:text())
- term.setBackgroundColor(colors.black)
- end
- local RSuper = Key.new(26, 11, 220, "#")
- function RSuper:click(x, y)
- if self.y == y and self.x <= x and self.x + self:text():len() > x then
- if not rSuperPressed then os.queueEvent("key", self.id) end
- if self:char() ~= nil then
- os.queueEvent("char", self:char())
- end
- rSuperPressed = not rSuperPressed
- end
- end
- function RSuper:draw()
- if(not rSuperPressed)then term.setBackgroundColor(colors.red) else term.setBackgroundColor(colors.lime) end
- term.setCursorPos(self.x, self.y)
- term.write(self:text())
- term.setBackgroundColor(colors.black)
- end
- local Menu = Key.new(28, 11, 221, "Menu")
- local Keys = {
- Esc, Key1, Key2, Key3, Key4
- , Key5, Key6, Key7, Key8, Key9
- , Key0, Minus, Equals, Backspace, Tab
- , Q, W, E, R, T
- , Y, U, I, O, P
- , OpenSquareBracket, ClosedSquareBracket, Enter, LCtrl, A
- , S, D, F, G, H
- , J, K, L, SemiColon, Quote
- , Grave, LShift, Backslash, Z, X
- , C, V, B, N, M
- , Comma, Period, Slash, RShift, NumAsterisk
- , LAlt, Space, CapsLk, F1, F2
- , F3, F4, F5, F6, F7
- , F8, F9, F10, NumLk, ScrollLk
- , Num7, Num8, Num9, NumMinus, Num4
- , Num5, Num6, NumPlus, Num1, Num2
- , Num3, Num0, NumPeriod, NumEnter, F11
- , F12, RCtrl, NumSlash, PrntScrn, RAlt
- , Pause, Home, Up, PgUp, Left
- , Right, End, Down, PgDown, Ins
- , Del, LSuper, RSuper, Menu
- }
- for k, v in pairs(Keys) do
- if type(v) == "table" then
- v:draw()
- end
- end
- local function hanldleMonitor()
- while true do
- event, side, x, y = os.pullEvent("monitor_touch")
- if side == tArgs[1] then
- --handle events
- for k, v in pairs(Keys) do
- v:click(x, y)
- end
- --redraw keys
- for k, v in pairs(Keys) do
- v:draw()
- end
- end
- end
- end
- local function queueEvents()
- while true do
- if lShiftPressed then os.queueEvent("key", LShift.id) end
- if rShiftPressed then os.queueEvent("key", RShift.id) end
- if lSuperPressed then os.queueEvent("key", LSuper.id) end
- if rSuperPressed then os.queueEvent("key", RSuper.id) end
- if lAltPressed then os.queueEvent("key", LAlt.id) end
- if rALtPressed then os.queueEvent("key", RAlt.id) end
- if lCtrlPressed then os.queueEvent("key", LCtrl.id) end
- if rCtrlPressed then os.queueEvent("key", RCtrl.id) end
- sleep(0.3)
- end
- end
- local function handleReboot()
- while true do
- if terminateProgress >= 5 then os.queueEvent("terminate") end
- if rebootProgress >= 5 then os.reboot() end
- if shutdownProgress >= 5 then os.shutdown() end
- terminateProgress = 0
- rebootProgress = 0
- shutdownProgress = 0
- sleep(1)
- end
- end
- parallel.waitForAny(hanldleMonitor, queueEvents, handleReboot)
Advertisement
Add Comment
Please, Sign In to add comment