Advertisement
Tag365

Keyboard script

May 1st, 2015
1,605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. -- Keyboard script by Tag365 --
  2. -- Free to use, edit, and copy. --
  3. local nKbWid, nKbHei = 80, 18
  4. local tKeyboard = {} -- This houses the keyboard keys.
  5.  
  6. -- Set up keyboard
  7. tKeyboard[1] = {}
  8. tKeyboard[1].y = 1/nKbHei
  9. tKeyboard[1].height = 1/nKbHei
  10. tKeyboard[1].keys = {
  11. {"esc", 1, 4, nil},
  12. {"f1", 5, 8, keys.f1},
  13. {"f2", 9, 12, keys.f2},
  14. {"f3", 13, 16, keys.f3},
  15. {"f4", 17, 20, keys.f4},
  16. {"f5", 21, 24, keys.f5},
  17. {"f6", 25, 28, keys.f6},
  18. {"f7", 29, 32, keys.f7},
  19. {"f8", 33, 36, keys.f8},
  20. {"f9", 37, 40, keys.f9},
  21. {"f10", 41, 44, keys.f10},
  22. {"f11", 45, 48, keys.f11},
  23. {"f12", 49, 52, keys.f12},
  24. {"ins", 53, 56, keys.insert},
  25. {"del", 57, 60, keys.delete},
  26. {"prnt", 63, 67, keys.home},
  27. {"sclk", 68, 71, keys.scrollLock},
  28. {"paus", 72, 76, keys.pause},
  29. {"brk", 77, 80, -1},
  30. }
  31.  
  32. tKeyboard[2] = {}
  33. tKeyboard[2].y = 4/nKbHei
  34. tKeyboard[2].height = 2/nKbHei
  35. tKeyboard[2].keys = {
  36. {"~", 1, 4, keys.grave},
  37. {"1", 5, 8, keys.one},
  38. {"2", 9, 12, keys.two},
  39. {"3", 13, 16, keys.three},
  40. {"4", 17, 20, keys.four},
  41. {"5", 21, 24, keys.five},
  42. {"6", 25, 28, keys.six},
  43. {"7", 29, 32, keys.seven},
  44. {"8", 33, 36, keys.eight},
  45. {"9", 37, 40, keys.nine},
  46. {"0", 41, 44, keys.ten},
  47. {"-", 45, 48, keys.minus},
  48. {"=", 49, 52, keys.equals},
  49. {"<-", 53, 60, keys.backspace},
  50. {"nmlk", 63, 67, nil},
  51. {"/", 68, 71, keys.slash},
  52. {"*", 72, 76, keys.multiply},
  53. {"-", 77, 80, keys.minus},
  54. }
  55.  
  56. tKeyboard[3] = {}
  57. tKeyboard[3].y = 7/nKbHei
  58. tKeyboard[3].height = 2/nKbHei
  59. tKeyboard[3].keys = {
  60. {"tab", 1, 4, keys.tab},
  61. {"Q", 5, 8, keys.q},
  62. {"W", 9, 12, keys.w},
  63. {"E", 13, 16, keys.e},
  64. {"R", 17, 20, keys.r},
  65. {"T", 21, 24, keys.t},
  66. {"Y", 25, 28, keys.y},
  67. {"U", 29, 32, keys.u},
  68. {"I", 33, 36, keys.i},
  69. {"O", 37, 40, keys.o},
  70. {"P", 41, 44, keys.p},
  71. {"[", 45, 48, keys.leftBracket},
  72. {"]", 49, 52, keys.rightBracket},
  73. {"\\", 53, 60, keys.backslash},
  74. {"home", 63, 67, keys.home},
  75. {"up", 68, 71, keys.up},
  76. {"pgup", 72, 76, keys.pageUp},
  77. {"+", 77, 80, keys.plus},
  78. }
  79.  
  80. tKeyboard[4] = {}
  81. tKeyboard[4].y = 10/nKbHei
  82. tKeyboard[4].height = 2/nKbHei
  83. tKeyboard[4].keys = {
  84. {"caps", 1, 4, keys.capsLock},
  85. {"A", 5, 8, keys.a},
  86. {"S", 9, 12, keys.s},
  87. {"D", 13, 16, keys.d},
  88. {"F", 17, 20, keys.f},
  89. {"G", 21, 24, keys.g},
  90. {"H", 25, 28, keys.h},
  91. {"J", 29, 32, keys.j},
  92. {"K", 33, 36, keys.k},
  93. {"L", 37, 40, keys.l},
  94. {";", 41, 44, keys.semiColon},
  95. {"'", 45, 48, keys.apostrophe},
  96. {"enter", 49, 60, keys.enter},
  97. {"left", 63, 67, keys.left},
  98. {"5", 68, 71, keys.numPad5},
  99. {"righ", 72, 76, keys.right},
  100. {"+", 77, 80, keys.numPadAdd},
  101. }
  102.  
  103. tKeyboard[5] = {}
  104. tKeyboard[5].y = 13/nKbHei
  105. tKeyboard[5].height = 2/nKbHei
  106. tKeyboard[5].keys = {
  107. {"shif", 1, 4, keys.leftShift},
  108. {"Z", 5, 8, keys.z},
  109. {"X", 9, 12, keys.x},
  110. {"C", 13, 16, keys.c},
  111. {"V", 17, 20, keys.v},
  112. {"B", 21, 24, keys.b},
  113. {"N", 25, 28, keys.n},
  114. {"M", 29, 32, keys.m},
  115. {",", 33, 36, keys.comma},
  116. {".", 37, 40, keys.period},
  117. {"/", 41, 44, keys.slash},
  118. {"shift", 45, 60, keys.rightShift},
  119. {"end", 63, 67, keys["end"]},
  120. {"down", 68, 71, keys.down},
  121. {"pgdn", 72, 76, keys.pageDown},
  122. {"ent", 77, 80, keys.numPadEnter},
  123. }
  124.  
  125. tKeyboard[6] = {}
  126. tKeyboard[6].y = 16/nKbHei
  127. tKeyboard[6].height = 2/nKbHei
  128. tKeyboard[6].keys = {
  129. {"ctrl", 1, 5, keys.leftCtrl},
  130. {"alt", 6, 9, keys.leftAlt},
  131. {"space", 10, 40, keys.space},
  132. {"alt", 41, 44, keys.rightAlt},
  133. {"ctrl", 45, 60, keys.rightCtrl},
  134. {"insert", 63, 71, keys["end"]},
  135. {"delete", 72, 76, keys.delete},
  136. {"ent", 77, 80, keys.numPadEnter},
  137. }
  138.  
  139. local tochar = {
  140. [2] = "1",
  141. [3] = "2",
  142. [4] = "3",
  143. [5] = "4",
  144. [6] = "5",
  145. [7] = "6",
  146. [8] = "7",
  147. [9] = "8",
  148. [10] = "9",
  149. [11] = "0",
  150. [12] = "-",
  151. [13] = "=",
  152. [16] = "q",
  153. [17] = "w",
  154. [18] = "e",
  155. [19] = "r",
  156. [20] = "t",
  157. [21] = "y",
  158. [22] = "u",
  159. [23] = "i",
  160. [24] = "o",
  161. [25] = "p",
  162. [26] = "[",
  163. [27] = "]",
  164. [30] = "a",
  165. [31] = "s",
  166. [32] = "d",
  167. [33] = "f",
  168. [34] = "g",
  169. [35] = "h",
  170. [36] = "j",
  171. [37] = "k",
  172. [38] = "l",
  173. [39] = ";",
  174. [40] = "'",
  175. [41] = "`",
  176. [43] = "\\",
  177. [44] = "z",
  178. [45] = "x",
  179. [46] = "c",
  180. [47] = "v",
  181. [48] = "b",
  182. [49] = "n",
  183. [50] = "m",
  184. [51] = ",",
  185. [52] = ".",
  186. [53] = "/",
  187. [57] = " "
  188. }
  189.  
  190. local tocharshift = {
  191. [2] = "!",
  192. [3] = "@",
  193. [4] = "#",
  194. [5] = "$",
  195. [6] = "%",
  196. [7] = "^",
  197. [8] = "&",
  198. [9] = "*",
  199. [10] = "(",
  200. [11] = ")",
  201. [12] = "_",
  202. [13] = "=",
  203. [26] = "{",
  204. [27] = "}",
  205. [39] = ":",
  206. [40] = '"',
  207. [43] = '|',
  208. [51] = '<',
  209. [52] = '>',
  210. [53] = '?',
  211. }
  212.  
  213. local nWidth, nHeight = term.getSize()
  214. local nBgColor, nTextColor = colors.lightGray, colors.black
  215. local nKeyBgColor, nKeyTextColor = colors.blue, colors.black
  216.  
  217. local setTextColor, setBackColor = term.setTextColor, term.setBackgroundColor
  218. local setCursorPos, clear = term.setCursorPos, term.clear
  219.  
  220. -- Fix the width of each key.
  221. for row, rowval in ipairs(tKeyboard) do
  222. for key, keyval in ipairs(rowval.keys) do
  223. keyval[2] = keyval[2]/nKbWid
  224. keyval[3] = keyval[3]/nKbWid
  225. end
  226. end
  227.  
  228. -- Clears the screen and restores it to the default colors.
  229. local function clearTerm()
  230. setTextColor(nTextColor)
  231. setBackColor(nBgColor)
  232. setCursorPos(1, 1)
  233. clear()
  234. end
  235.  
  236. -- Used to draw the keyboard.
  237. local function drawKeyboard()
  238. clearTerm()
  239. setTextColor(nKeyTextColor)
  240. setBackColor(nKeyBgColor)
  241. local curHeight = 1
  242. for row, rowval in ipairs(tKeyboard) do
  243. curHeight = math.ceil(rowval.y*nHeight)
  244. local rowHeight = math.floor(rowval.height*nHeight)
  245. for key, keyval in ipairs(rowval.keys) do
  246. for k=curHeight, curHeight + rowHeight - 1 do
  247. setCursorPos(math.floor(keyval[2]*nWidth), k)
  248. term.write(string.rep(" ", math.floor(keyval[3]*nWidth) - math.floor(keyval[2]*nWidth)))
  249. end
  250. setCursorPos(math.floor(keyval[2]*nWidth), curHeight)
  251. term.write(keyval[1])
  252. end
  253. curHeight = curHeight + rowHeight
  254. end
  255. end
  256.  
  257. local function touchKeyboard(x, y)
  258. for row, rowval in ipairs(tKeyboard) do
  259. curHeight = math.ceil(rowval.y*nHeight)
  260. local rowHeight = math.floor(rowval.height*nHeight)
  261. if y >= curHeight and y <= curHeight + rowHeight - 1 then
  262. for key, keyval in ipairs(rowval.keys) do
  263. if x >= math.floor(keyval[2]*nWidth) and x <= math.floor(keyval[3]*nWidth) then
  264. os.queueEvent("key", keyval[4])
  265. if keyval[4] == 42 or keyval[4] == 54 then
  266. bShift = not bShift
  267. end
  268. if not bShift then
  269. if tochar[keyval[4]] then
  270. os.queueEvent("char", tochar[keyval[4]])
  271. end
  272. else
  273. if tocharshift[keyval[4]] then
  274. os.queueEvent("char", tocharshift[keyval[4]])
  275. elseif tochar[keyval[4]] then
  276. os.queueEvent("char", string.upper(tochar[keyval[4]]))
  277. end
  278. end
  279. return keyval[4]
  280. end
  281. end
  282. end
  283. curHeight = curHeight + rowHeight
  284. end
  285. end
  286.  
  287. -- This is called when the terminal resizes.
  288. local function onTermResize()
  289. nWidth, nHeight = term.getSize()
  290. drawKeyboard()
  291. end
  292.  
  293. drawKeyboard()
  294.  
  295. -- Main loop
  296. while true do
  297. local event, p1, p2, p3 = os.pullEvent()
  298. if event == "monitor_touch" then
  299. touchKeyboard(p2, p3)
  300. elseif event == "monitor_resize" or event == "term_resize" then
  301. onTermResize()
  302. end
  303. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement