austinv11

ctrlkeys

Dec 23rd, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.71 KB | None | 0 0
  1. --Created by GopherAtl
  2.  
  3. local localVersion = "0.1"
  4. local author = "GopherAtl"
  5.  
  6. --DEPENDS use:goroutine
  7.  
  8. --ctrlkey events
  9.  
  10. --if goroutine is active, assumes it's running as a managed coroutine
  11. --and passes on char and key events, which should be assigned to it if
  12. --input events are being directed to a single routine by a task manager.
  13. --if goroutine is not present, or not active, will have no effects.
  14.  
  15. if not goroutine then
  16.   --attempt to load it
  17.   --gotta override printError to catch the error
  18.   local oldPrintError=_G.printError
  19.   rawset(_G,"printError",error)
  20.   local ok,err=pcall(os.loadAPI,"goroutine")
  21.   if not ok then
  22.     --doesn't exist, just make a dummy I guess?
  23.     rawset(_G,"goroutine",{ passEvent=function() end })
  24.   end
  25.   --restore normal printError function
  26.   rawset(_G,"printError",oldPrintError)
  27. end
  28.  
  29. --the target coroutine to pass events after processing. Defaults "", which is all.
  30. local eventPassTarget=""
  31.  
  32. function setEventPassTarget(target)
  33.   eventPassTarget=target
  34. end
  35.  
  36. 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]="/",}
  37. 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]="?",}
  38. 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,}
  39.  
  40.  
  41. function coGenCtrlKeyEvents()
  42.   local lastInCtrl, lastInCtrlShift,lastInCtrlAlt=false,false,false
  43.   local prevKey=0
  44.   local pasting=false
  45.   local pasteTimer=0
  46.  
  47.   while true do  
  48.     local e,p1,p2,p3=os.pullEventRaw()
  49.     if e=="key" then    
  50.       goroutine.passEvent(eventPassTarget)
  51.       if p1==keys.leftCtrl or p1==keys.rightCtrl then
  52.         lastInCtrl=true
  53.         lastInCtrlShift=false
  54.         lastInCtrlAlt=false
  55.       elseif p1==keys.leftShift or p1==keys.rightShift and lastInCtrl then
  56.         lastInCtrlShift=true
  57.       elseif p1==keys.leftAlt or p1==keys.rightAlt and lastInCtrl then
  58.         lastInCtrlAlt=true
  59.       elseif lastInCtrl and keyToCharShift[p1] then
  60.         os.startTimer(0)
  61.         local _,char,p2,p3,p4,p5=os.pullEventRaw()
  62.         if _=="char" or _=="key" then
  63.           goroutine.passEvent(eventPassTarget)
  64.         end
  65.         if _~="char" or charToKey[char]~=p1 then
  66.           os.queueEvent("ctrl_key",p1,lastInCtrlShift and keyToCharShift[p1] or keyToChar[p1],lastInCtrlShift,lastInCtrlAlt)        
  67.         end
  68.         lastInCtrlShift=false
  69.         lastInCtrl=false
  70.       end
  71.       prevKey=p1
  72.       pasting=false
  73.     elseif e=="char" then
  74.       goroutine.passEvent(eventPassTarget)
  75.       if not pasting and charToKey[p1]~=prevKey then
  76.         --paste begin, send a ctrl-v event
  77.         os.queueEvent("ctrl_key",keys.v,lastInCtrlShift and "V" or "v",lastInCtrlShift,lastInCtrlAlt)
  78.         pasting=true
  79.         pasteTimer=os.startTimer(0)
  80.       end
  81.       lastInCtrlShift=false
  82.       lastInCtrlAlt=false
  83.       lastInCtrl=false    
  84.     elseif e=="timer" and p1==pasteTimer then
  85.       pasting=false
  86.     end
  87.   end
  88. end
  89.  
  90.  
  91. function waitForAny(...)
  92.   local args={...}
  93.   table.insert(args,1,coGenCtrlKeyEvents)
  94.   parallel.waitForAny(unpack(args))
  95. end
  96.  
  97. function waitForAll(...)
  98.   local args={...}
  99.   waitForAny(function() parallel.waitForAll(unpack(args)) end)
  100. end
  101.  
  102. function run(program,...)
  103.   args={...}
  104.   waitForAny(function() os.run({},program,unpack(args)) end)
  105. end
Advertisement
Add Comment
Please, Sign In to add comment