Guest User

Untitled

a guest
Dec 25th, 2013
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 27.28 KB | None | 0 0
  1. -- Code update by Bystander on  8/28/2011
  2. ------------------------------------------------------------------------------------------------------------
  3. POLL_DELAY = 25     -- Sleep time after each poll (beware of number below 25)
  4. POLL_FAMILY = "kb"  -- The device polling will take place on.  Prefered to not be on macroed device.
  5. -- KB_EVENTS = true    -- If you don't have a logitech keyboard, make this false/nil or comment out
  6. -- LHC_EVENTS = true   -- if you don't have a logitech left handed controller, make this false/nil or comment out
  7. ------------------------------------------------------------------------------------------------------------
  8.  
  9. DEVICE = "kb"
  10. color = 1
  11. mouse = true
  12. button = 1
  13. speed = POLL_DELAY  -- this should alwasy be equal or larger than POLL_DELAY
  14. disco = 0
  15. mouseRepeatTime = 5000  -- in milliseconds
  16. mouseDelay = 250
  17. mousePressTime = 25
  18.  
  19. function _onActivation(event, arg, family)
  20. --
  21. -- ADD ANY START UP ROUTINES HERE
  22. --
  23. --    POLL:setMKeyModifier( "default", "mb5", "mb4", "lhc" )  -- Allows the use of Modifiers to change M pages
  24.  
  25.     ctDiscoColors1 = CoThread:new(discoColors1)
  26.     ctDiscoColors2 = CoThread:new(discoColors2)
  27.     ctRandomColors = CoThread:new(randomColors)
  28.     ctRepeatMouse = CoThread:new(repeatMouse)
  29. end
  30.  
  31. function _OnEvent(event, arg, family)
  32. --
  33. -- ADD EVENT FUNCTIONALITY HERE
  34. --
  35.  
  36.     if (event == "G_PRESSED" and arg == 1 and GetMKeyState(DEVICE) == 1) then
  37.         ClearLCD()
  38.         if color == 1 then
  39.             ctRandomColors:stop()   -- stops randomColors if it was running()
  40.             SetBacklightColor(255,0,0,DEVICE)
  41.             color = 2
  42.             OutputLCDMessage("Color changed to: Red", 2000)
  43.         elseif color == 3 then
  44.             SetBacklightColor(0,0,255,DEVICE)
  45.             color = 4
  46.             OutputLCDMessage("Color changed to: Blue", 2000)
  47.         elseif color == 2 then
  48.             SetBacklightColor(0,255,0,DEVICE)
  49.             color = 3
  50.             OutputLCDMessage("Color changed to: Green", 2000)
  51.         elseif color == 4 then
  52.             color = 5
  53.             OutputLCDMessage("Color changed to: Fading", 2000)
  54.             ctDiscoColors1:start()  -- starts discoColors
  55.         elseif color == 5 then
  56.             ctDiscoColors1:stop()
  57.             color = 6
  58.             OutputLCDMessage("Color changed to: Blinking", 2000)
  59.             ctDiscoColors2:start()  -- starts discoColors
  60.         elseif color == 6 then
  61.             ctDiscoColors2:stop()   -- stops discoColors
  62.             color = 1
  63.             OutputLCDMessage("Color changed to: Random", 2000)
  64.                 ctRandomColors:start() -- starts randomColors
  65.         end
  66.     end -- line 28
  67.     if (event == "G_PRESSED" and arg == 3 and disco == 1) then
  68.         speed = speed-25
  69.         OutputLCDMessage("Speed decreased: " .. speed, 2000)
  70.         if speed < POLL_DELAY   then    -- if speed is 0 or less, the program will not reliquish control
  71.             speed = POLL_DELAY
  72.         end
  73.     end
  74.     if (event == "G_PRESSED" and arg == 6 and disco == 1) then
  75.         OutputLogMessage("speed\n")
  76.         OutputLCDMessage("Speed increased: " .. speed, 2000)
  77.         speed = speed+25
  78.     end
  79.     if (event == "G_PRESSED" and arg == 5) then
  80.         if (button < 5) then
  81.             button = button + 1
  82.             OutputLCDMessage("Mousebutton changed too: " .. button, 2000)
  83.         else
  84.             button = 1
  85.             OutputLCDMessage("Mousebutton changed too: " .. button, 2000)
  86.         end
  87.  
  88.     end
  89.     if (event == "G_PRESSED" and arg == 2) then
  90.         if (mouse) then
  91.             ctRepeatMouse:start()
  92.             mouse = false
  93.         else
  94.             ctRepeatMouse:stop()
  95.             mouse = true
  96.         end
  97.     end
  98.  
  99.     ctDiscoColors1:run()
  100.     ctDiscoColors2:run() -- this will run discoColors as long as it hasn't finished or been stopped with :stop()
  101.     ctRandomColors:run() -- this will run randomColors as long as it hasn't finished or been stopped with :stop()
  102.     ctRepeatMouse:run()
  103. end
  104.  
  105. function repeatMouse(ct)
  106.     while true do
  107.         PressMouseButton(button)
  108.         log("PressMouseButton\n")
  109.         ct:sleep(mousePressTime)
  110.         ReleaseMouseButton(button)
  111.         ct:sleep(mouseDelay)
  112.     end
  113. end
  114.  
  115. function randomColors(ct)
  116.     while true do
  117.         disco = 1
  118.         SetBacklightColor(math.random(0,255),math.random(0,255),math.random(0,255),DEVICE)
  119.         ct:sleep(speed)
  120.         if color ~= 1 then -- if color = 1 it'll continue to loop as before
  121.             disco = 0
  122.             break
  123.         end
  124.     end
  125. end
  126.  
  127. function discoColors1(ct)
  128.     while true do
  129.         disco = 1
  130.  
  131.         for i = 0, 255, 15 do
  132.             SetBacklightColor(i,0,255,DEVICE)
  133. --          OutputLogMessage("Red: %i, Green: %i, Blue: %i\n", i, 0, 255)
  134.             ct:sleep(speed)
  135.         end
  136.  
  137.     -- now to red
  138.         for i = 255, 0, -15 do
  139.             SetBacklightColor(255,0,i,DEVICE)
  140. --          OutputLogMessage("Red: %i, Green: %i, Blue: %i\n", 255, 0, i)
  141.             ct:sleep(speed)
  142.         end
  143.  
  144.     -- now to green-red
  145.         for i = 0, 255, 15 do
  146.             SetBacklightColor(255,i,0,DEVICE)
  147. --          OutputLogMessage("Red: %i, Green: %i, Blue: %i\n", 255, i, 0)
  148.             ct:sleep(speed)
  149.         end
  150.  
  151.     -- now to green
  152.         for i = 255, 0, -15 do
  153.             SetBacklightColor(i,255,0,DEVICE)
  154. --          OutputLogMessage("Red: %i, Green: %i, Blue: %i\n", i, 255, 0)
  155.             ct:sleep(speed)
  156.         end
  157.  
  158.     -- now to green-blue
  159.         for i = 0, 255, 15 do
  160.             SetBacklightColor(0,255,i,DEVICE)
  161. --          OutputLogMessage("Red: %i, Green: %i, Blue: %i\n", 0, 255, i)
  162.             ct:sleep(speed)
  163.         end
  164.  
  165.     -- back to blue
  166.         for i = 255, 0, -15 do
  167.             SetBacklightColor(0,i,255,DEVICE)
  168. --          OutputLogMessage("Red: %i, Green: %i, Blue: %i\n", 0, i, 255)
  169.             ct:sleep(speed)
  170.         end
  171.     -- done
  172.  
  173.         if color ~= 5 then    -- if color == 5 it will continue to loop as before
  174.             disco = 0
  175.             break
  176.         end
  177.     end
  178. end
  179.  
  180. function discoColors2(ct)
  181.     while true do
  182.         disco = 1
  183.  
  184.         for i = 0, 255, 15 do
  185.             SetBacklightColor(i,0,255,DEVICE)
  186. --          OutputLogMessage("Red: %i, Green: %i, Blue: %i\n", i, 0, 255)
  187.             ct:sleep(speed)
  188.             SetBacklightColor(0,0,0,DEVICE)
  189.             ct:sleep(speed)
  190.         end
  191.  
  192.     -- now to red
  193.         for i = 255, 0, -15 do
  194.             SetBacklightColor(255,0,i,DEVICE)
  195. --          OutputLogMessage("Red: %i, Green: %i, Blue: %i\n", 255, 0, i)
  196.             ct:sleep(speed)
  197.             SetBacklightColor(0,0,0,DEVICE)
  198.             ct:sleep(speed)
  199.         end
  200.  
  201.     -- now to green-red
  202.         for i = 0, 255, 15 do
  203.             SetBacklightColor(255,i,0,DEVICE)
  204. --          OutputLogMessage("Red: %i, Green: %i, Blue: %i\n", 255, i, 0)
  205.             ct:sleep(speed)
  206.             SetBacklightColor(0,0,0,DEVICE)
  207.             ct:sleep(speed)
  208.         end
  209.  
  210.     -- now to green
  211.         for i = 255, 0, -15 do
  212.             SetBacklightColor(i,255,0,DEVICE)
  213. --          OutputLogMessage("Red: %i, Green: %i, Blue: %i\n", i, 255, 0)
  214.             ct:sleep(speed)
  215.             SetBacklightColor(0,0,0,DEVICE)
  216.             ct:sleep(speed)
  217.         end
  218.  
  219.     -- now to green-blue
  220.         for i = 0, 255, 15 do
  221.             SetBacklightColor(0,255,i,DEVICE)
  222. --          OutputLogMessage("Red: %i, Green: %i, Blue: %i\n", 0, 255, i)
  223.             ct:sleep(speed)
  224.             SetBacklightColor(0,0,0,DEVICE)
  225.             ct:sleep(speed)
  226.         end
  227.  
  228.     -- back to blue
  229.         for i = 255, 0, -15 do
  230.             SetBacklightColor(0,i,255,DEVICE)
  231. --          OutputLogMessage("Red: %i, Green: %i, Blue: %i\n", 0, i, 255)
  232.             ct:sleep(speed)
  233.             SetBacklightColor(0,0,0,DEVICE)
  234.             ct:sleep(speed)
  235.         end
  236.     -- done
  237.  
  238.         if color ~= 6 then    -- if color == 5 it will continue to loop as before
  239.             disco = 0
  240.             break
  241.         end
  242.     end
  243. end
  244.  
  245. --------------------------------------------------------------------------------------------------------
  246. --------------------------------------------------------------------------------------------------------
  247. --
  248. --                             NOTHING BELOW HERE NEEDS TO BE ALTERED
  249. --
  250. --------------------------------------------------------------------------------------------------------
  251. --------------------------------------------------------------------------------------------------------
  252. -- function Type(v)  -- returns what type of object v is.  It works with my objects as well
  253. -- function log(...) -- MessageLogOutput(...)
  254. -- function typeString(string,delay)    -- will type out the string with a Sleep(delay) between keys
  255. --------------------------------------------------------------------------------------------------------
  256. --------------------------------------------------------------------------------------------------------
  257. -- function CoThread:new(f)      f - function(coThread,...)   coThread is the controlling CoThread
  258. -- function CoThread:recreate()  kill existing coroutine and recreate
  259. -- function CoThread:create()    creates coroutine
  260. -- function CoThread:isDead()    true if coroutine is dead or nil/false
  261. -- function CoThread:run(...)    run coroutine with variables don't include mt
  262. -- function CoThread:kill()      kill coroutine
  263. -- function CoThread:yield()     use in function(coThread,...) to yield
  264. -- function CoThread:sleep(Milliseconds) use in function(coThread,...) to sleep for Milliseconds
  265. -- function CoThread:start()     does the same thing as recreate()
  266. -- function CoThread:stop()      does the same thing as kill()
  267. -- function CoThread:isFinished() same as isDead()
  268. --------------------------------------------------------------------------------------------------------
  269. --------------------------------------------------------------------------------------------------------
  270. -- function Poll:new(family, delay)         delay is sleep time between polls
  271. -- function Poll:run(event, arg, family)    handles events and performs the polling as long as it's running
  272. -- function Poll:setMKeyModifier( m1, m2, m3, family )  Allows the use of Modifiers to change M pages
  273. -- function Poll:start()                    turn on polling
  274. -- function Poll:stop()                     stop polling
  275. --      function Poll:_press()                  NOT TO BE USED
  276. --      function Poll:release()                 NOT TO BE USED
  277. --      function Poll:setMKeyState(MKeyState)   NOT TO BE USED
  278. ------------------------------------------------------------------------------------------------------------
  279.  
  280. function OnEvent(event, arg, family)
  281. -- POLLING INITIATED HERE
  282.     _GetRunningTime = GetRunningTime()
  283.     if event == "PROFILE_ACTIVATED" then
  284.         ClearLog()
  285.         POLL = Poll:new(POLL_FAMILY, POLL_DELAY)
  286.         POLL:start()
  287.         if KB_EVENTS then
  288.             kb = EventHandler:new("kb")
  289.             kb:ProcessEvent(event, arg, family)
  290.         end
  291.         if LHC_EVENTS then
  292.             lhc = EventHandler:new("lhc")
  293.             lhc:ProcessEvent(event, arg, family)
  294.         end
  295.         _onActivation(event, arg, family)
  296.     else
  297. -- POLLING ROUTINES BELOW
  298.         POLL:run(event,arg,family)
  299.         if KB_EVENTS then kb:ProcessEvent(event, arg, family) end
  300.         if LHC_EVENTS then lhc:ProcessEvent(event, arg, family) end
  301.         _GetRunningTime = GetRunningTime()
  302.         _OnEvent(event, arg, family)  -- Runs myOnEvent to compartmentalize
  303.     end
  304.     if KB_EVENTS then kb:CleanupAfterEvent() end
  305.     if LHC_EVENTS then lhc:CleanupAfterEvent() end
  306. end
  307.  
  308. function log(...)
  309.     if ... == nil then
  310.         error("log(...) cannot display nil\n",2)
  311.     end
  312.     OutputLogMessage(...)
  313. end
  314.  
  315. function Type(v)
  316.     if type(v) == "table" then
  317.         if v.Type ~= nil then
  318.             return v.Type
  319.         elseif v.type ~= nil then
  320.             return v.type
  321.         end
  322.     end
  323.     return type(v)
  324. end
  325.  
  326. function typeString(s, delay)
  327.     if s == nil then return end
  328.  
  329.     if not original then
  330.         original = {}; originalShift = {}; baseKey = {}
  331.         original[1]      = { "`",     "-",     "=",     "[",        "]",        "\\",        ";",         "'",     ",",     ".",      "/",     " "        }
  332.         originalShift[1] = { "~",     "_",     "+",     "{",        "}",        "|",         ":",         '"',     "<",     ">",      "?"                 }
  333.         baseKey[1]       = { "tilde", "minus", "equal", "lbracket", "rbracket", "backslash", "semicolon", "quote", "comma", "period", "slash", "spacebar" }
  334.         original[2]      = {}
  335.         originalShift[2] = { "!", "@", "#", "$", "%", "^", "&", "*", "(", ")" }
  336.         baseKey[2]       = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" }
  337.         originalShift[3] = {}
  338.         original[3]      = { "\\",        "\n",    "\r",    "\t",  "\a",   "\c",    "\s"     }
  339.         baseKey[3]       = { "backslash", "enter", "enter", "tab", "lalt", "lctrl", "lshift" }
  340.     end
  341.    
  342.     for i=1, # s, 1 do
  343.         local char = string.sub(s,i,i)
  344.         local char2 = string.sub(s,i,i+1)
  345.  
  346.         if string.find(char,"%d") then                              -- if char is a digit
  347.             PressAndReleaseKey(char)
  348.             if delay then Sleep(delay) end
  349.         elseif string.find(char,"%a") then                          -- if char is letter
  350.             local lchar = string.lower(char)
  351.             if char ~= lchar then
  352.                 PressKey("lshift")
  353.                 if delay then Sleep(delay) end
  354.             end
  355.             PressAndReleaseKey(lchar)
  356.             if delay then Sleep(delay) end
  357.             if char ~= lchar then ReleaseKey("lshift") end
  358.         else                                                        -- if it's not a number or letter
  359.             local match = false
  360.             for index, value in ipairs(original) do
  361.                 for j, v in ipairs(original[index]) do              -- check all enties in original
  362.                     if char == v or char2 == v then                 -- found a match
  363.                         PressAndReleaseKey( baseKey[index][j] )     -- press and release base key
  364.                         match = true                                -- flag that a match was found
  365.                         break
  366.                     end
  367.                 end
  368.                 if match then break end
  369.             end
  370.             if not match then                                       -- if a match wasn't found, check for the originalShift values
  371.                 for index, value in ipairs(originalShift) do
  372.                     for j, v in ipairs(originalShift[index]) do     -- check all enties in originalShift
  373.                         if char == v or char2 == v then             -- found a match
  374.                             PressKey("lshift");
  375.                             if delay then Sleep(delay) end
  376.                             PressAndReleaseKey( baseKey[index][j] )
  377.                             ReleaseKey("lshift")
  378.                             match = true                            -- flag that a match was found
  379.                             break
  380.                         end
  381.                     end
  382.                     if match then break end
  383.                 end            
  384.             end
  385.  
  386.             if delay then Sleep(delay) end
  387.             if not match then
  388.                 error("function typeString(s,delay) - '"..char.."' or '"..char2.."' not recognized.\n", 2)
  389.             end
  390.         end
  391.     end
  392. end
  393.  
  394. ------------------------------------------------------------------------------------------------------------
  395. -- function Poll:new(family, delay)         delay is sleep time between polls
  396. -- function Poll:run(event, arg, family)    handles events and performs the polling as long as it's running
  397. -- function Poll:setMKeyModifier( m1, m2, m3, family )  Allows the use of Modifiers to change M pages
  398. -- function Poll:start()                    turn on polling
  399. -- function Poll:stop()                     stop polling
  400. --      function Poll:_press()                   NOT TO BE USED
  401. --      function Poll:release()                 NOT TO BE USED
  402. --      function Poll:setMKeyState(MKeyState)   NOT TO BE USED
  403. ------------------------------------------------------------------------------------------------------------
  404.  
  405. Poll = {}
  406. Poll.__index = Poll
  407. Poll.__newindex = function(table, key, value)
  408.     for k,v in pairs(table) do
  409.         if k == key then
  410.             rawset(table,key,value) return
  411.         end
  412.     end
  413.     error("'" .. key .. "' is not a member of Poll.", 2)
  414. end
  415.  
  416. function Poll:new(family, delay)
  417.     local self = {}
  418.  
  419.     self.delay = delay or 50
  420.     if family ~= "kb" and family ~= "lhc" then
  421.         error("Poll:new() - 'pFamily' must be 'kb' or 'lhc'", 2)
  422.     end
  423.     self.pFamily = family
  424.     self.presses = 0
  425.     self.running = false
  426.     self.runMKeyModifier = false
  427.     self.MKeyModifier = { nil, nil, nil }   -- "default" or Modifiers. i.e. "lshift", "ctrl"
  428.     self.modFamily = ""
  429.     self.Type = "Poll"
  430.  
  431.     setmetatable(self, Poll)
  432.  
  433.     return self
  434. end
  435.  
  436. --------------------------------------------------------------------------------------------
  437. --  Poll:setMKeyModifier( m1, m2, m3, family )
  438. --  Sets what modifiers will change you to the associated M page of the device on family
  439. --  "shift", "ctrl", "alt", "lshift", "lctrl", "lalt"... are all modifiers
  440. --  "default" is the page that will be set when none of the listed modifiers are pressed
  441. --  "" will cause a page to be ignored.
  442. --  I.E. - Poll:setMKeyModifier( "default", "shift", "ctrl", "lhc" )
  443. --  will set the default page to m1, shift will change you to m2, ctrl to m3 on a left hand controler
  444. --------------------------------------------------------------------------------------------
  445. function Poll:setMKeyModifier( m1, m2, m3, family )
  446.     self.runMKeyModifier = true
  447.     self.MKeyModifier[1] = m1
  448.     self.MKeyModifier[2] = m2
  449.     self.MKeyModifier[3] = m3
  450.     if family ~= "kb" and family ~= "lhc" then  error("Poll:setMKeyModifier() family - needs to be 'kb' or 'lhc'",2) end
  451.     self.modFamily = family
  452. end
  453.  
  454. function Poll:start()
  455.     self.running = true
  456.     self:setMKeyState()
  457. end
  458.  
  459. function Poll:stop()
  460.     self.running = false
  461. end
  462.  
  463. function Poll:_press()
  464.     if self.running then
  465.         self.presses = self.presses + 1
  466.     end
  467. end
  468.  
  469. function Poll:release()
  470.     if self.running then
  471.         self.presses = self.presses - 1
  472.     end
  473. end
  474.  
  475. function Poll:setMKeyState()
  476.     if self.runMKeyModifier then
  477.         local default = nil
  478.         local i = 1
  479.         local modMKeyState = nil
  480.  
  481.         for i = 1, 3, 1 do
  482.             if self.MKeyModifier[i] == "default" then
  483.                 default = i
  484.             elseif self.MKeyModifier[i] == "" then
  485.                 -- do nothing
  486.             elseif string.find(self.MKeyModifier[i],"mb%d") then
  487.                 local iMB = tonumber(string.sub(self.MKeyModifier[i],3))
  488.                 if IsMouseButtonPressed(iMB) then
  489.                     modMKeyState = i
  490.                 end
  491.             elseif IsModifierPressed(self.MKeyModifier[i]) then
  492.                 modMKeyState = i
  493.             end
  494.         end
  495.         if modMKeyState == nil then
  496.             modMKeyState = default
  497.         end
  498.         if modMKeyState ~= nil then
  499.             if self.pFamily == self.modFamily then
  500.                 SetMKeyState(modMKeyState, self.pFamily)
  501.                 return
  502.             elseif GetMKeyState(self.modFamily) ~= modMKeyState then
  503.                 SetMKeyState(modMKeyState, self.modFamily)
  504.             end
  505.         end
  506.     end
  507.     SetMKeyState( GetMKeyState(self.pFamily), self.pFamily )
  508. end
  509.  
  510. function Poll:run(event, arg, family)
  511.     if self.running and self.pFamily == family then
  512.         if event == "M_PRESSED" then
  513.             self:_press()
  514.         elseif event == "M_RELEASED" then
  515.             if self.presses == 1 then
  516.                 self:setMKeyState()
  517.                 Sleep(self.delay)
  518.             end
  519.             self:release()
  520.         end
  521.     end
  522. end
  523.  
  524. --------------------------------------------------------------------------------------------------------
  525. -- function CoThread:new(f)      f - function(coThread,...)   coThread is the controlling CoThread
  526. -- function CoThread:recreate()  kill existing coroutine and recreate
  527. -- function CoThread:create()    creates coroutine
  528. -- function CoThread:isDead()    true if coroutine is dead or nil/false
  529. -- function CoThread:run(...)    run coroutine with variables don't include mt
  530. -- function CoThread:kill()      kill coroutine
  531. -- function CoThread:yield()     use in function(coThread,...) to yield
  532. -- function CoThread:sleep(Milliseconds) use in function(coThread,...) to sleep for Milliseconds
  533. -- function CoThread:start()     does the same thing as recreate()
  534. -- function CoThread:stop()      does the same thing as kill()
  535. -- function CoThread:isFinished() same as isDead()
  536. --------------------------------------------------------------------------------------------------------
  537.  
  538. CoThread = {}
  539. CoThread.__index = CoThread
  540.  
  541. function CoThread:new(f) -- f - function(MultiThread)
  542.     local self = {}
  543.  
  544.     if type(f) ~= "function" then
  545.         error("A function is needed",2)
  546.     end
  547.     self.func = f
  548.     self.co = nil
  549.     self.sleepUntilTimeStamp = nil
  550.     self.Type = "CoThread"
  551.  
  552.     setmetatable(self, CoThread)
  553.     return self
  554. end
  555.  
  556. function CoThread:recreate()
  557.     self:kill()
  558.     self.co = coroutine.create(self.func)
  559. end
  560.  
  561. function CoThread:create()
  562.     self.co = coroutine.create(self.func)
  563. end
  564.  
  565. function CoThread:isDead()
  566.     if self.co == nil then
  567.         return true
  568.     elseif coroutine.status(self.co) == "dead" then
  569.         self.co = nil
  570.         return true
  571.     else
  572.         return false
  573.     end
  574. end
  575.  
  576. function CoThread:run(...)
  577.     if self:isDead() then
  578.         return
  579.     end
  580.     if self.sleepUntilTimeStamp ~= nil then
  581.         if _GetRunningTime < self.sleepUntilTimeStamp then
  582.             return
  583.         else
  584.             self.sleepUntilTimeStamp = nil
  585.         end
  586.     end
  587.     coroutine.resume(self.co, self, ...)
  588. end
  589.  
  590. function CoThread:kill()
  591.     self.co = nil       -- lua automatically deallocated
  592.     self.sleepUntilTimeStamp = nil
  593. end
  594.  
  595. function CoThread:yield()
  596.     coroutine.yield()
  597. end
  598.  
  599. function CoThread:sleep(Milliseconds)
  600.     self.sleepUntilTimeStamp = _GetRunningTime + Milliseconds
  601.  
  602.     coroutine.yield()
  603. end
  604.  
  605. function CoThread:start()    
  606.     self:recreate()
  607. end
  608.  
  609. function CoThread:stop()
  610.     self:kill()
  611. end
  612.  
  613. function CoThread:isFinished()
  614.     return self:isDead()
  615. end
  616.  
  617. --------------------------------------------------------------------------------------------------------
  618. -- function EventHandler:new(family)        Created an event handler for events of family ("lhc" or "kb")
  619. -- function EventHandler:ProcessEvent(event, arg, family)  This will add events to the handler.
  620. -- function EventHandler:CleanUpEvent()     This will clean up events that only happen once.
  621.  
  622. -- To find out if an event or state is present, access it by EventHandler["name of event"]
  623. -- Events are of the following catagories: "g" - G key, "m" - M key, "mb" - mouse Button
  624. -- The event catagory is followed by a number for the button pressed (i.e. "G1", "M2", "B4")
  625. -- The event catagory and button number is followed by one of the following:
  626. -- _pressed, _released, _down, _toggle, _m = mkey state
  627. -- A completed event will look like g1m2_toggle or g3_down to signify that G1 is pressed on m2, or G3 is down
  628. -- under construction Modifiers are also available as "shift", "alt", "ctrl" and right and left alternatives "lshift" or "rshift"
  629. --------------------------------------------------------------------------------------------------------
  630.  
  631. EventHandler = {}
  632. EventHandler.__index = EventHandler
  633.  
  634. function EventHandler:new(family)
  635.     local self = {}
  636.  
  637.     self.family = family
  638.     self.Type = "EventHandler"
  639.  
  640.     setmetatable(self, EventHandler)
  641.     return self
  642. end
  643.  
  644. function EventHandler:ProcessEvent(event, arg, family)
  645.  
  646.     -- Create events for modifiers
  647.     local modKeys = { "shift", "rshift", "lshift", "ctrl", "rctrl", "lctrl", "alt", "ralt", "lalt" }
  648.  
  649.     for k,v in pairs(modKeys) do
  650.         if IsModifierPressed(v) then                -- modifier is down
  651.             if not self[v .. "_down"] then      -- if just started being pressed, create pressed and down event
  652.                 self[v .. "_pressed"] = true
  653.                 self[v .. "_down"] = true
  654.                 if self[v .. "_toggle"] then
  655.                     self[v .. "_toggle"] = nil
  656.                 else
  657.                     self[v .. "_toggle"] = true
  658.                 end
  659.             end
  660.         else    -- modifer is not down
  661.             if self[v .. "_down"] then
  662.                 self[v .. "_down"] = nil
  663.                 self[v .. "_released"] = true
  664.             end
  665.         end
  666.     end
  667.  
  668.     -- Create events for mouse buttons
  669.     local mouseButtons = { "mb1", "mb2", "mb3", "mb4", "mb5" }
  670.  
  671.     for i,v in ipairs(mouseButtons) do
  672.         if IsMouseButtonPressed(i) then             -- Checks if mouse button is down.  i = index, so mouseButtons must be in order from 1
  673.             if not self[v .. "_down"] then      -- if just started being pressed, create pressed and down event
  674.                 self[v .. "_pressed"] = true
  675.                 self[v .. "_down"] = true
  676.                 if self[v .. "_toggle"] then
  677.                     self[v .. "_toggle"] = nil
  678.                 else
  679.                     self[v .. "_toggle"] = true
  680.                 end
  681.             end
  682.         else    -- mouseButton is not down
  683.             if self[v .. "_down"] then
  684.                 self[v .. "_down"] = nil
  685.                 self[v .. "_released"] = true
  686.             end
  687.         end
  688.     end
  689.  
  690.     -- Set MKeyState flag
  691.     local curMState = GetMKeyState(self.family)
  692.     local strMKeyState = "m" .. curMState
  693.  
  694.     self[strMKeyState .. "_m"] = true
  695.  
  696.     if family ~= self.family then
  697.         return
  698.     end
  699.  
  700.     -- remove MState events not on current MState
  701.     for k, v in pairs(self) do
  702.         local i, j = string.find(k, "m%d")
  703.         if i ~= nil then
  704.             if string.sub(k,i,j) ~= strMKeyState then
  705.                 if string.find(k,"g%dm%d_down") then  -- create a released event for GKey releases on a different M page
  706.                     self[string.gsub(k,"_down","_released")] = true
  707.                 end
  708.                 if not string.find(k, "g%dm%d_toggle") then
  709.                     self[k] = nil
  710.                 end
  711.             end
  712.         end
  713.     end
  714.  
  715.     local keyType = nil
  716.     local keyNum = nil
  717.     local eventType = {}
  718.  
  719.     -- create G and M events.
  720.     if string.sub(event,1,1) == "G" or string.sub(event,1,1) == "M" then
  721.         keyType = string.lower(string.sub(event,1,1))
  722.         keyNum = arg
  723.  
  724.         if string.find(event, "PRESSED") then
  725.             eventType = { "pressed", "down", "toggle" }
  726.         elseif string.find(event, "RELEASED") then
  727.             eventType = { "released" }
  728.         else
  729.             error("EventHandler:GetEventString(event, arg, family) - Not a recognized G or M event.\n",2)
  730.         end
  731.  
  732.         for i,v in ipairs(eventType) do
  733.             local keys = {}
  734.             keys[1] = keyType .. tostring(arg) .. "_" .. v
  735.             if keyType == "g" then
  736.                 keys[2] = keyType .. tostring(arg) .. strMKeyState .. "_" .. v
  737.             end
  738.  
  739.             if v == "toggle" then
  740.                 for k,val in pairs(keys) do
  741.                     if self[val] then
  742.                         self[val] = nil
  743.                     else
  744.                         self[val] = true
  745.                     end
  746.                 end              
  747.             else
  748.                 for k, val in pairs(keys) do
  749.                     self[val] = true
  750.                     if v == "released" then -- if released, remove the _down event
  751.                         self[string.gsub(val, "released", "down")] = nil
  752.                     end
  753.                 end
  754.             end
  755.         end
  756.        
  757.     elseif string.find(event,"PROFILE") then
  758.         eventType = string.sub(event,9)
  759.         self[eventType] = true
  760.     else
  761.         error("EventHandler:GetEventString(event, arg, family) - Not a recognized event.\n",2)
  762.     end
  763. end
  764.  
  765. function EventHandler:CleanupAfterEvent()
  766.     -- Clean up events like releases and presses to be removed now they've been used already
  767.     for k, v in pairs(self) do
  768.         if k == "ACTIVATED" or k == "DEACTIVATED" then
  769.             self[k] = nil
  770.         elseif string.find(k, "_pressed") then
  771.             self[k] = nil
  772.         elseif string.find(k, "_released") then
  773.             self[string.gsub(k,"_released","_down")] = nil
  774.             self[k] = nil
  775.         end
  776.     end
  777. end
  778.  
  779. -- EOF
Advertisement
Add Comment
Please, Sign In to add comment