Advertisement
bystander36

ReleaseAllKeys(co)

Oct 23rd, 2018
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.86 KB | None | 0 0
  1. function _OnActivated()
  2.   EnablePrimaryMouseButtonEvents(true)
  3.   --Task_G5 = NewTask(fn_MacroMap, "RightAndLeft")
  4.   Task_G5 = NewTask(fn_RightAndLeft)
  5.   Task_G4 = NewTask(fn_MacroMap, "UpAndDown")
  6. end
  7.  
  8. function _OnEvent(event, arg, family)
  9.   if (event == "G_PRESSED" and arg == 5) then
  10.     Task_G5.Start()
  11.   elseif (event == "G_RELEASED" and arg == 5) then
  12.     ReleaseAllKeys(Task_G5._Co)
  13.     Task_G5.Stop()
  14.   elseif (event == "G_PRESSED" and arg == 4) then
  15.     Task_G4.Start()
  16.   elseif (event == "G_RELEASED" and arg == 4) then
  17.     ReleaseAllKeys(Task_G4._Co)
  18.     Task_G4.Stop()
  19.   end
  20. end
  21.  
  22. function fn_MacroMap(macro_name)
  23.   while true do
  24.     --PlayMacro(macro_name)
  25.     fn_RightAndLeft()
  26.     TaskSleep(5)
  27.   end
  28. end
  29.  
  30. function fn_RightAndLeft()
  31.   while true do
  32.     TaskSleep(100)
  33.     PressKey("d")
  34.     TaskSleep(100)
  35.     ReleaseKey("d")
  36.     TaskSleep(100)
  37.     PressKey("a")
  38.     TaskSleep(100)
  39.     ReleaseKey("a")
  40.     TaskSleep(5)
  41.   end
  42. end
  43.  
  44. ------------------------------------------------------------------------------------------------------
  45. -- Task Class
  46. ------------------------------------------------------------------------------------------------------
  47. -- NewTask(func, ...)   returns a new object, and creates the coroutine with given func and variables
  48. --  .ChangeVars(...)    Change vars to new vars for next .Execute()
  49. --  .SetRepeat(boolean) Sets repeat status to true or false.  If repeat, when the task ends, it restarts.
  50. --  .GetRepeat()    returns if Repeat is set to true or false.
  51. --  .Create()       creates the task, but is not running yet.
  52. --  .Start()        creates if not already created and at the start, then sets the task to Run.
  53. --  .Stop()         stops and finishes the task.  It cannot be resumed.
  54. --  .IsCreated()    checks if the Task's coroutine exists (self._Co ~= nil)
  55. --  .IsRunning()    checks if the Task's is created, if it is running, and not dead (finished running)
  56. --  .IsAtStart()    checks if the Task is created but has not yet run.
  57. --  .IsAtEnd()      checks if the Task has completed, and still exists.
  58. --  .GetStatus()    returns the status of the coroutine, or nil if it doesn't exist
  59. --  .Pause()        stops running the task, but does not destroy it.
  60. --  .Resume()       resumes the task where it left off.
  61. --  .Execute()      run the Task with the given variables.  This should be run every event to keep the task running
  62. --  .Destroy()      stops and removes the Task.  It remove from the TaskHandler and nilled.
  63. --  .Remove()       calls .Destroy()
  64. -- TaskSleep(delay) This will yield within a Task function with the given delay.  Execution will pick up where it left off after the delay.
  65. -- PressKey(key, tag)   Presses key, if no tag given, it will be saved under the coroutine running, or "generic" if not in a coroutine
  66. -- ReleaseKey(key)  Releases key, and clears it from tracking table.
  67. -- ReleaseAllKeys(tag)  If tag, releases all keys pressed within a coroutine handler, or specific handler given.  If no tag is given, releases all keys.
  68. ------------------------------------------------------------------------------------------------------
  69.  
  70. -------------------------------------------------
  71. --  The following is for polling.  Do not alter.
  72. -------------------------------------------------
  73. _StartUpParameters = {
  74.     PollDevice = "mouse",
  75.     PollDelay = 10,
  76.     AutoTaskSleep = false,
  77. }
  78. function _PreEvent() end
  79. function _PostEvent()
  80.     _TaskHandler.Execute()
  81. end
  82. function OnEvent(event, arg, family)
  83.     if event == "PROFILE_ACTIVATED" then
  84.         _TaskHandler = InitTaskHandler()
  85.         Poll = InitPolling(_StartUpParameters.PollDelay, _StartUpParameters.PollDevice, _PreEvent, _PostEvent)
  86.     end
  87.     Poll.Execute(event, arg, family)
  88. end
  89.  
  90. ----------------------------
  91. -- Polling Class
  92. ----------------------------
  93. function InitPolling(PollDelay, PollDevice, PreOnEventFunc, PostOnEventFunc)
  94.     local self = {
  95.         PollDelay = PollDelay,
  96.         PollDevice = PollDevice,
  97.         PreOnEventFunc = PreOnEventFunc,
  98.         PostOnEventFunc = PostOnEventFunc,
  99.         Sleep = Sleep_hook,
  100.     }
  101.     local function CreateEvent() SetMKeyState(1, self.PollDevice) end
  102.     local function OnEvent(event, arg, family)
  103.         if self.PreOnEventFunc then self.PreOnEventFunc() end
  104.         _OnEvent(event, arg, family)
  105.         if self.PostOnEventFunc then self.PostOnEventFunc() end
  106.     end
  107.     function self.Execute(event, arg, family)
  108.         if event == "PROFILE_ACTIVATED" then
  109.             if _OnActivated then _OnActivated(event, arg, family) end
  110.             OnEvent(event, arg, family)
  111.             CreateEvent()                                   -- initiates the first polling event
  112.         elseif event == "M_RELEASED" and family == self.PollDevice then
  113.             OnEvent("POLLING", 0, self.PollDevice)
  114.             CreateEvent()
  115.             self.Sleep(self.PollDelay)
  116.         elseif event == "M_PRESSED" and family == self.PollDevice then
  117.             OnEvent("POLLING", 0, self.PollDevice)
  118.             self.Sleep(self.PollDelay)
  119.         elseif event == "PROFILE_DEACTIVATED" then
  120.             if _OnDeactivated then  _OnDeactivated(event, arg, family) end
  121.         else
  122.             OnEvent(event, arg, family)
  123.         end
  124.     end
  125.     function self.SetPreOnEventFunc(func) self.PreOnEventFunc = func end
  126.     function self.SetPostOnEventFunc(func) self.PosOnEventFunc = func end
  127.     return self
  128. end
  129.  
  130. ------------------------
  131. -- Task Class
  132. ------------------------
  133. function TaskSleep(delay) return coroutine.yield(delay) end
  134. function NewTask(func, ...)
  135.     local self = {
  136.         _Func = func,
  137.         _Running = false,
  138.         _Co = nil,
  139.         _ResumeRunningTime = -1,
  140.         _AtStart = false,
  141.         _Repeat = false,
  142.         _Vars = nil,
  143.         _TH = _TaskHandler or nil,
  144.     }
  145.     function self.ChangeVars(...)   self._Vars = { ... } end
  146.     function self.SetRepeat(r)  self._Repeat = r end
  147.     function self.GetRepeat()   return self._Repeat end
  148.     function self.Create()
  149.         self._ResumeRunningTime = -1
  150.         self._Running = false
  151.         self._Co = coroutine.create(self._Func)
  152.         self._AtStart = true
  153.     end
  154.     function self.Start()
  155.         if not self.IsAtStart() or not self.IsCreated() then
  156.             self.Create()
  157.         end
  158.         self._Running = true
  159.     end
  160.     function self.Stop() self._Running = false; self._Co = nil end
  161.     function self.GetStatus()
  162.         if self._Co then return coroutine.status(self._Co)
  163.         else return nil end
  164.     end
  165.     function self.IsAtStart() return self._AtStart end
  166.     function self.IsAtEnd() return self.IsDead() end
  167.     function self.IsCreated()
  168.         if self._Co then return true
  169.         else return false   end
  170.     end
  171.     function self.IsDead()
  172.         if self._Co and self.GetStatus() == "dead" then return true
  173.         else return false   end
  174.     end
  175.     function self.IsRunning()
  176.         if self.IsCreated() and self._Running and not self.IsDead() then return true
  177.         else return false end
  178.     end
  179.     function self.IsReady()
  180.         if self._Running and self.IsCreated() and not self.IsDead()
  181.             and self._ResumeRunningTime <= GetRunningTime() then
  182.             return true
  183.         else return false end
  184.     end
  185.     function self.Pause() self._Running = false end
  186.     function self.Resume() self._Running = true end
  187.     function self.Execute()
  188.         if self.GetRepeat() and self.IsDead() and self._Running then self.Start() end
  189.         if self.IsReady() then
  190.             local status, delay = coroutine.resume(self._Co, unpack(self._Vars))
  191.             self._AtStart = false
  192.             if delay then self._ResumeRunningTime = delay + GetRunningTime()
  193.             else self._ResumeRunningTime = -1 end
  194.             return status
  195.         end
  196.     end
  197.     function self.Destroy()
  198.         if self._TH then self._TH.RemoveTask(self) end
  199.         self = nil
  200.         return nil
  201.     end
  202.     function self.Remove() self.Destroy() end
  203.     self.ChangeVars(...)
  204.     self.Create()
  205.     if self._TH then self._TH.AddTask(self) end
  206.     return self
  207. end
  208.  
  209. --------------------------
  210. --  TaskHandler
  211. --------------------------
  212. function InitTaskHandler()
  213.     local self = {  _TaskList = {}, }
  214.     function self.AddTask(Task) self._TaskList[Task] = true end
  215.     function self.RemoveTask(Task) self._TaskList[Task] = nil end
  216.     function self.Execute()
  217.         for k,v in pairs(self._TaskList) do k.Execute() end
  218.     end
  219.     return self
  220. end
  221. coroutine.running_hook = coroutine.running
  222. function coroutine.running()
  223.     local v = coroutine.running_hook()
  224.     return v
  225. end
  226. Sleep_hook = Sleep
  227. function Sleep(d)
  228.     if _StartUpParameters.AutoTaskSleep and coroutine.running() then return TaskSleep(d)
  229.     else return Sleep_hook(d) end
  230. end
  231. _KeysPressed = {} -- Keys pressed from within a coroutine.  CO-KeysPressed[ct][key]
  232.  
  233. PressKey_hook = PressKey
  234. ReleaseKey_hook = ReleaseKey
  235.  
  236. function PressKey(k, tag)
  237.     local t = tag or coroutine.running() or "generic"
  238.  
  239.     if not _KeysPressed[t] then
  240.         _KeysPressed[t] = {}
  241.     end
  242.     _KeysPressed[t][k] = true       -- set the key to true, meaning it is pressed
  243.  
  244.     PressKey_hook(k)
  245. end
  246.  
  247. function ReleaseKey(k)  -- releases the key, and nil's the key in all locations of _KeysPressed
  248.     ReleaseKey_hook(k)
  249.  
  250.     for key, value in pairs(_KeysPressed) do
  251.         _KeysPressed[key][k] = nil
  252.     end
  253. end
  254.  
  255. function ReleaseAllKeys(tag)
  256.  
  257.     if _KeysPressed[tag] then
  258.         for key, value in pairs(_KeysPressed[tag]) do
  259.             ReleaseKey(key)
  260.         end
  261.     elseif not tag then
  262.         for key, value in pairs(_KeysPressed) do
  263.             for k, v in pairs(_KeysPressed[key]) do
  264.                 ReleaseKey(k)
  265.             end
  266.         end
  267.     end
  268. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement