Advertisement
MarkFergus

/lib/getInput

Sep 1st, 2020
1,551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.30 KB | None | 0 0
  1. function read(m,length,input)
  2.    
  3.     local pos = {}
  4.     local sPos = 0
  5.     local periphs = {}
  6.     local erase_queue = false
  7.     if input == nil then input = "" end
  8.    
  9.     if type(m[1]) ~= "table" then local _temp = m  periphs[1] = _temp else periphs = m end
  10.    
  11.     for i,v in pairs(periphs) do
  12.         pos[i] = {v.getCursorPos()}
  13.         v.setCursorBlink(true)
  14.     end
  15.  
  16.     repeat
  17.    
  18.     for i,v in pairs(periphs) do
  19.        
  20.         v.setCursorPos(pos[i][1], pos[i][2])
  21.         v.write(input)
  22.        
  23.         if erase_queue then
  24.             x1,y1 = v.getCursorPos()
  25.             v.setCursorPos(xl-1,pos[i][2])
  26.             v.write(" ")
  27.         end
  28.        
  29.         v.setCursorPos(pos[i][1]-sPos,pos[i][2])
  30.        
  31.         erase_queue = false
  32.     end
  33.    
  34.     local ev, p1 = os.pullEvent()
  35.    
  36.     if ev == 'char' then
  37.         if #input < length then
  38.             input = input .. p1
  39.         end
  40.     elseif ev == 'key' then
  41.         if p1 == keys.backspace then
  42.             if #input ~= 0 then
  43.                 erase_queue = true
  44.             end
  45.             input = input:sub(1, #input - 1)
  46.         elseif p1 == keys.left then
  47.             if #input ~= 0 then
  48.                 sPos = sPos + 1
  49.             end
  50.         elseif p1 == keys.right then
  51.             if #input ~= length-1 then
  52.                 sPos = sPos - 1
  53.             end
  54.         end
  55.     end
  56.    
  57.     until ev == 'key' and p1 == keys.enter or ev == 'mouse_click' or ev == 'monitor_touch'
  58.    
  59.     for i,v in pairs(periphs) do
  60.         v.setCursorBlink(false)
  61.         v.setCursorPos(pos[i][1],pos[i][2])
  62.     end
  63.     return input, ev, p1
  64. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement