Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. -- Turn off AutoRepeat
  2. local AutoRepeat = false
  3.  
  4. function _OnEvent(event, arg)
  5. --OutputLogMessage("event = %s, arg = %s\n", event, arg)
  6. -- adds ability to turn ON and OFF the script ---------------------------------------------------
  7.  
  8. if event == "MOUSE_BUTTON_PRESSED" and arg == 4 then
  9. ToggleState = not ToggleState
  10. if ToggleState then
  11. PressKey("f7")
  12. else
  13. ReleaseKey("f7")
  14. end
  15. end
  16.  
  17. if event == "MOUSE_BUTTON_PRESSED" and arg == 8 then
  18. ToggleState = not ToggleState
  19. if ToggleState then
  20. PressKey("q")
  21. PressKey("w")
  22. PressKey("r")
  23. else
  24. ReleaseKey("q")
  25. ReleaseKey("w")
  26. ReleaseKey("r")
  27. end
  28. end
  29.  
  30. if event == "PROFILE_ACTIVATED" then
  31. PressLoop_task = NewTask(PressLoop)
  32. PressLoop_task.SetRepeat(true)
  33. end
  34. if (event == "MOUSE_BUTTON_PRESSED" and arg == 7) then -- Press Mouse 7 to toggle AutoRepeat
  35. AutoRepeat = not AutoRepeat
  36. if (AutoRepeat == false) then
  37. PressLoop_task.Start()
  38. else
  39. PressLoop_task.Stop()
  40. end
  41. end
  42. end
  43.  
  44. function PressLoop()
  45. PressAndReleaseKey("f7")
  46. Sleep(math.random(62,157))
  47. end
  48.  
  49.  
  50. -------------------------------------------------
  51. -- The following is for polling. Do not alter.
  52. -------------------------------------------------
  53. _StartUpParameters = {
  54. PollDevice = "mouse",
  55. PollDelay = 10,
  56. AutoTaskSleep = true,
  57. }
  58. function _PreEvent() end
  59. function _PostEvent()
  60. _TaskHandler.Execute()
  61. end
  62. function OnEvent(event, arg, family)
  63. if event == "PROFILE_ACTIVATED" then
  64. _TaskHandler = InitTaskHandler()
  65. Poll = InitPolling(_StartUpParameters.PollDelay, _StartUpParameters.PollDevice, _PreEvent, _PostEvent)
  66. end
  67. Poll.Execute(event, arg, family)
  68. end
  69.  
  70. ----------------------------
  71. -- Polling Class
  72. ----------------------------
  73. function InitPolling(PollDelay, PollDevice, PreOnEventFunc, PostOnEventFunc)
  74. local self = {
  75. PollDelay = PollDelay,
  76. PollDevice = PollDevice,
  77. PreOnEventFunc = PreOnEventFunc,
  78. PostOnEventFunc = PostOnEventFunc,
  79. Sleep = Sleep_hook,
  80. }
  81. local function CreateEvent() SetMKeyState(1, self.PollDevice) end
  82. local function OnEvent(event, arg, family)
  83. if self.PreOnEventFunc then self.PreOnEventFunc() end
  84. _OnEvent(event, arg, family)
  85. if self.PostOnEventFunc then self.PostOnEventFunc() end
  86. end
  87. function self.Execute(event, arg, family)
  88. if event == "PROFILE_ACTIVATED" then
  89. if _OnActivated then _OnActivated(event, arg, family) end
  90. OnEvent(event, arg, family)
  91. CreateEvent() -- initiates the first polling event
  92. elseif event == "M_RELEASED" and family == self.PollDevice then
  93. OnEvent("POLLING", 0, self.PollDevice)
  94. CreateEvent()
  95. self.Sleep(self.PollDelay)
  96. elseif event == "M_PRESSED" and family == self.PollDevice then
  97. OnEvent("POLLING", 0, self.PollDevice)
  98. self.Sleep(self.PollDelay)
  99. elseif event == "PROFILE_DEACTIVATED" then
  100. if _OnDeactivated then _OnDeactivated(event, arg, family) end
  101. else
  102. OnEvent(event, arg, family)
  103. end
  104. end
  105. function self.SetPreOnEventFunc(func) self.PreOnEventFunc = func end
  106. function self.SetPostOnEventFunc(func) self.PosOnEventFunc = func end
  107. return self
  108. end
  109.  
  110. ------------------------
  111. -- Task Class
  112. ------------------------
  113. function TaskSleep(delay) return coroutine.yield(delay) end
  114. function NewTask(func, ...)
  115. local self = {
  116. _Func = func,
  117. _Running = false,
  118. _Co = nil,
  119. _ResumeRunningTime = -1,
  120. _AtStart = false,
  121. _Repeat = false,
  122. _Vars = nil,
  123. _TH = _TaskHandler or nil,
  124. }
  125. function self.ChangeVars(...) self._Vars = { ... } end
  126. function self.SetRepeat(r) self._Repeat = r end
  127. function self.GetRepeat() return self._Repeat end
  128. function self.Create()
  129. self._Running = false
  130. self._Co = coroutine.create(self._Func)
  131. self._AtStart = true
  132. end
  133. function self.Start()
  134. if not self.IsAtStart() or not self.IsCreated() then
  135. self.Create()
  136. end
  137. self._Running = true
  138. end
  139. function self.Stop() self._Running = false; self._Co = nil end
  140. function self.GetStatus()
  141. if self._Co then return coroutine.status(self._Co)
  142. else return nil end
  143. end
  144. function self.IsAtStart() return self._AtStart end
  145. function self.IsAtEnd() return self.IsDead() end
  146. function self.IsCreated()
  147. if self._Co then return true
  148. else return false end
  149. end
  150. function self.IsDead()
  151. if self._Co and self.GetStatus() == "dead" then return true
  152. else return false end
  153. end
  154. function self.IsRunning()
  155. if self.IsCreated() and self._Running and not self.IsDead() then return true
  156. else return false end
  157. end
  158. function self.IsReady()
  159. if self._Running and self.IsCreated() and not self.IsDead()
  160. and self._ResumeRunningTime <= GetRunningTime() then
  161. return true
  162. else return false end
  163. end
  164. function self.Pause() self._Running = false end
  165. function self.Resume() self._Running = true end
  166. function self.Execute()
  167. if self.GetRepeat() and self.IsDead() and self._Running then self.Start() end
  168. if self.IsReady() then
  169. local status, delay = coroutine.resume(self._Co, unpack(self._Vars))
  170. self._AtStart = false
  171. if delay then self._ResumeRunningTime = delay + GetRunningTime()
  172. else self._ResumeRunningTime = -1 end
  173. return status
  174. end
  175. end
  176. function self.Destroy()
  177. if self._TH then self._TH.RemoveTask(self) end
  178. self = nil
  179. return nil
  180. end
  181. function self.Remove() self.Destroy() end
  182. self.ChangeVars(...)
  183. self.Create()
  184. if self._TH then self._TH.AddTask(self) end
  185. return self
  186. end
  187.  
  188. --------------------------
  189. -- TaskHandler
  190. --------------------------
  191. function InitTaskHandler()
  192. local self = { _TaskList = {}, }
  193. function self.AddTask(Task) self._TaskList[Task] = true end
  194. function self.RemoveTask(Task) self._TaskList[Task] = nil end
  195. function self.Execute()
  196. for k,v in pairs(self._TaskList) do k.Execute() end
  197. end
  198. return self
  199. end
  200. coroutine.running_hook = coroutine.running
  201. function coroutine.running()
  202. local v = coroutine.running_hook()
  203. return v
  204. end
  205. Sleep_hook = Sleep
  206. function Sleep(d)
  207. if _StartUpParameters.AutoTaskSleep and coroutine.running() then return TaskSleep(d)
  208. else return Sleep_hook(d) end
  209.  
  210. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement