Advertisement
Guest User

keyboard

a guest
Apr 24th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.76 KB | None | 0 0
  1. -- On-Screen Keyboard (Terminal)
  2. -- Created By DannySMc
  3. -- Platform: Lua Virtual Machine
  4.  
  5. -- Common Draw Functions
  6. function cs()
  7.     term.clear()
  8.     term.setCursorPos(1,1)
  9.     return
  10. end
  11.  
  12. function setCol(textColour, backgroundColour)
  13.     if textColour and backgroundColour then
  14.         if term.isColour() then
  15.             term.setTextColour(colours[textColour])
  16.             term.setBackgroundColour(colours[backgroundColour])
  17.             return true
  18.         else
  19.             return false
  20.         end
  21.     else
  22.         return false
  23.     end
  24. end
  25.  
  26. function resetCol()
  27.     if term.isColour then
  28.         term.setTextColour(colours.white)
  29.         term.setBackgroundColour(colours.black)
  30.         return true
  31.     else
  32.         return false
  33.     end
  34. end
  35.  
  36. -- Print Functions
  37. function printC(Text, Line, NextLine, Color, BkgColor) -- print centered
  38.   local x, y = term.getSize()
  39.   x = x/2 - #Text/2
  40.   term.setCursorPos(x, Line)
  41.   if Color then setCol(Color, BkgColor) end
  42.   term.write(Text)
  43.   if NextLine then
  44.     term.setCursorPos(1, NextLine)
  45.   end
  46.   if Color then resetCol(Color, BkgColor) end
  47.   return true  
  48. end
  49.  
  50. function printA(Text, xx, yy, NextLine, Color, BkgColor) -- print anywhere
  51.   term.setCursorPos(xx,yy)
  52.   if Color then setCol(Color, BkgColor) end
  53.   term.write(Text)
  54.   if NextLine then  
  55.     term.setCursorPos(1, NextLine)
  56.   end
  57.   if Color then resetCol(Color, BkgColor) end
  58.   return true  
  59. end
  60.  
  61. function drawBox(StartX, lengthX, StartY, lengthY, Text, Color, BkgColor) -- does what is says on the tin.
  62.   local x, y = term.getSize()
  63.   if Color then setCol(Color, BkgColor) end
  64.   if not Text then Text = "*" end
  65.   lengthX = lengthX - 1
  66.   lengthY = lengthY - 1
  67.   EndX = StartX + lengthX  
  68.   EndY = StartY + lengthY
  69.   term.setCursorPos(StartX, StartY)
  70.   term.write(string.rep(Text, lengthX))
  71.   term.setCursorPos(StartX, EndY)
  72.   term.write(string.rep(Text, lengthX))
  73.   for i = StartY, EndY do
  74.     term.setCursorPos(StartX, i)
  75.     term.write(Text)
  76.     term.setCursorPos(EndX, i)    
  77.     term.write(Text)
  78.   end
  79.   resetCol(Color, BkgColor)
  80.   return true  
  81. end
  82.  
  83. -- Start Code:
  84. kbtc = "white"
  85. kbbc = "blue"
  86. keyText = "black"
  87. keyBack = "white"
  88. sKeyVer = 1.1
  89. keyOutput = {}
  90. capsToggle = false
  91.  
  92. function drawKeyboard()
  93.     tRow1 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "(", ")", "F1", " BACKSPACE",}
  94.     tRow2 = {"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", ":", "_", "F2", " ENTER/ OK",}
  95.     tRow3 = {"A", "S", "D", "F", "G", "H", "J", "K", "L", "#", ";", "=", "F3", " CAPS LOCK",}
  96.     tRow4 = {"/", "Z", "X", "C", "V", "B", "N", "M", "@", "~", "'", "+", "F4", " ---------",}
  97.     tRow5 = {"[", "]", "{", "}", "&", "$", "%", "-", "*", "!", "?", ".", "F5", " >DannySMc",}
  98.  
  99.     local intX, intY = term.getCursorPos()
  100.     drawBox(8, 38, 11, 3, " ", kbtc, kbbc)
  101.     drawBox(8, 38, 13, 7, " ", kbtc, kbbc)
  102.     drawBox(9, 36, 12, 1, " ", keyText, keyBack)
  103.     drawBox(9, 36, 14, 2, " ", keyText, keyBack)
  104.     drawBox(9, 36, 16, 2, " ", keyText, keyBack)
  105.     drawBox(9, 36, 18, 1, " ", keyText, keyBack)
  106.     printA("SPACE", 23, 19, false, keyText, keyBack)
  107.     setCol(keyText, keyBack)
  108.  
  109.     i = 9
  110.     for _, v in ipairs(tRow1) do
  111.         term.setCursorPos(i,14)
  112.         write(v)
  113.         i = i + 2
  114.     end
  115.     i = 9
  116.     for _,v in ipairs(tRow2) do
  117.         term.setCursorPos(i,15)
  118.         write(v)
  119.         i = i + 2
  120.     end
  121.     i = 9
  122.     for _,v in ipairs(tRow3) do
  123.         term.setCursorPos(i,16)
  124.         write(v)
  125.         i = i + 2
  126.     end
  127.     i = 9
  128.     for _,v in ipairs(tRow4) do
  129.         term.setCursorPos(i,17)
  130.         write(v)
  131.         i = i + 2
  132.     end
  133.     i = 9
  134.     for _,v in ipairs(tRow5) do
  135.         term.setCursorPos(i,18)
  136.         write(v)
  137.         i = i + 2
  138.     end
  139.     resetCol()
  140.     printC("   Keyboard (Ver: "..tostring(sKeyVer)..") - DannySMc", 13, 1, "red", kbbc)
  141.     setCol("black", "white")
  142.     term.setCursorPos(9, 12)
  143.     write(": ")
  144.     resetCol()
  145.     setCol("black", "white")
  146.     term.setCursorPos(11, 12)
  147. end
  148.  
  149. function inputKeyboard()
  150.     drawKeyboard()
  151.     i = 11
  152.     while true do
  153.         local args = { os.pullEvent() }
  154.         if args[1] == "monitor_touch" or args[1] == "mouse_click" then
  155.             -- Check Y value first!
  156.             if args[4] == 14 then
  157.                 -- Check X value
  158.                 if args[3] == 9 then
  159.                     addCharacter(tRow1[1])
  160.                 elseif args[3] == 11 then
  161.                     addCharacter(tRow1[2])
  162.                 elseif args[3] == 13 then
  163.                     addCharacter(tRow1[3])
  164.                 elseif args[3] == 15 then
  165.                     addCharacter(tRow1[4])
  166.                 elseif args[3] == 17 then
  167.                     addCharacter(tRow1[5])
  168.                 elseif args[3] == 19 then
  169.                     addCharacter(tRow1[6])
  170.                 elseif args[3] == 21 then
  171.                     addCharacter(tRow1[7])
  172.                 elseif args[3] == 23 then
  173.                     addCharacter(tRow1[8])
  174.                 elseif args[3] == 25 then
  175.                     addCharacter(tRow1[9])
  176.                 elseif args[3] == 27 then
  177.                     addCharacter(tRow1[10])
  178.                 elseif args[3] == 29 then
  179.                     addCharacter(tRow1[11])
  180.                 elseif args[3] == 31 then
  181.                     addCharacter(tRow1[12])
  182.                 elseif args[3] >= 33 and args[3] <= 34 then
  183.                     return specialKeys(1)
  184.                 elseif args[3] >= 36 and args[3] <= 44 then
  185.                     backSpace()
  186.                 end
  187.             elseif args[4] == 15 then
  188.                 -- check x value
  189.                 if args[3] == 9 then
  190.                     addCharacter(tRow2[1])
  191.                 elseif args[3] == 11 then
  192.                     addCharacter(tRow2[2])
  193.                 elseif args[3] == 13 then
  194.                     addCharacter(tRow2[3])
  195.                 elseif args[3] == 15 then
  196.                     addCharacter(tRow2[4])
  197.                 elseif args[3] == 17 then
  198.                     addCharacter(tRow2[5])
  199.                 elseif args[3] == 19 then
  200.                     addCharacter(tRow2[6])
  201.                 elseif args[3] == 21 then
  202.                     addCharacter(tRow2[7])
  203.                 elseif args[3] == 23 then
  204.                     addCharacter(tRow2[8])
  205.                 elseif args[3] == 25 then
  206.                     addCharacter(tRow2[9])
  207.                 elseif args[3] == 27 then
  208.                     addCharacter(tRow2[10])
  209.                 elseif args[3] == 29 then
  210.                     addCharacter(tRow2[11])
  211.                 elseif args[3] == 31 then
  212.                     addCharacter(tRow2[12])
  213.                 elseif args[3] >= 33 and args[3] <= 34 then
  214.                     return specialKeys(2)
  215.                 elseif args[3] >= 36 and args[3] <= 44 then
  216.                     return enter_ok()
  217.                 end
  218.             elseif args[4] == 16 then
  219.                 -- check x value
  220.                 if args[3] == 9 then
  221.                     addCharacter(tRow3[1])
  222.                 elseif args[3] == 11 then
  223.                     addCharacter(tRow3[2])
  224.                 elseif args[3] == 13 then
  225.                     addCharacter(tRow3[3])
  226.                 elseif args[3] == 15 then
  227.                     addCharacter(tRow3[4])
  228.                 elseif args[3] == 17 then
  229.                     addCharacter(tRow3[5])
  230.                 elseif args[3] == 19 then
  231.                     addCharacter(tRow3[6])
  232.                 elseif args[3] == 21 then
  233.                     addCharacter(tRow3[7])
  234.                 elseif args[3] == 23 then
  235.                     addCharacter(tRow3[8])
  236.                 elseif args[3] == 25 then
  237.                     addCharacter(tRow3[9])
  238.                 elseif args[3] == 27 then
  239.                     addCharacter(tRow3[10])
  240.                 elseif args[3] == 29 then
  241.                     addCharacter(tRow3[11])
  242.                 elseif args[3] == 31 then
  243.                     addCharacter(tRow3[12])
  244.                 elseif args[3] >= 33 and args[3] <= 34 then
  245.                     return specialKeys(3)
  246.                 elseif args[3] >= 36 and args[3] <= 44 then
  247.                     caps_lock()
  248.                 end
  249.             elseif args[4] == 17 then
  250.                 -- check x value
  251.                 if args[3] == 9 then
  252.                     addCharacter(tRow4[1])
  253.                 elseif args[3] == 11 then
  254.                     addCharacter(tRow4[2])
  255.                 elseif args[3] == 13 then
  256.                     addCharacter(tRow4[3])
  257.                 elseif args[3] == 15 then
  258.                     addCharacter(tRow4[4])
  259.                 elseif args[3] == 17 then
  260.                     addCharacter(tRow4[5])
  261.                 elseif args[3] == 19 then
  262.                     addCharacter(tRow4[6])
  263.                 elseif args[3] == 21 then
  264.                     addCharacter(tRow4[7])
  265.                 elseif args[3] == 23 then
  266.                     addCharacter(tRow4[8])
  267.                 elseif args[3] == 25 then
  268.                     addCharacter(tRow4[9])
  269.                 elseif args[3] == 27 then
  270.                     addCharacter(tRow4[10])
  271.                 elseif args[3] == 29 then
  272.                     addCharacter(tRow4[11])
  273.                 elseif args[3] == 31 then
  274.                     addCharacter(tRow4[12])
  275.                 elseif args[3] >= 33 and args[3] <= 34 then
  276.                     return specialKeys(4)
  277.                 elseif args[3] >= 36 and args[3] <= 44 then
  278.                     extraInfo()
  279.                 end
  280.             elseif args[4] == 18 then
  281.                 -- check x value
  282.                 if args[3] == 9 then
  283.                     addCharacter(tRow5[1])
  284.                 elseif args[3] == 11 then
  285.                     addCharacter(tRow5[2])
  286.                 elseif args[3] == 13 then
  287.                     addCharacter(tRow5[3])
  288.                 elseif args[3] == 15 then
  289.                     addCharacter(tRow5[4])
  290.                 elseif args[3] == 17 then
  291.                     addCharacter(tRow5[5])
  292.                 elseif args[3] == 19 then
  293.                     addCharacter(tRow5[6])
  294.                 elseif args[3] == 21 then
  295.                     addCharacter(tRow5[7])
  296.                 elseif args[3] == 23 then
  297.                     addCharacter(tRow5[8])
  298.                 elseif args[3] == 25 then
  299.                     addCharacter(tRow5[9])
  300.                 elseif args[3] == 27 then
  301.                     addCharacter(tRow5[10])
  302.                 elseif args[3] == 29 then
  303.                     addCharacter(tRow5[11])
  304.                 elseif args[3] == 31 then
  305.                     addCharacter(tRow5[12])
  306.                 elseif args[3] >= 33 and args[3] <= 34 then
  307.                     return specialKeys(5)
  308.                 elseif args[3] >= 36 and args[3] <= 44 then
  309.                     instructions()
  310.                 end
  311.             elseif args[4] == 19 then
  312.                 -- check x value
  313.                 if args[3] >= 23 and args[3] <= 27 then
  314.                     addCharacter(" ")
  315.                 end
  316.             end
  317.         end
  318.     end
  319. end
  320.  
  321. function addCharacter(charInput)
  322.     if charInput == " " then
  323.         setCol("black", "white")
  324.         table.insert(keyOutput, charInput)
  325.         term.setCursorPos(i, 12)
  326.         write(charInput)
  327.         i = i + 1
  328.         resetCol()
  329.     else
  330.         if capsToggle == true then
  331.             setCol("black", "white")
  332.             table.insert(keyOutput, string.upper(charInput))
  333.             term.setCursorPos(i, 12)
  334.             write(string.upper(charInput))
  335.             i = i + 1
  336.             resetCol()
  337.         elseif capsToggle == false then
  338.             setCol("black", "white")
  339.             table.insert(keyOutput, string.lower(charInput))
  340.             term.setCursorPos(i, 12)
  341.             write(string.lower(charInput))
  342.             i = i + 1
  343.             resetCol()
  344.         end
  345.     end
  346. end
  347.  
  348. function backSpace()
  349.     setCol("black", "white")
  350.     table.remove(keyOutput)
  351.     i = i - 1
  352.     term.setCursorPos(i,12)
  353.     write(" ")
  354.     term.setCursorPos(i,12)
  355.     resetCol()
  356. end
  357.  
  358. function caps_lock()
  359.     if capsToggle == true then
  360.         term.setCursorPos(36,16)
  361.         setCol("black", "white")
  362.         write("CAPS LOCK")
  363.         capsToggle = false
  364.     elseif capsToggle == false then
  365.         term.setCursorPos(36,16)
  366.         setCol("red", "white")
  367.         write("CAPS LOCK")
  368.         capsToggle = true
  369.     end
  370. end
  371.  
  372. function enter_ok()
  373.     returnString = table.concat(keyOutput)
  374.     return returnString
  375. end
  376.  
  377. function specialKeys(nKey)
  378.     return nKey
  379. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement