Advertisement
bystander36

Repeat V (Polling)

Jan 16th, 2018
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.11 KB | None | 0 0
  1. function _OnEvent(event, arg,family)
  2.     if event == "PROFILE_ACTIVATED" then
  3.         task_PressKeyLoop = NewTask(PressKeyLoop,"v")
  4.     elseif event == "MOUSE_BUTTON_PRESSED" and arg == 4 then
  5.         toggle = not toggle
  6.         if toggle then
  7.             task_PressKeyLoop.Start()
  8.         else
  9.             task_PressKeyLoop.Stop()
  10.         end
  11.     end
  12. end
  13.  
  14. function PressKeyLoop(key)
  15.     while true do
  16.         PressAndReleaseKey("v")
  17.         Sleep(math.random(2000,5000))
  18.     end
  19. end
  20.  
  21. -------------------------------------------------
  22. --  The following is for polling.  Do not alter.
  23. -------------------------------------------------
  24.  
  25. ------------------------------------------------------------------------------------------------------
  26. -- Task Class
  27. ------------------------------------------------------------------------------------------------------
  28. -- NewTask(func, ...)   returns a new object, and creates the coroutine with given func and variables
  29. --  .ChangeVars(...)    Change vars to new vars for next .Execute()
  30. --  .SetRepeat(boolean) Sets repeat status to true or false.  If repeat, when the task ends, it restarts.
  31. --  .GetRepeat()    returns if Repeat is set to true or false.
  32. --  .Create()       creates the task, but is not running yet.
  33. --  .Start()        creates if not already created and at the start, then sets the task to Run.
  34. --  .Stop()         stops and finishes the task.  It cannot be resumed.
  35. --  .IsCreated()    checks if the Task's coroutine exists (self._Co ~= nil)
  36. --  .IsRunning()    checks if the Task's is created, if it is running, and not dead (finished running)
  37. --  .IsAtStart()    checks if the Task is created but has not yet run.
  38. --  .IsAtEnd()      checks if the Task has completed, and still exists.
  39. --  .GetStatus()    returns the status of the coroutine, or nil if it doesn't exist
  40. --  .Pause()        stops running the task, but does not destroy it.
  41. --  .Resume()       resumes the task where it left off.
  42. --  .Execute()      run the Task with the given variables.  This should be run every event to keep the task running
  43. --  .Destroy()      stops and removes the Task.  It remove from the TaskHandler and nilled.
  44. --  .Remove()       calls .Destroy()
  45. -- TaskSleep(delay) This will yield within a Task function with the given delay.  Execution will pick up where it left off after the delay.
  46. ------------------------------------------------------------------------------------------------------
  47.  
  48. _StartUpParameters = {
  49.     PollDevice = "mouse",
  50.     PollDelay = 10,
  51.     AutoTaskSleep = true,
  52. }
  53. function _PreEvent() end
  54. function _PostEvent()
  55.     _TaskHandler.Execute()
  56. end
  57. function OnEvent(event, arg, family)
  58.     if event == "PROFILE_ACTIVATED" then
  59.         _TaskHandler = InitTaskHandler()
  60.         Poll = InitPolling(_StartUpParameters.PollDelay, _StartUpParameters.PollDevice, _PreEvent, _PostEvent)
  61.     end
  62.     Poll.Execute(event, arg, family)
  63. end
  64.  
  65. ----------------------------
  66. -- Polling Class
  67. ----------------------------
  68. function InitPolling(PollDelay, PollDevice, PreOnEventFunc, PostOnEventFunc)
  69.     local self = {
  70.         PollDelay = PollDelay,
  71.         PollDevice = PollDevice,
  72.         PreOnEventFunc = PreOnEventFunc,
  73.         PostOnEventFunc = PostOnEventFunc,
  74.         Sleep = Sleep_hook,
  75.     }
  76.     local function CreateEvent() SetMKeyState(1, self.PollDevice) end
  77.     local function OnEvent(event, arg, family)
  78.         if self.PreOnEventFunc then self.PreOnEventFunc() end
  79.         _OnEvent(event, arg, family)
  80.         if self.PostOnEventFunc then self.PostOnEventFunc() end
  81.     end
  82.     function self.Execute(event, arg, family)
  83.         if event == "PROFILE_ACTIVATED" then
  84.             if _OnActivated then _OnActivated(event, arg, family) end
  85.             OnEvent(event, arg, family)
  86.             CreateEvent()                                   -- initiates the first polling event
  87.         elseif event == "M_RELEASED" and family == self.PollDevice then
  88.             OnEvent("POLLING", 0, self.PollDevice)
  89.             CreateEvent()
  90.             self.Sleep(self.PollDelay)
  91.         elseif event == "M_PRESSED" and family == self.PollDevice then
  92.             OnEvent("POLLING", 0, self.PollDevice)
  93.             self.Sleep(self.PollDelay)
  94.         elseif event == "PROFILE_DEACTIVATED" then
  95.             if _OnDeactivated then  _OnDeactivated(event, arg, family) end
  96.         else
  97.             OnEvent(event, arg, family)
  98.         end
  99.     end
  100.     function self.SetPreOnEventFunc(func) self.PreOnEventFunc = func end
  101.     function self.SetPostOnEventFunc(func) self.PosOnEventFunc = func end
  102.     return self
  103. end
  104.  
  105. ------------------------
  106. -- Task Class
  107. ------------------------
  108. function TaskSleep(delay) return coroutine.yield(delay) end
  109. function NewTask(func, ...)
  110.     local self = {
  111.         _Func = func,
  112.         _Running = false,
  113.         _Co = nil,
  114.         _ResumeRunningTime = -1,
  115.         _AtStart = false,
  116.         _Repeat = false,
  117.         _Vars = nil,
  118.         _TH = _TaskHandler or nil,
  119.     }
  120.     function self.ChangeVars(...)   self._Vars = { ... } end
  121.     function self.SetRepeat(r)  self._Repeat = r end
  122.     function self.GetRepeat()   return self._Repeat end
  123.     function self.Create()
  124.         self._ResumeRunningTime = -1
  125.         self._Running = false
  126.         self._Co = coroutine.create(self._Func)
  127.         self._AtStart = true
  128.     end
  129.     function self.Start()
  130.         if not self.IsAtStart() or not self.IsCreated() then
  131.             self.Create()
  132.         end
  133.         self._Running = true
  134.     end
  135.     function self.Stop() self._Running = false; self._Co = nil end
  136.     function self.GetStatus()
  137.         if self._Co then return coroutine.status(self._Co)
  138.         else return nil end
  139.     end
  140.     function self.IsAtStart() return self._AtStart end
  141.     function self.IsAtEnd() return self.IsDead() end
  142.     function self.IsCreated()
  143.         if self._Co then return true
  144.         else return false   end
  145.     end
  146.     function self.IsDead()
  147.         if self._Co and self.GetStatus() == "dead" then return true
  148.         else return false   end
  149.     end
  150.     function self.IsRunning()
  151.         if self.IsCreated() and self._Running and not self.IsDead() then return true
  152.         else return false end
  153.     end
  154.     function self.IsReady()
  155.         if self._Running and self.IsCreated() and not self.IsDead()
  156.             and self._ResumeRunningTime <= GetRunningTime() then
  157.             return true
  158.         else return false end
  159.     end
  160.     function self.Pause() self._Running = false end
  161.     function self.Resume() self._Running = true end
  162.     function self.Execute()
  163.         if self.GetRepeat() and self.IsDead() and self._Running then self.Start() end
  164.         if self.IsReady() then
  165.             local status, delay = coroutine.resume(self._Co, unpack(self._Vars))
  166.             self._AtStart = false
  167.             if delay then self._ResumeRunningTime = delay + GetRunningTime()
  168.             else self._ResumeRunningTime = -1 end
  169.             return status
  170.         end
  171.     end
  172.     function self.Destroy()
  173.         if self._TH then self._TH.RemoveTask(self) end
  174.         self = nil
  175.         return nil
  176.     end
  177.     function self.Remove() self.Destroy() end
  178.     self.ChangeVars(...)
  179.     self.Create()
  180.     if self._TH then self._TH.AddTask(self) end
  181.     return self
  182. end
  183.  
  184. --------------------------
  185. --  TaskHandler
  186. --------------------------
  187. function InitTaskHandler()
  188.     local self = {  _TaskList = {}, }
  189.     function self.AddTask(Task) self._TaskList[Task] = true end
  190.     function self.RemoveTask(Task) self._TaskList[Task] = nil end
  191.     function self.Execute()
  192.         for k,v in pairs(self._TaskList) do k.Execute() end
  193.     end
  194.     return self
  195. end
  196. coroutine.running_hook = coroutine.running
  197. function coroutine.running()
  198.     local v = coroutine.running_hook()
  199.     return v
  200. end
  201. Sleep_hook = Sleep
  202. function Sleep(d)
  203.     if _StartUpParameters.AutoTaskSleep and coroutine.running() then return TaskSleep(d)
  204.     else return Sleep_hook(d) end
  205. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement