Advertisement
bystander36

DPI Hold (not recommended)

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