Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local redirectPath = '/rom/programs/redirect' -- Change to redirect API path if installed elsewhere!
- local redirectAPI, loadError = loadfile(redirectPath)
- if not redirectAPI then
- print('Unable to load terminal redirection API')
- print(loadError == 'File not found' and
- 'No file exists at '..redirectPath..'. Edit the first line in this program to the location of the redirect API.' or
- 'Syntax error in redirect API.'
- )
- return
- end
- local newTerm=setmetatable({},{__index=_G})
- setfenv(redirectAPI, newTerm)
- if not pcall(redirectAPI) then
- print('Error occured in redirect API')
- return
- end
- newTerm = newTerm.newTerm;
- local side, programPath = ... -- Temp
- local debugOn = false;
- local args = {select(3,...)}
- if select('#',...) < 2 then
- print('Usage: ',fs.getName(shell.getRunningProgram()),' <side/name of monitor> <program to run> [arguments for program]')
- return
- end
- if peripheral.getType(side) ~= 'monitor' then
- print('Error: There is no monitor on side ',side)
- return
- end
- if not shell.resolveProgram(programPath) then
- print('Error: There is no program at ',programPath)
- return
- end
- local monitor = peripheral.wrap(side);
- if not monitor.isColor() then
- print('Monitor must be advanced')
- return
- end
- if (monitor.getSize()) < 50 or select(2,monitor.getSize()) < 33 then
- print('Monitor too small. Must be at least 5x5 blocks (width x height)')
- return
- end
- local function debug(...) if not debugOn then return end print(...) end
- --local side;
- local mouseButton = 1;
- --[[local shiftPressed = false;
- local ctrlPressed = false;
- local altPressed = false;]]
- local keysPressed = {[29]=false, [42]=false, [56]=false}; -- 42=shift, 56=alt, 29=ctrl
- local lastPressed = {[29]=0, [42]=0, [56]=0}
- local capsLock = false;
- local programTerm = newTerm(1, 1, 50, 18, monitor);
- local filter = nil;
- local function colorSetup()
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- monitor.setCursorBlink(false)
- end
- colorSetup()
- monitor.clear()
- -- Program mangement stuff --
- --[[local program = coroutine.create(setfenv(function() shell.run(programPath) end,
- setmetatable({
- peripheral = setmetatable({
- }, {__index = peripheral})
- },{__index = _G}))
- )]]
- local program = coroutine.create(function() shell.run(programPath, unpack(args)) end);
- local function resume(...)
- assert(coroutine.status(program) ~= 'dead')
- term.redirect(programTerm)
- local ok;
- ok, filter = coroutine.resume(program,...)
- term.restore()
- assert(ok)
- end
- -- Virtual keyboard and mouse handling --
- local keyboard = {
- 'F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 ',
- '',
- 'A B C D E F G H I J K L M N O P Q R S T',
- 'U V W X Y Z 1 2 3 4 5 6 7 8 9 0 ! @ # $',
- '% ^ & * ( ) - _ = + | { } [ ] : ; " \' <',
- '> , . ? / \\ ` ~ Space Enter Backspace',
- '',
- 'Ctrl Shift Alt Insert Home PageUp',
- 'Caps Tab Meta Delete End PageDown',
- }
- local arrowKeys = {
- ' /\\',
- ' ||',
- '/-/\\-\\',
- '\\-\\/-/',
- ' ||',
- ' \\/',
- }
- local keyCodes = {
- Ctrl = 29, Shift = 42, Alt = 56, Caps = 58,
- ['1']=2, ['2']=3, ['3']=4, ['4']=5, ['5']=6,
- ['6']=7, ['7']=8, ['8']=9, ['9']=10,['0']=11,
- ['!']=2, ['@']=3, ['#']=4, ['$']=5, ['%']=6,
- ['^']=7, ['&']=8, ['*']=9, ['(']=10,[')']=11,
- ['`']=41, ['~']=144,['-']=12,['_']=12,['=']=13,
- ['+']=13, ['[']=26, ['{']=26,['}']=27,[']']=27,
- ['\\']=43,['|']=43, [';']=39,[':']=39,["'"]=40,
- ['"']=40, [',']=51, ['<']=51,['.']=52,['>']=52,
- ['/']=53, ['/']=53,
- PageDown=209,PageUp=201,Meta=219,
- }
- local function drawKeyboard()
- for i=1,9 do
- monitor.setCursorPos(3,23+i)
- monitor.write(keyboard[i])
- end
- monitor.setCursorPos(44,24)
- monitor.write('Arrows:')
- for i=1,6 do
- monitor.setCursorPos(44,25+i)
- monitor.write(arrowKeys[i])
- end
- for i=1,3 do
- monitor.setCursorPos(44 + (i==1 and 0 or 1),19+i)
- monitor.write(({'Scroll:','Up','Down'})[i])
- end
- end
- drawKeyboard()
- local function updateKeyDisplay()
- colorSetup()
- monitor.setCursorPos(3, 20)
- monitor.write('Shift: ' .. (keysPressed[42] and 'Down' or 'Up '))
- monitor.setCursorPos(3, 21)
- monitor.write('Alt: ' .. (keysPressed[56] and 'Down' or 'Up '))
- monitor.setCursorPos(3, 22)
- monitor.write('Ctrl: ' .. (keysPressed[29] and 'Down' or 'Up '))
- end
- updateKeyDisplay();
- local function getArrowKey(x,y)
- --[[if y >= 26 and y <= 29 and x >= 45 and x <= ({46,48,49,48})[y-25] then
- return ({200,203,205,208})[y-25]
- end]]
- y = y - 25;
- x = x - 43;
- if x<1 or y<1 or x>6 or y>6 then
- return nil;
- end
- x = (x - (x-1)%2 + 1)/2;
- y = (y - (y-1)%2 + 1)/2;
- if x==1 then
- return y==2 and 203 or nil;
- elseif x==2 then
- return y==1 and 200 or y==3 and 208 or nil;
- elseif x==3 then
- return y==2 and 205 or nil;
- end
- return nil;
- end
- local function getKey(x,y)
- local arrowKey = getArrowKey(x,y);
- if arrowKey then return arrowKey end
- y = y - 23
- x = x - 2
- if x<1 or y<1 or x>39 or y > 9 or y==2 or y==7 then
- return nil
- end
- debug('Touched keyboard')
- local row = keyboard[y]
- local startPos = x;
- while startPos > 1 and row:sub(startPos-1,startPos-1) ~= ' ' do
- startPos=startPos-1;
- end
- while x<39 and row:sub(x+1,x+1) ~= ' ' do
- x=x+1
- end
- local key=row:sub(startPos,x);
- local char = nil;
- if x==startPos then
- char = (keysPressed[42] or capsLock) and key or key:lower();
- end
- key = keys[key:lower()] or keyCodes[key];
- debug('Key: ',key or 'nil',', Char: ',char or 'nil')
- local setPressed = false;
- if key==42 or key==29 or key==56 then
- if os.clock()-lastPressed[key] <= 0.3 then
- keysPressed[key] = true;
- lastPressed[key]=os.clock()+0.15;
- setPressed=true;
- end
- lastPressed[key] = os.clock();
- end
- for i=1,3 do
- local k = ({29,42,56})[i];
- if not (key==42 or key==29 or key==56) or (k == key and not setPressed) then
- keysPressed[k] = false
- end
- end
- updateKeyDisplay();
- if key==57 then
- char=' ';
- end
- if key==58 then
- capsLock=not capsLock;
- end
- return key, char;
- end
- local function getMouseButton(x,y)
- if y>= 20 and y<=22 and x > 32 and x <= ({36,38,37})[y-19] then
- y = y-19;
- if y ~= 1 then y=5-y end
- return y;
- end
- return nil;
- end
- local function setMouseButton(button)
- colorSetup()
- mouseButton=button;
- monitor.setCursorPos(18,21)
- monitor.write('Mouse button:')
- for i=1,3 do
- monitor.setCursorPos(32,19+i)
- local char1 = (i==1 and 1 or 5-i)==button and '>' or ' '
- local char2 = (i==1 and 1 or 5-i)==button and '<' or ' '
- monitor.write(char1..({'Left','Middle','Right'})[i]..char2)
- end
- end
- setMouseButton(1);
- local function getScroll(x,y)
- if y>=21 and y<=22 and x>=45 and x<=({46,48})[y-20] then
- return (y-20)*2 - 3;
- end
- return nil
- end
- --[[for i=1,4 do
- monitor.setCursorPos(45,25+i)
- monitor.write(({'Up','Left','Right','Down'})[i])
- end]]
- -- Main loop --
- resume();
- local ok,err = pcall(function()
- for event,p1,p2,p3,p4,p5 in os.pullEvent do
- debug(event,': ',table.concat({p1,p2,p3,p4,p5},', '))
- if event == 'monitor_touch' and p1 == side then
- debug('Touched')
- if p2 <= 51 and p3 <= 18 then
- resume('mouse_click', mouseButton, p2, p3)
- else
- local key, char = getKey(p2, p3);
- if key and not filter or filter=='key' and not(char and ctrlPressed) then
- resume('key', key)
- end
- if char and not filter or filter=='char' then
- resume('char', char)
- end
- if not key then
- --[[local newButton = newMouseButton(p2,p3)
- if newButton then
- mouseButton = newButton;
- elseif
- end]]
- local button = getMouseButton(p2,p3);
- if button then
- setMouseButton(button)
- end
- local scroll = getScroll(p2,p3)
- if scroll then
- resume('mouse_scroll',scroll,1,1)
- end
- end
- end
- else
- if not filter or filter==event then
- resume(event,p1,p2,p3,p4,p5)
- end
- end
- if coroutine.status(program)=='dead' then
- print('Program has exited')
- break
- end
- end
- end)
- debug(err)
- colorSetup()
- monitor.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement