dev2919

Untitled

Feb 10th, 2017
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.33 KB | None | 0 0
  1.  
  2. DEVICE = "mouse" -- "kb" for keyboard, "lhc" for left handed controller, such as the G13
  3.  
  4. ------------------------------------------------------------------------------------------------------------
  5.  
  6. POLL_DELAY = 1
  7.  
  8. POLL_FAMILY = "kb" -- The device polling will take place on. Prefered to not be on macroed device.
  9.  
  10. ------------------------------------------------------------------------------------------------------------
  11.  
  12.  
  13.  
  14. speed = POLL_DELAY -- this should alwasy be equal or larger than POLL_DELAY
  15.  
  16. inc = 2
  17.  
  18.  
  19.  
  20. lcd = OutputLCDMessage
  21.  
  22.  
  23.  
  24. function _onActivation(event, arg, family)
  25.  
  26. --
  27.  
  28. -- ADD ANY START UP ROUTINES HERE
  29.  
  30. --
  31.  
  32.  
  33.  
  34. ctDiscoColors = CoThread:new(discoColors)
  35.  
  36. ctDiscoColors:start()
  37.  
  38. Toggle = true
  39.  
  40. end
  41.  
  42.  
  43.  
  44. function _OnEvent(event, arg, family)
  45.  
  46. --
  47.  
  48. -- ADD EVENT FUNCTIONALITY HERE
  49.  
  50. --
  51.  
  52.  
  53.  
  54. if event == "G_PRESSED" and arg == 1 then
  55.  
  56. Toggle = not Toggle
  57.  
  58. if Toggle then
  59.  
  60. lcd("Fade resumed.", 3000)
  61.  
  62. else
  63.  
  64. lcd("Fade paused.", 3000)
  65.  
  66. end
  67.  
  68. end
  69.  
  70. if event == "G_PRESSED" and arg == 2 then
  71.  
  72. speed = speed-POLL_DELAY
  73.  
  74. if speed < POLL_DELAY then -- if speed is 0 or less, the program will not reliquish control
  75.  
  76. speed = POLL_DELAY
  77.  
  78. end
  79.  
  80. lcd(speed .. "ms between updates")
  81.  
  82. end
  83.  
  84. if event == "G_PRESSED" and arg == 3 then
  85.  
  86. speed = speed+POLL_DELAY
  87.  
  88. lcd(speed .. "ms between updates")
  89.  
  90. end
  91.  
  92.  
  93.  
  94. if event == "G_PRESSED" and arg == 4 then
  95.  
  96. inc = inc + 1
  97.  
  98. lcd(inc.." color increment(s)")
  99.  
  100. end
  101.  
  102. if event == "G_PRESSED" and arg == 5 then
  103.  
  104. if inc > 1 then
  105.  
  106. inc = inc - 1
  107.  
  108. end
  109.  
  110. lcd(inc.." color increment(s)")
  111.  
  112. end
  113.  
  114.  
  115.  
  116. if Toggle then
  117.  
  118. ctDiscoColors:run()
  119.  
  120. end
  121.  
  122. end
  123.  
  124.  
  125.  
  126. function discoColors(ct)
  127.  
  128. while true do
  129.  
  130.  
  131.  
  132. for i=0,25,inc do
  133.  
  134. SetBacklightColor(255,0,0,DEVICE)
  135.  
  136. ct:sleep(25-i)
  137.  
  138. SetBacklightColor(255,255,255,DEVICE)
  139.  
  140. ct:sleep(i)
  141.  
  142. end
  143.  
  144. ct:sleep(6)
  145.  
  146. for i=0,25,inc do
  147.  
  148. SetBacklightColor(0,255,0,DEVICE)
  149.  
  150. ct:sleep(25-i)
  151.  
  152. SetBacklightColor(255,255,255,DEVICE)
  153.  
  154. ct:sleep(i)
  155.  
  156. end
  157.  
  158. ct:sleep(6)
  159.  
  160. for i=0,25,inc do
  161.  
  162. SetBacklightColor(0,0,255,DEVICE)
  163.  
  164. ct:sleep(25-i)
  165.  
  166. SetBacklightColor(255,255,255,DEVICE)
  167.  
  168. ct:sleep(i)
  169.  
  170. end
  171.  
  172. ct:sleep(6)
  173.  
  174. for i=0,25,inc do
  175.  
  176. SetBacklightColor(255,0,0,DEVICE)
  177.  
  178. ct:sleep(25-i)
  179.  
  180. SetBacklightColor(255,255,255,DEVICE)
  181.  
  182. ct:sleep(i)
  183.  
  184. end
  185.  
  186. ct:sleep(6)
  187.  
  188. for i=0,25,inc do
  189.  
  190. SetBacklightColor(0,255,0,DEVICE)
  191.  
  192. ct:sleep(25-i)
  193.  
  194. SetBacklightColor(255,255,255,DEVICE)
  195.  
  196. ct:sleep(i)
  197.  
  198. end
  199.  
  200. ct:sleep(6)
  201.  
  202. for i=0,25,inc do
  203.  
  204. SetBacklightColor(0,0,255,DEVICE)
  205.  
  206. ct:sleep(25-i)
  207.  
  208. SetBacklightColor(255,255,255,DEVICE)
  209.  
  210. ct:sleep(i)
  211.  
  212. end
  213.  
  214. ct:sleep(6)
  215.  
  216.  
  217.  
  218. end
  219.  
  220. end
  221.  
  222.  
  223.  
  224. --------------------------------------------------------------------------------------------------------
  225.  
  226. --------------------------------------------------------------------------------------------------------
  227.  
  228. --
  229.  
  230. -- NOTHING BELOW HERE NEEDS TO BE ALTERED
  231.  
  232. --
  233.  
  234. --------------------------------------------------------------------------------------------------------
  235.  
  236.  
  237.  
  238. function OnEvent(event, arg, family)
  239.  
  240. -- POLLING INITIATED HERE
  241.  
  242. _GetRunningTime = GetRunningTime()
  243.  
  244. if event == "PROFILE_ACTIVATED" then
  245.  
  246. ClearLog()
  247.  
  248. POLL = Poll:new(POLL_FAMILY, POLL_DELAY)
  249.  
  250. POLL:start()
  251.  
  252. if KB_EVENTS then
  253.  
  254. kb = EventHandler:new("kb")
  255.  
  256. kb:ProcessEvent(event, arg, family)
  257.  
  258. end
  259.  
  260. if LHC_EVENTS then
  261.  
  262. lhc = EventHandler:new("lhc")
  263.  
  264. lhc:ProcessEvent(event, arg, family)
  265.  
  266. end
  267.  
  268. _onActivation(event, arg, family)
  269.  
  270. else
  271.  
  272. -- POLLING ROUTINES BELOW
  273.  
  274. POLL:run(event,arg,family)
  275.  
  276. if KB_EVENTS then kb:ProcessEvent(event, arg, family) end
  277.  
  278. if LHC_EVENTS then lhc:ProcessEvent(event, arg, family) end
  279.  
  280. _GetRunningTime = GetRunningTime()
  281.  
  282. _OnEvent(event, arg, family) -- Runs myOnEvent to compartmentalize
  283.  
  284. end
  285.  
  286. if KB_EVENTS then kb:CleanupAfterEvent() end
  287.  
  288. if LHC_EVENTS then lhc:CleanupAfterEvent() end
  289.  
  290. end
  291.  
  292.  
  293.  
  294. log = OutputLogMessage
  295.  
  296.  
  297.  
  298. function Type(v)
  299.  
  300. if type(v) == "table" then
  301.  
  302. if v.Type ~= nil then
  303.  
  304. return v.Type
  305.  
  306. elseif v.type ~= nil then
  307.  
  308. return v.type
  309.  
  310. end
  311.  
  312. end
  313.  
  314. return type(v)
  315.  
  316. end
  317.  
  318.  
  319.  
  320. Poll = {}
  321.  
  322. Poll.__index = Poll
  323.  
  324. Poll.__newindex = function(table, key, value)
  325.  
  326. for k,v in pairs(table) do
  327.  
  328. if k == key then
  329.  
  330. rawset(table,key,value) return
  331.  
  332. end
  333.  
  334. end
  335.  
  336. error("'" .. key .. "' is not a member of Poll.", 2)
  337.  
  338. end
  339.  
  340.  
  341.  
  342. function Poll:new(family, delay)
  343.  
  344. local self = {}
  345.  
  346.  
  347.  
  348. self.delay = delay or 50
  349.  
  350. if family ~= "kb" and family ~= "lhc" then
  351.  
  352. error("Poll:new() - 'pFamily' must be 'kb' or 'lhc'", 2)
  353.  
  354. end
  355.  
  356. self.pFamily = family
  357.  
  358. self.presses = 0
  359.  
  360. self.running = false
  361.  
  362. self.runMKeyModifier = false
  363.  
  364. self.MKeyModifier = { nil, nil, nil } -- "default" or Modifiers. i.e. "lshift", "ctrl"
  365.  
  366. self.modFamily = ""
  367.  
  368. self.Type = "Poll"
  369.  
  370.  
  371.  
  372. setmetatable(self, Poll)
  373.  
  374.  
  375.  
  376. return self
  377.  
  378. end
  379.  
  380.  
  381.  
  382. function Poll:setMKeyModifier( m1, m2, m3, family )
  383.  
  384. self.runMKeyModifier = true
  385.  
  386. self.MKeyModifier[1] = m1
  387.  
  388. self.MKeyModifier[2] = m2
  389.  
  390. self.MKeyModifier[3] = m3
  391.  
  392. if family ~= "kb" and family ~= "lhc" then error("Poll:setMKeyModifier() family - needs to be 'kb' or 'lhc'",2) end
  393.  
  394. self.modFamily = family
  395.  
  396. end
  397.  
  398.  
  399.  
  400. function Poll:start()
  401.  
  402. self.running = true
  403.  
  404. self:setMKeyState()
  405.  
  406. end
  407.  
  408.  
  409.  
  410. function Poll:stop()
  411.  
  412. self.running = false
  413.  
  414. end
  415.  
  416.  
  417.  
  418. function Poll:_press()
  419.  
  420. if self.running then
  421.  
  422. self.presses = self.presses + 1
  423.  
  424. end
  425.  
  426. end
  427.  
  428.  
  429.  
  430. function Poll:release()
  431.  
  432. if self.running then
  433.  
  434. self.presses = self.presses - 1
  435.  
  436. end
  437.  
  438. end
  439.  
  440.  
  441.  
  442. function Poll:setMKeyState()
  443.  
  444. if self.runMKeyModifier then
  445.  
  446. local default = nil
  447.  
  448. local i = 1
  449.  
  450. local modMKeyState = nil
  451.  
  452.  
  453.  
  454. for i = 1, 3, 1 do
  455.  
  456. if self.MKeyModifier[i] == "default" then
  457.  
  458. default = i
  459.  
  460. elseif self.MKeyModifier[i] == "" then
  461.  
  462. -- do nothing
  463.  
  464. elseif string.find(self.MKeyModifier[i],"mb%d") then
  465.  
  466. local iMB = tonumber(string.sub(self.MKeyModifier[i],3))
  467.  
  468. if IsMouseButtonPressed(iMB) then
  469.  
  470. modMKeyState = i
  471.  
  472. end
  473.  
  474. elseif IsModifierPressed(self.MKeyModifier[i]) then
  475.  
  476. modMKeyState = i
  477.  
  478. end
  479.  
  480. end
  481.  
  482. if modMKeyState == nil then
  483.  
  484. modMKeyState = default
  485.  
  486. end
  487.  
  488. if modMKeyState ~= nil then
  489.  
  490. if self.pFamily == self.modFamily then
  491.  
  492. SetMKeyState(modMKeyState, self.pFamily)
  493.  
  494. return
  495.  
  496. elseif GetMKeyState(self.modFamily) ~= modMKeyState then
  497.  
  498. SetMKeyState(modMKeyState, self.modFamily)
  499.  
  500. end
  501.  
  502. end
  503.  
  504. end
  505.  
  506. SetMKeyState( GetMKeyState(self.pFamily), self.pFamily )
  507.  
  508. end
  509.  
  510.  
  511.  
  512. function Poll:run(event, arg, family)
  513.  
  514. if self.running and self.pFamily == family then
  515.  
  516. if event == "M_PRESSED" then
  517.  
  518. self:_press()
  519.  
  520. Sleep(self.delay)
  521.  
  522. elseif event == "M_RELEASED" then
  523.  
  524. if self.presses == 1 then
  525.  
  526. self:setMKeyState()
  527.  
  528. Sleep(self.delay)
  529.  
  530. end
  531.  
  532. self:release()
  533.  
  534. end
  535.  
  536. end
  537.  
  538. end
  539.  
  540.  
  541.  
  542. CoThread = {}
  543.  
  544. CoThread.__index = CoThread
  545.  
  546.  
  547.  
  548. function CoThread:new(f) -- f - function(MultiThread)
  549.  
  550. local self = {}
  551.  
  552.  
  553.  
  554. if type(f) ~= "function" then
  555.  
  556. error("A function is needed",2)
  557.  
  558. end
  559.  
  560. self.func = f
  561.  
  562. self.co = nil
  563.  
  564. self.sleepUntilTimeStamp = nil
  565.  
  566. self.Type = "CoThread"
  567.  
  568.  
  569.  
  570. setmetatable(self, CoThread)
  571.  
  572. return self
  573.  
  574. end
  575.  
  576.  
  577.  
  578. function CoThread:recreate()
  579.  
  580. self:kill()
  581.  
  582. self.co = coroutine.create(self.func)
  583.  
  584. end
  585.  
  586.  
  587.  
  588. function CoThread:create()
  589.  
  590. self.co = coroutine.create(self.func)
  591.  
  592. end
  593.  
  594.  
  595.  
  596. function CoThread:isDead()
  597.  
  598. if self.co == nil then
  599.  
  600. return true
  601.  
  602. elseif coroutine.status(self.co) == "dead" then
  603.  
  604. self.co = nil
  605.  
  606. return true
  607.  
  608. else
  609.  
  610. return false
  611.  
  612. end
  613.  
  614. end
  615.  
  616.  
  617.  
  618. function CoThread:run(...)
  619.  
  620. if self:isDead() then
  621.  
  622. return
  623.  
  624. end
  625.  
  626. if self.sleepUntilTimeStamp ~= nil then
  627.  
  628. if _GetRunningTime < self.sleepUntilTimeStamp then
  629.  
  630. return
  631.  
  632. else
  633.  
  634. self.sleepUntilTimeStamp = nil
  635.  
  636. end
  637.  
  638. end
  639.  
  640. coroutine.resume(self.co, self, ...)
  641.  
  642. end
  643.  
  644.  
  645.  
  646. function CoThread:kill()
  647.  
  648. self.co = nil
  649.  
  650. self.sleepUntilTimeStamp = nil
  651.  
  652. end
  653.  
  654.  
  655.  
  656. function CoThread:yield()
  657.  
  658. coroutine.yield()
  659.  
  660. end
  661.  
  662.  
  663.  
  664. function CoThread:sleep(Milliseconds)
  665.  
  666. self.sleepUntilTimeStamp = _GetRunningTime + Milliseconds
  667.  
  668.  
  669.  
  670. coroutine.yield()
  671.  
  672. end
  673.  
  674.  
  675.  
  676. function CoThread:start()
  677.  
  678. self:recreate()
  679.  
  680. end
  681.  
  682.  
  683.  
  684. function CoThread:stop()
  685.  
  686. self:kill()
  687.  
  688. end
  689.  
  690.  
  691.  
  692. function CoThread:isFinished()
  693.  
  694. return self:isDead()
  695.  
  696. end
  697. DEVICE = "mouse" -- "kb" for keyboard, "lhc" for left handed controller, such as the G13
  698.  
  699. ------------------------------------------------------------------------------------------------------------
  700.  
  701. POLL_DELAY = 1
  702.  
  703. POLL_FAMILY = "kb" -- The device polling will take place on. Prefered to not be on macroed device.
  704.  
  705. ------------------------------------------------------------------------------------------------------------
  706.  
  707.  
  708.  
  709. speed = POLL_DELAY -- this should alwasy be equal or larger than POLL_DELAY
  710.  
  711. inc = 2
  712.  
  713.  
  714.  
  715. lcd = OutputLCDMessage
  716.  
  717.  
  718.  
  719. function _onActivation(event, arg, family)
  720.  
  721. --
  722.  
  723. -- ADD ANY START UP ROUTINES HERE
  724.  
  725. --
  726.  
  727.  
  728.  
  729. ctDiscoColors = CoThread:new(discoColors)
  730.  
  731. ctDiscoColors:start()
  732.  
  733. Toggle = true
  734.  
  735. end
  736.  
  737.  
  738.  
  739. function _OnEvent(event, arg, family)
  740.  
  741. --
  742.  
  743. -- ADD EVENT FUNCTIONALITY HERE
  744.  
  745. --
  746.  
  747.  
  748.  
  749. if event == "G_PRESSED" and arg == 1 then
  750.  
  751. Toggle = not Toggle
  752.  
  753. if Toggle then
  754.  
  755. lcd("Fade resumed.", 3000)
  756.  
  757. else
  758.  
  759. lcd("Fade paused.", 3000)
  760.  
  761. end
  762.  
  763. end
  764.  
  765. if event == "G_PRESSED" and arg == 2 then
  766.  
  767. speed = speed-POLL_DELAY
  768.  
  769. if speed < POLL_DELAY then -- if speed is 0 or less, the program will not reliquish control
  770.  
  771. speed = POLL_DELAY
  772.  
  773. end
  774.  
  775. lcd(speed .. "ms between updates")
  776.  
  777. end
  778.  
  779. if event == "G_PRESSED" and arg == 3 then
  780.  
  781. speed = speed+POLL_DELAY
  782.  
  783. lcd(speed .. "ms between updates")
  784.  
  785. end
  786.  
  787.  
  788.  
  789. if event == "G_PRESSED" and arg == 4 then
  790.  
  791. inc = inc + 1
  792.  
  793. lcd(inc.." color increment(s)")
  794.  
  795. end
  796.  
  797. if event == "G_PRESSED" and arg == 5 then
  798.  
  799. if inc > 1 then
  800.  
  801. inc = inc - 1
  802.  
  803. end
  804.  
  805. lcd(inc.." color increment(s)")
  806.  
  807. end
  808.  
  809.  
  810.  
  811. if Toggle then
  812.  
  813. ctDiscoColors:run()
  814.  
  815. end
  816.  
  817. end
  818.  
  819.  
  820.  
  821. function discoColors(ct)
  822.  
  823. while true do
  824.  
  825.  
  826.  
  827. for i=0,25,inc do
  828.  
  829. SetBacklightColor(255,0,0,DEVICE)
  830.  
  831. ct:sleep(25-i)
  832.  
  833. SetBacklightColor(255,255,255,DEVICE)
  834.  
  835. ct:sleep(i)
  836.  
  837. end
  838.  
  839. ct:sleep(6)
  840.  
  841. for i=0,25,inc do
  842.  
  843. SetBacklightColor(0,255,0,DEVICE)
  844.  
  845. ct:sleep(25-i)
  846.  
  847. SetBacklightColor(255,255,255,DEVICE)
  848.  
  849. ct:sleep(i)
  850.  
  851. end
  852.  
  853. ct:sleep(6)
  854.  
  855. for i=0,25,inc do
  856.  
  857. SetBacklightColor(0,0,255,DEVICE)
  858.  
  859. ct:sleep(25-i)
  860.  
  861. SetBacklightColor(255,255,255,DEVICE)
  862.  
  863. ct:sleep(i)
  864.  
  865. end
  866.  
  867. ct:sleep(6)
  868.  
  869. for i=0,25,inc do
  870.  
  871. SetBacklightColor(255,0,0,DEVICE)
  872.  
  873. ct:sleep(25-i)
  874.  
  875. SetBacklightColor(255,255,255,DEVICE)
  876.  
  877. ct:sleep(i)
  878.  
  879. end
  880.  
  881. ct:sleep(6)
  882.  
  883. for i=0,25,inc do
  884.  
  885. SetBacklightColor(0,255,0,DEVICE)
  886.  
  887. ct:sleep(25-i)
  888.  
  889. SetBacklightColor(255,255,255,DEVICE)
  890.  
  891. ct:sleep(i)
  892.  
  893. end
  894.  
  895. ct:sleep(6)
  896.  
  897. for i=0,25,inc do
  898.  
  899. SetBacklightColor(0,0,255,DEVICE)
  900.  
  901. ct:sleep(25-i)
  902.  
  903. SetBacklightColor(255,255,255,DEVICE)
  904.  
  905. ct:sleep(i)
  906.  
  907. end
  908.  
  909. ct:sleep(6)
  910.  
  911.  
  912.  
  913. end
  914.  
  915. end
  916.  
  917.  
  918.  
  919. --------------------------------------------------------------------------------------------------------
  920.  
  921. --------------------------------------------------------------------------------------------------------
  922.  
  923. --
  924.  
  925. -- NOTHING BELOW HERE NEEDS TO BE ALTERED
  926.  
  927. --
  928.  
  929. --------------------------------------------------------------------------------------------------------
  930.  
  931.  
  932.  
  933. function OnEvent(event, arg, family)
  934.  
  935. -- POLLING INITIATED HERE
  936.  
  937. _GetRunningTime = GetRunningTime()
  938.  
  939. if event == "PROFILE_ACTIVATED" then
  940.  
  941. ClearLog()
  942.  
  943. POLL = Poll:new(POLL_FAMILY, POLL_DELAY)
  944.  
  945. POLL:start()
  946.  
  947. if KB_EVENTS then
  948.  
  949. kb = EventHandler:new("kb")
  950.  
  951. kb:ProcessEvent(event, arg, family)
  952.  
  953. end
  954.  
  955. if LHC_EVENTS then
  956.  
  957. lhc = EventHandler:new("lhc")
  958.  
  959. lhc:ProcessEvent(event, arg, family)
  960.  
  961. end
  962.  
  963. _onActivation(event, arg, family)
  964.  
  965. else
  966.  
  967. -- POLLING ROUTINES BELOW
  968.  
  969. POLL:run(event,arg,family)
  970.  
  971. if KB_EVENTS then kb:ProcessEvent(event, arg, family) end
  972.  
  973. if LHC_EVENTS then lhc:ProcessEvent(event, arg, family) end
  974.  
  975. _GetRunningTime = GetRunningTime()
  976.  
  977. _OnEvent(event, arg, family) -- Runs myOnEvent to compartmentalize
  978.  
  979. end
  980.  
  981. if KB_EVENTS then kb:CleanupAfterEvent() end
  982.  
  983. if LHC_EVENTS then lhc:CleanupAfterEvent() end
  984.  
  985. end
  986.  
  987.  
  988.  
  989. log = OutputLogMessage
  990.  
  991.  
  992.  
  993. function Type(v)
  994.  
  995. if type(v) == "table" then
  996.  
  997. if v.Type ~= nil then
  998.  
  999. return v.Type
  1000.  
  1001. elseif v.type ~= nil then
  1002.  
  1003. return v.type
  1004.  
  1005. end
  1006.  
  1007. end
  1008.  
  1009. return type(v)
  1010.  
  1011. end
  1012.  
  1013.  
  1014.  
  1015. Poll = {}
  1016.  
  1017. Poll.__index = Poll
  1018.  
  1019. Poll.__newindex = function(table, key, value)
  1020.  
  1021. for k,v in pairs(table) do
  1022.  
  1023. if k == key then
  1024.  
  1025. rawset(table,key,value) return
  1026.  
  1027. end
  1028.  
  1029. end
  1030.  
  1031. error("'" .. key .. "' is not a member of Poll.", 2)
  1032.  
  1033. end
  1034.  
  1035.  
  1036.  
  1037. function Poll:new(family, delay)
  1038.  
  1039. local self = {}
  1040.  
  1041.  
  1042.  
  1043. self.delay = delay or 50
  1044.  
  1045. if family ~= "kb" and family ~= "lhc" then
  1046.  
  1047. error("Poll:new() - 'pFamily' must be 'kb' or 'lhc'", 2)
  1048.  
  1049. end
  1050.  
  1051. self.pFamily = family
  1052.  
  1053. self.presses = 0
  1054.  
  1055. self.running = false
  1056.  
  1057. self.runMKeyModifier = false
  1058.  
  1059. self.MKeyModifier = { nil, nil, nil } -- "default" or Modifiers. i.e. "lshift", "ctrl"
  1060.  
  1061. self.modFamily = ""
  1062.  
  1063. self.Type = "Poll"
  1064.  
  1065.  
  1066.  
  1067. setmetatable(self, Poll)
  1068.  
  1069.  
  1070.  
  1071. return self
  1072.  
  1073. end
  1074.  
  1075.  
  1076.  
  1077. function Poll:setMKeyModifier( m1, m2, m3, family )
  1078.  
  1079. self.runMKeyModifier = true
  1080.  
  1081. self.MKeyModifier[1] = m1
  1082.  
  1083. self.MKeyModifier[2] = m2
  1084.  
  1085. self.MKeyModifier[3] = m3
  1086.  
  1087. if family ~= "kb" and family ~= "lhc" then error("Poll:setMKeyModifier() family - needs to be 'kb' or 'lhc'",2) end
  1088.  
  1089. self.modFamily = family
  1090.  
  1091. end
  1092.  
  1093.  
  1094.  
  1095. function Poll:start()
  1096.  
  1097. self.running = true
  1098.  
  1099. self:setMKeyState()
  1100.  
  1101. end
  1102.  
  1103.  
  1104.  
  1105. function Poll:stop()
  1106.  
  1107. self.running = false
  1108.  
  1109. end
  1110.  
  1111.  
  1112.  
  1113. function Poll:_press()
  1114.  
  1115. if self.running then
  1116.  
  1117. self.presses = self.presses + 1
  1118.  
  1119. end
  1120.  
  1121. end
  1122.  
  1123.  
  1124.  
  1125. function Poll:release()
  1126.  
  1127. if self.running then
  1128.  
  1129. self.presses = self.presses - 1
  1130.  
  1131. end
  1132.  
  1133. end
  1134.  
  1135.  
  1136.  
  1137. function Poll:setMKeyState()
  1138.  
  1139. if self.runMKeyModifier then
  1140.  
  1141. local default = nil
  1142.  
  1143. local i = 1
  1144.  
  1145. local modMKeyState = nil
  1146.  
  1147.  
  1148.  
  1149. for i = 1, 3, 1 do
  1150.  
  1151. if self.MKeyModifier[i] == "default" then
  1152.  
  1153. default = i
  1154.  
  1155. elseif self.MKeyModifier[i] == "" then
  1156.  
  1157. -- do nothing
  1158.  
  1159. elseif string.find(self.MKeyModifier[i],"mb%d") then
  1160.  
  1161. local iMB = tonumber(string.sub(self.MKeyModifier[i],3))
  1162.  
  1163. if IsMouseButtonPressed(iMB) then
  1164.  
  1165. modMKeyState = i
  1166.  
  1167. end
  1168.  
  1169. elseif IsModifierPressed(self.MKeyModifier[i]) then
  1170.  
  1171. modMKeyState = i
  1172.  
  1173. end
  1174.  
  1175. end
  1176.  
  1177. if modMKeyState == nil then
  1178.  
  1179. modMKeyState = default
  1180.  
  1181. end
  1182.  
  1183. if modMKeyState ~= nil then
  1184.  
  1185. if self.pFamily == self.modFamily then
  1186.  
  1187. SetMKeyState(modMKeyState, self.pFamily)
  1188.  
  1189. return
  1190.  
  1191. elseif GetMKeyState(self.modFamily) ~= modMKeyState then
  1192.  
  1193. SetMKeyState(modMKeyState, self.modFamily)
  1194.  
  1195. end
  1196.  
  1197. end
  1198.  
  1199. end
  1200.  
  1201. SetMKeyState( GetMKeyState(self.pFamily), self.pFamily )
  1202.  
  1203. end
  1204.  
  1205.  
  1206.  
  1207. function Poll:run(event, arg, family)
  1208.  
  1209. if self.running and self.pFamily == family then
  1210.  
  1211. if event == "M_PRESSED" then
  1212.  
  1213. self:_press()
  1214.  
  1215. Sleep(self.delay)
  1216.  
  1217. elseif event == "M_RELEASED" then
  1218.  
  1219. if self.presses == 1 then
  1220.  
  1221. self:setMKeyState()
  1222.  
  1223. Sleep(self.delay)
  1224.  
  1225. end
  1226.  
  1227. self:release()
  1228.  
  1229. end
  1230.  
  1231. end
  1232.  
  1233. end
  1234.  
  1235.  
  1236.  
  1237. CoThread = {}
  1238.  
  1239. CoThread.__index = CoThread
  1240.  
  1241.  
  1242.  
  1243. function CoThread:new(f) -- f - function(MultiThread)
  1244.  
  1245. local self = {}
  1246.  
  1247.  
  1248.  
  1249. if type(f) ~= "function" then
  1250.  
  1251. error("A function is needed",2)
  1252.  
  1253. end
  1254.  
  1255. self.func = f
  1256.  
  1257. self.co = nil
  1258.  
  1259. self.sleepUntilTimeStamp = nil
  1260.  
  1261. self.Type = "CoThread"
  1262.  
  1263.  
  1264.  
  1265. setmetatable(self, CoThread)
  1266.  
  1267. return self
  1268.  
  1269. end
  1270.  
  1271.  
  1272.  
  1273. function CoThread:recreate()
  1274.  
  1275. self:kill()
  1276.  
  1277. self.co = coroutine.create(self.func)
  1278.  
  1279. end
  1280.  
  1281.  
  1282.  
  1283. function CoThread:create()
  1284.  
  1285. self.co = coroutine.create(self.func)
  1286.  
  1287. end
  1288.  
  1289.  
  1290.  
  1291. function CoThread:isDead()
  1292.  
  1293. if self.co == nil then
  1294.  
  1295. return true
  1296.  
  1297. elseif coroutine.status(self.co) == "dead" then
  1298.  
  1299. self.co = nil
  1300.  
  1301. return true
  1302.  
  1303. else
  1304.  
  1305. return false
  1306.  
  1307. end
  1308.  
  1309. end
  1310.  
  1311.  
  1312.  
  1313. function CoThread:run(...)
  1314.  
  1315. if self:isDead() then
  1316.  
  1317. return
  1318.  
  1319. end
  1320.  
  1321. if self.sleepUntilTimeStamp ~= nil then
  1322.  
  1323. if _GetRunningTime < self.sleepUntilTimeStamp then
  1324.  
  1325. return
  1326.  
  1327. else
  1328.  
  1329. self.sleepUntilTimeStamp = nil
  1330.  
  1331. end
  1332.  
  1333. end
  1334.  
  1335. coroutine.resume(self.co, self, ...)
  1336.  
  1337. end
  1338.  
  1339.  
  1340.  
  1341. function CoThread:kill()
  1342.  
  1343. self.co = nil
  1344.  
  1345. self.sleepUntilTimeStamp = nil
  1346.  
  1347. end
  1348.  
  1349.  
  1350.  
  1351. function CoThread:yield()
  1352.  
  1353. coroutine.yield()
  1354.  
  1355. end
  1356.  
  1357.  
  1358.  
  1359. function CoThread:sleep(Milliseconds)
  1360.  
  1361. self.sleepUntilTimeStamp = _GetRunningTime + Milliseconds
  1362.  
  1363.  
  1364.  
  1365. coroutine.yield()
  1366.  
  1367. end
  1368.  
  1369.  
  1370.  
  1371. function CoThread:start()
  1372.  
  1373. self:recreate()
  1374.  
  1375. end
  1376.  
  1377.  
  1378.  
  1379. function CoThread:stop()
  1380.  
  1381. self:kill()
  1382.  
  1383. end
  1384.  
  1385.  
  1386.  
  1387. function CoThread:isFinished()
  1388.  
  1389. return self:isDead()
  1390.  
  1391. end
Add Comment
Please, Sign In to add comment