Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Created by GopherAtl
- local localVersion = "0.1"
- local author = "GopherAtl"
- --DEPENDS use:goroutine
- --ctrlkey events
- --if goroutine is active, assumes it's running as a managed coroutine
- --and passes on char and key events, which should be assigned to it if
- --input events are being directed to a single routine by a task manager.
- --if goroutine is not present, or not active, will have no effects.
- if not goroutine then
- --attempt to load it
- --gotta override printError to catch the error
- local oldPrintError=_G.printError
- rawset(_G,"printError",error)
- local ok,err=pcall(os.loadAPI,"goroutine")
- if not ok then
- --doesn't exist, just make a dummy I guess?
- rawset(_G,"goroutine",{ passEvent=function() end })
- end
- --restore normal printError function
- rawset(_G,"printError",oldPrintError)
- end
- --the target coroutine to pass events after processing. Defaults "", which is all.
- local eventPassTarget=""
- function setEventPassTarget(target)
- eventPassTarget=target
- end
- local keyToChar = {[2]="1",[3]="2",[4]="3",[5]="4",[6]="5",[7]="6",[8]="7",[9]="8",[10]="9",[11]="0",[12]="-",[13]="=",[16]="q",[17]="w",[18]="e",[19]="r",[20]="t",[21]="y",[22]="u",[23]="i",[24]="o",[25]="p",[26]="[",[27]="]",[30]="a",[31]="s",[32]="d",[33]="f",[34]="g",[35]="h",[36]="j",[37]="k",[38]="l",[39]=";",[40]="'", [41]='`',[43]="\\",[44]="z",[45]="x",[46]="c",[47]="v",[48]="b",[49]="n",[50]="m",[51]=",",[52]=".",[53]="/",}
- local keyToCharShift = {[2]="!",[3]="@",[4]="#",[5]="$",[6]="%",[7]="^",[8]="&",[9]="*",[10]="(",[11]=")",[12]="_",[13]="+",[16]="Q",[17]="W",[18]="E",[19]="R",[20]="T",[21]="Y",[22]="U",[23]="I",[24]="O",[25]="P",[26]="{",[27]="}",[30]="A",[31]="S",[32]="D",[33]="F",[34]="G",[35]="H",[36]="J",[37]="K",[38]="L",[39]=":",[40]="\"",[41]="~",[43]="|", [44]="Z",[45]="X",[46]="C",[47]="V",[48]="B",[49]="N",[50]="M",[51]="<",[52]=">",[53]="?",}
- local charToKey = {["P"]=25,["S"]=31,["R"]=19,["U"]=22,["T"]=20,["W"]=17,["V"]=47,["Y"]=21,["X"]=45,["["]=26,["Z"]=44,["]"]=27,["\\"]=43,["_"]=12,["^"]=7,["a"]=30,["c"]=46,["b"]=48,["e"]=18,["d"]=32,["g"]=34,["f"]=33,["i"]=23,["h"]=35,["k"]=37,["j"]=36,["m"]=50,["l"]=38,["o"]=24,["n"]=49,["q"]=16,["p"]=25,["s"]=31,["r"]=19,["u"]=22,["t"]=20,["w"]=17,["v"]=47,["y"]=21,["x"]=45,["{"]=26,["z"]=44,["}"]=27,["|"]=43,["~"]=41,["!"]=2,["#"]=4,["\""]=40,["%"]=6,["$"]=5,["'"]=40,["&"]=8,[")"]=11,["("]=10,["+"]=13,["*"]=9,["-"]=12,[","]=51,["/"]=53,["."]=52,["1"]=2,["0"]=11,["3"]=4,["2"]=3,["5"]=6,["4"]=5,["7"]=8,["6"]=7,["9"]=10,["8"]=9,[";"]=39,[":"]=39,["="]=13,["<"]=51,["?"]=53,[">"]=52,["A"]=30,["@"]=3,["C"]=46,["B"]=48,["E"]=18,["D"]=32,["G"]=34,["F"]=33,["I"]=23,["H"]=35,["K"]=37,["J"]=36,["M"]=50,["L"]=38,["O"]=24,["N"]=49,["Q"]=16,}
- function coGenCtrlKeyEvents()
- local lastInCtrl, lastInCtrlShift,lastInCtrlAlt=false,false,false
- local prevKey=0
- local pasting=false
- local pasteTimer=0
- while true do
- local e,p1,p2,p3=os.pullEventRaw()
- if e=="key" then
- goroutine.passEvent(eventPassTarget)
- if p1==keys.leftCtrl or p1==keys.rightCtrl then
- lastInCtrl=true
- lastInCtrlShift=false
- lastInCtrlAlt=false
- elseif p1==keys.leftShift or p1==keys.rightShift and lastInCtrl then
- lastInCtrlShift=true
- elseif p1==keys.leftAlt or p1==keys.rightAlt and lastInCtrl then
- lastInCtrlAlt=true
- elseif lastInCtrl and keyToCharShift[p1] then
- os.startTimer(0)
- local _,char,p2,p3,p4,p5=os.pullEventRaw()
- if _=="char" or _=="key" then
- goroutine.passEvent(eventPassTarget)
- end
- if _~="char" or charToKey[char]~=p1 then
- os.queueEvent("ctrl_key",p1,lastInCtrlShift and keyToCharShift[p1] or keyToChar[p1],lastInCtrlShift,lastInCtrlAlt)
- end
- lastInCtrlShift=false
- lastInCtrl=false
- end
- prevKey=p1
- pasting=false
- elseif e=="char" then
- goroutine.passEvent(eventPassTarget)
- if not pasting and charToKey[p1]~=prevKey then
- --paste begin, send a ctrl-v event
- os.queueEvent("ctrl_key",keys.v,lastInCtrlShift and "V" or "v",lastInCtrlShift,lastInCtrlAlt)
- pasting=true
- pasteTimer=os.startTimer(0)
- end
- lastInCtrlShift=false
- lastInCtrlAlt=false
- lastInCtrl=false
- elseif e=="timer" and p1==pasteTimer then
- pasting=false
- end
- end
- end
- function waitForAny(...)
- local args={...}
- table.insert(args,1,coGenCtrlKeyEvents)
- parallel.waitForAny(unpack(args))
- end
- function waitForAll(...)
- local args={...}
- waitForAny(function() parallel.waitForAll(unpack(args)) end)
- end
- function run(program,...)
- args={...}
- waitForAny(function() os.run({},program,unpack(args)) end)
- end
Advertisement
Add Comment
Please, Sign In to add comment