Advertisement
Forthcode

G13 Mouse M script by: UPIA -- Toggle edit by: Airwolf

Jul 9th, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- gL Project revision 2.0 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --
  2. ClearLog(); gL={};gL.com={};gL.mode={};gL.SET={};gL.device={};gL.poll={};gL.KEY={} --
  3. -- G-Series Lua Script Template by UPIA ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --
  4. -- Toggle revision by Airwolf ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --
  5.  
  6. -- SETUP ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7.  
  8.                         -- Keyboard Setup
  9. gL.device[0]            = { "mml",  5,  6,  3   }       -- regular keyboard and mouse (default: 5,6,3 / 0 = off)
  10. gL.device[1]            = { "lhc",  29, 3       }       -- G13:     left-hand-controller = "lhc", 29 G-keys, 3 M-keys
  11. gL.device[2]            = { "kb",   18, 3       }       -- G15:     keyoard = "kb", 18 G-keys, 3 M-keys
  12.  
  13.                         -- Poll / Loop Setup
  14. gL.poll.autostart       = 1                             -- Start Poll with Script
  15. gL.poll.grid            = 10                            -- ( min Interval        >=  10 ms )
  16. gL.poll.factor          = 2                             -- ( G-Key-Factor int    >=  1 )
  17. gL.poll.steps           = 0                             -- ( 0 = off, 1 = 1 ms )
  18.  
  19.                         -- Key Setup
  20. gL.KEY.fastclick        = 200                           -- Fast Click Time (for multi-click-selection)
  21.  
  22.                         -- Play Mode Setup
  23. gL.com.PlayMode         = "OnEvent"                         -- Play Mode ("OnEvent", "SET" or "Script")
  24.                         -- "SET" Mode Setup
  25. gL.KEY.showSET          = 0                             -- Log Assignment Slots
  26.                         -- "OnEvent" Mode Setup
  27. gL.com.OnEventDown      = 1                             -- Play "Down" Event
  28.  
  29. -- /SETUP ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  30.  
  31.  
  32. -- Example for PlayMode "OnEvent" : (normal scripting, additional Keys (in poll): MB, MO, LO, additional event (in poll): _DOWN )
  33.  
  34. gL.com.OnEvent = function(event, arg, family)
  35.     family = family or "" -- family NOT nil
  36.     --OutputLogMessage("event = %s, arg = %s, family = %s\n", event, arg, family) -- log events
  37.     -- Profile Activation
  38.     if event        == "PROFILE_ACTIVATED" then OutputLogMessage("Profile activated in OnEvent Mode\n") end
  39.     -- G-Keys (example)
  40.     -- if event        == "G_PRESSED" and arg == 1 then gL.poll.pause()
  41.  
  42.     -- Mouse Buttons
  43.     if event        == "MB_PRESSED" and arg == 4 then
  44.     Mstate = not Mstate
  45.         if Mstate then
  46.             SetMKeyState(2,"lhc")  -- if MB4 pressed => Set G13 to M2
  47.         else
  48.             SetMKeyState(1,"lhc") -- if MB4 pressed => Set G13 to M1
  49.     end
  50.  
  51.     elseif event    == "MB_PRESSED" and arg == 5 then
  52.     Mstate = not Mstate
  53.         if Mstate then
  54.             SetMKeyState(3,"lhc")  -- if MB5 pressed => Set G13 to M3
  55.         else
  56.             SetMKeyState(1,"lhc") -- if MB5 pressed => Set G13 to M1
  57.     end
  58.     end
  59.  
  60.  
  61. end
  62.  
  63. -- Example for PlayMode "Script" : Using gL Key State Syntax gL.KEY.kb/lhc/mml.G/M/MB/MO/LO["name"].s=0/1/2/3=off/press/down/release
  64.  
  65. gL.com.Script = function(pEvent)
  66.     -- Profile Activation
  67.     if pEvent == "PROFILE_ACTIVATED" then OutputLogMessage("Profile activated in Script Mode\n") end
  68.     -- G1: Start / Stop Polling
  69.     if gL.KEY.lhc.G[1].s == 1 then gL.poll.pause() end
  70.  
  71. end
  72.  
  73. -- Example for PlayMode "SET" : (Prototype) Play mode-defined key assignments gL.SET.kb/lhc/mml.M[0/1/2/3].MB[1].s[1/2/3]) M[0] => all M-Modes!
  74.  
  75. gL.mode.default = function ()
  76.     -- Profile Activation
  77.     gL.SET.PA                   = function()    OutputLogMessage("Profile activated in SET Mode\n") end
  78.     -- G1: Start / Stop Polling
  79.     gL.SET.lhc.M[0].G[1].s[1]   = function()    gL.poll.pause() end
  80.  
  81.  
  82. end
  83.  
  84.  
  85. -- More User Actions can simply be added to these special events:
  86.  
  87. gL.com.gmEvents = function()        -- play on every GM event
  88.     -- user define
  89. end
  90.  
  91. gL.com.pollEvents = function()      -- play on every poll event
  92.     -- user define
  93. end
  94.  
  95. gL.com.gridEvents = function()      -- play on every grid event
  96.     -- user define
  97. end
  98.  
  99. gL.com.stepEvents = function()      -- play on every step event (ms)
  100.     -- user define
  101. end
  102.  
  103. gL.com.allkeysReleased = function() -- POLL ONLY! play, if all keys are released
  104.     -- user define
  105.     --OutputLogMessage("All Keys are released!\n")
  106. end
  107.  
  108.  
  109. function gL.KEY.defaultSetup()      -- Define the Keys you want to use in gL
  110.     -- ADD THE KEYS YOU WANT TO USE
  111.     gL.KEY.setup.mml.MB     = {1,2,3,4,5}
  112.     gL.KEY.setup.mml.MO     = {"lalt", "lctrl", "lshift", "ralt", "rctrl", "rshift"}
  113.     gL.KEY.setup.mml.LO     = {"capslock", "numlock", "scrolllock"}
  114.     gL.KEY.setup.kb.G       = {1,2,3,4,5,6,7,8,9,10,11,12}
  115.     gL.KEY.setup.kb.M       = {1,2,3}
  116.     gL.KEY.setup.lhc.G      = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29}
  117.     gL.KEY.setup.lhc.M      = {1,2,3}
  118.     gL.com.setupkeys()      -- setup the keys
  119.  
  120.     -- ADD THE DETAILED KEYS YOU WANT TO USE
  121.     gL.KEY.setdetail( "mml",    "MB",   {1,2,3,4,5} )
  122.     gL.KEY.setdetail( "mml",    "MO",   {"lalt", "lctrl", "lshift", "ralt", "rctrl", "rshift"} )
  123.     gL.KEY.setdetail( "kb",     "G",    {1,2,3,4,5,6,7,8,9,10,11,12} )
  124.     gL.KEY.setdetail( "kb",     "M",    {1,2,3} )
  125.     gL.KEY.setdetail( "lhc",    "G",    {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29} )
  126.     gL.KEY.setdetail( "lhc",    "M",    {1,2,3} )
  127. end
  128.  
  129. -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  130. -- ~~ STOP EDITING HERE !!! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  131. -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gL Functions from here ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  132.  
  133. -- Main Handler Functions ...
  134.  
  135. function OnEvent(event,arg,family)
  136.     gL.com.ini(event)                       -- initialize gL
  137.     gL.com.setEvents(event,arg,family)      -- set event values
  138.     gL.com.setMmodes()                      -- set/update m-modes
  139.     gL.com.filterPoll()                     -- set/filter Poll events
  140.     --gL.com.showLog()                        -- print events to log
  141.     gL.com.catchGM()                        -- catch GM events 1+3
  142.     gL.com.catchP()                         -- catch PROFILE events
  143.     gL.com.handler()                        -- play main handler (mml-events, step events ...)
  144. end
  145.  
  146. gL.com.handler = function ()
  147.     if gL.poll.on then        -- Poll is on
  148.         gL.poll.onLast = true
  149.         if  gL.com.E == "Poll" then        
  150.             local grid; local step
  151.             gL.com.pollEvents()             -- play Poll events
  152.             --
  153.             for grid = 1,gL.poll.factor,1 do
  154.                 -- Grid
  155.                 if gL.poll.on then gL.com.ComGridEvents() end        -- play grid events (MM, rMacro, Mode.Grid, ...)
  156.                 if gL.poll.steps > 0 then
  157.                     for step = 1,gL.poll.grid/gL.poll.steps,1 do
  158.                         Sleep(gL.poll.steps)
  159.                         gL.com.time = gL.com.time + gL.poll.steps
  160.                         gL.com.ComStepEvents() -- play step events (e.g. millisecond macros)
  161.                     end
  162.                     Sleep(gL.poll.steps)
  163.                     gL.com.time = gL.com.time + gL.poll.steps
  164.                 else
  165.                     Sleep(gL.poll.grid)
  166.                     gL.com.time = gL.com.time + gL.poll.grid
  167.                 end
  168.             end
  169.             gL.com.SMKS(gL.com.M2, gL.com.family2)
  170.         end
  171.     end
  172. end
  173.  
  174. gL.com.ComGridEvents = function()      -- play assignments on grid events ----------------namechange!!!!!!!!!!
  175.     -- Mouse&Modifiers, Record Macros ...
  176.     gL.KEY.checkGM()                -- Handle G,M Events in the loop (details, time ...)
  177.     gL.KEY.catchMML()               -- Handle MB,MO,LO Events
  178.     gL.com.PlayEvents()             -- Play all Events
  179.     gL.com.gridEvents()             -- Play user defined
  180. end
  181.  
  182. gL.com.ComStepEvents = function()      -- play assignments on step events (ms)
  183.     gL.com.stepEvents()             -- play user defined
  184. end
  185.  
  186. -- Poll Handling ...
  187.  
  188. gL.com.filterPoll = function() -- Analyze M action on Poll Device
  189.     if table.getn(gL.device)==2 then
  190.         if gL.poll.on and gL.com.F == gL.com.family2 and (gL.com.E == "M_PRESSED" or gL.com.E == "M_RELEASED") then
  191.             gL.com.M2d = gL.com.A
  192.             if gL.com.M2d == gL.com.M2 and gL.com.M2d == gL.com.M2l then             -- Case 1: all the same
  193.                 if gL.com.E == "M_PRESSED" and gL.com.Er == "M_PRESSED" then        -- Manual Press of the same M-Mode Button as M-Mode running
  194.                     gL.com.Er = gL.com.E; gL.com.Ep = "Poll0"; gL.com.Es = gL.com.E
  195.                 elseif gL.com.E == "M_RELEASED" and gL.com.Er == "M_RELEASED" then  -- Manual Release same M-Mode
  196.                     gL.com.Er = gL.com.E; gL.com.Ep = "none"; gL.com.Es = gL.com.E
  197.                 else                                                                -- Regular Polling
  198.                     if gL.com.E == "M_PRESSED" then
  199.                         gL.com.Ep = "Poll0"; gL.com.Es = "Poll0"
  200.                         if gL.com.SMKS_set ~= 0 then                                -- SetMKeyState (same State press)
  201.                             gL.com.Ep = "none"; gL.com.Es = gL.com.E
  202.                         end
  203.                     else
  204.                         gL.com.Ep = "Poll"; gL.com.Es = "Poll"
  205.                         if gL.com.SMKS_set ~= 0 then                                -- SetMKeyState (same State release)
  206.                             gL.com.Ep = "none"; gL.com.Es = gL.com.E
  207.                             gL.com.SMKS_set = 0
  208.                         end
  209.                     end
  210.                     gL.com.Er = gL.com.E
  211.                 end
  212.             elseif gL.com.M2d ~= gL.com.M2 and gL.com.M2 == gL.com.M2l then         -- Case 2: Manual P/R on different M-Mode => Changing
  213.                 if gL.com.E == "M_PRESSED" then
  214.                     gL.com.Er = gL.com.E; gL.com.Ep = "none"; gL.com.Es = gL.com.E
  215.                     gL.com.PC = 1; gL.com.M2 = gL.com.A; gL.com.M[gL.com.family2]=gL.com.M2
  216.                 else gL.ERROR("unknown state 1") -- XXX
  217.                 end
  218.             elseif gL.com.M2d ~= gL.com.M2 and gL.com.M2d == gL.com.M2l then         -- Case 3: Delayed Poll (polling with old M-State) ADD M CHANGE for M.x??????????
  219.                 if gL.com.E == "M_PRESSED" then gL.com.Ep = "Poll0"; gL.com.Es = "Poll0" else gL.com.Ep = "Poll"; gL.com.Es = "Poll"; gL.com.PD = gL.com.PD + 1 end
  220.                 gL.com.Er = gL.com.E
  221.             elseif gL.com.M2d == gL.com.M2 and gL.com.M2d ~= gL.com.M2l then         -- Case 4:
  222.                 if gL.com.E == "M_RELEASED" and gL.com.Er == "M_RELEASED" then      -- Manual different M-Mode Release
  223.                     gL.com.Er = gL.com.E; gL.com.Ep = "none"; gL.com.Es = gL.com.E
  224.                     gL.com.PC = 2
  225.                 elseif gL.com.E == "M_RELEASED" and gL.com.Es == "M_PRESSED" then   -- Quick different Release / or SetMKeyState()
  226.                     gL.com.Er = gL.com.E; gL.com.Ep = "none"; gL.com.Es = gL.com.E
  227.                     gL.com.PC = 2
  228.                 else                                                                -- Waiting Poll (waiting for Release of M-Button)
  229.                     if gL.com.E == "M_PRESSED" then gL.com.Ep = "Poll0"; gL.com.Es = "Poll0" else gL.com.Ep = "Poll"; gL.com.Es = "Poll" end
  230.                     gL.com.Er = gL.com.E
  231.                 end
  232.             else
  233.                 OutputLogMessage("ERROR: undef state 2\n\n")-- XXX
  234.             end
  235.             if gL.com.PC == 2 and gL.com.E == "M_RELEASED" then                     -- Check if Poll Change is complete and set M2l = M2s
  236.                 if gL.com.PD < 3 then -- wait 1 more poll (max. 2 delayed polls) .. make this variable (10ms => 3 polls, 25ms => 2polls)??
  237.                     gL.com.PD = gL.com.PD + 1
  238.                 elseif gL.com.PD >= 3 then -- PollChange complete
  239.                     gL.com.PC = 0; gL.com.PD = 0
  240.                     gL.com.M2l = gL.com.M2
  241.                 end
  242.             end
  243.             gL.com.E = gL.com.Es                                                    -- Change event !!!
  244.         end
  245.     elseif table.getn(gL.device) == 1 then -- Polling with fake device
  246.         if gL.poll.on and gL.com.F == gL.com.family2 then
  247.             if gL.com.E == "M_PRESSED" then gL.com.Es = "Poll0"
  248.             elseif gL.com.E == "M_RELEASED" then gL.com.Es = "Poll"
  249.             end
  250.             gL.com.E = gL.com.Es                                                    -- Change event !!!
  251.         end
  252.     end
  253.     -- Eliminate late Polls when Poll is off and reset PST and poll vars ..
  254.     if gL.com.PST ~= 0 and gL.poll.on == false and gL.poll.onLast then
  255.         if gL.com.F == gL.com.family2 and (gL.com.E == "M_PRESSED" or gL.com.E == "M_RELEASED") then
  256.             if GetRunningTime() - gL.com.PST < (4 * gL.poll.factor * gL.poll.grid) then
  257.                 gL.com.E = "ignore"
  258.             end
  259.         end
  260.         if GetRunningTime() - gL.com.PST > (4 * gL.poll.factor * gL.poll.grid) then -- add the reset on any event ?????????????????????
  261.             gL.com.PST = 0; gL.poll.onLast = false
  262.         end
  263.     end
  264. end
  265.  
  266. gL.poll.start = function ()
  267.     gL.com.setTime()
  268.     gL.poll.on = true
  269.     gL.poll.onLast = true
  270.     OutputLogMessage("--> Poll Start!!!\n")
  271.     gL.com.SMKS(gL.com.M2d, gL.com.family2)
  272. end
  273.  
  274. gL.poll.stop = function ()
  275.     gL.com.PST = GetRunningTime()
  276.     gL.poll.on = false
  277.     gL.KEY.delActive()
  278.     OutputLogMessage("--> Poll Stop!!!\n")
  279. end
  280.  
  281. gL.poll.pause = function ()
  282.     if gL.poll.on then gL.poll.stop() else gL.poll.start() end
  283. end
  284.  
  285. -- Key Status Functions ...
  286.  
  287. gL.com.catchP= function()  -- Catch PROFILE events
  288.     if gL.com.E == "PROFILE_ACTIVATED" or gL.com.E == "PROFILE_DEACTIVATED" then
  289.         if gL.com.PlayMode == "OnEvent" then
  290.             gL.com.OnEvent(gL.com.E,0) ---------------,"") ?????????????
  291.         elseif gL.com.PlayMode == "SET" then
  292.             local e
  293.             if gL.com.E == "PROFILE_ACTIVATED" then e = "PA" elseif gL.com.E == "PROFILE_DEACTIVATED" then e = "PD" end
  294.             if type(gL.SET[e]) == "function" then gL.SET[e]() end
  295.         elseif gL.com.PlayMode == "Script" then
  296.             gL.com.Script(gL.com.E)
  297.         end
  298.     end
  299. end
  300.  
  301. function gL.KEY.checkGM() -- update G and M Keys on devices
  302.     local device
  303.     if gL.poll.on then
  304.         for devices = 1,table.getn(gL.device),1 do
  305.             device = gL.device[devices][1]
  306.             for g,group in pairs({"G","M"}) do
  307.                 for n,name in pairs(gL.KEY.active[device][group]) do                                           -- Update active keys -->
  308.                     if gL.KEY[device][group][name].s == 1 then     -- if state is pressed, set to down
  309.                         gL.KEY[device][group][name].s = 2
  310.                         if gL.KEY[device][group][name].d == 1 then -- update details
  311.                             gL.KEY[device][group][name].tog = 0
  312.                             gL.KEY[device][group][name].time = gL.KEY[device][group][name].time + gL.poll.grid
  313.                             if gL.KEY[device][group][name].mcs ~= 0 then gL.KEY[device][group][name].mcs = 0 end -----------------?
  314.                         end
  315.                     elseif gL.KEY[device][group][name].s == 2 then -- if stae is on, update time
  316.                         if gL.KEY[device][group][name].d == 1 then gL.KEY[device][group][name].time = gL.KEY[device][group][name].time + gL.poll.grid end
  317.                     elseif gL.KEY[device][group][name].s == 3 then -- if state is released, set 0 and remove from active keys
  318.                         gL.KEY[device][group][name].s = 0
  319.                         if gL.KEY[device][group][name].d == 1 then gL.KEY[device][group][name].time = 0 end
  320.                         table.remove(gL.KEY.active[device][group],n) -- remove old releases
  321.                     elseif gL.KEY[device][group][name].s == -1 then                                             -- PLAY PRESSES
  322.                         gL.KEY[device][group][name].s = 1
  323.                         if gL.KEY[device][group][name].d == 1 then
  324.                             gL.KEY[device][group][name].togstat = gL.KEY.toggle(gL.KEY[device][group][name].togstat)
  325.                             gL.KEY[device][group][name].tog = gL.KEY[device][group][name].togstat                             -- set toggle
  326.                             if ( gL.com.time - gL.KEY[device][group][name].last_r ) > gL.KEY.fastclick then
  327.                                 gL.KEY[device][group][name].clicks = 0
  328.                             end                                                             -- sclick
  329.                             gL.KEY[device][group][name].last_p = gL.com.time
  330.                         end
  331.  
  332.                     elseif gL.KEY[device][group][name].s == -3 then
  333.                         gL.KEY[device][group][name].s = 3
  334.                         if gL.KEY[device][group][name].d == 1 then
  335.                             if ( gL.com.time - gL.KEY[device][group][name].last_p ) <= gL.KEY.fastclick then -- shortclick detection
  336.                                 gL.KEY[device][group][name].clicks = gL.KEY[device][group][name].clicks + 1
  337.                                 --OutputLogMessage("clicks MB "..name.." : "..gL.KEY.mml[group][name].clicks.."\n")
  338.                             else
  339.                                 gL.KEY[device][group][name].mcs = gL.KEY[device][group][name].clicks; gL.KEY[device][group][name].clicks=0
  340.                             end -- multiclick selection
  341.                             gL.KEY[device][group][name].last_r = gL.com.time
  342.                         end
  343.                     end
  344.                 end
  345.             end
  346.         end
  347.     end
  348. end
  349.  
  350. gL.com.catchGM = function() -- Catch G and M keys of the devices
  351.     if gL.com.E == "M_PRESSED" or gL.com.E == "M_RELEASED" or gL.com.E == "G_PRESSED" or gL.com.E == "G_RELEASED" then -- GM event
  352.         gL.com.gmEvents() -- play user defined
  353.         local name, group, device, s
  354.         name = gL.com.A
  355.         device = gL.com.F
  356.         if string.sub(gL.com.E,1,1) == "G" then group = "G" else group = "M" end
  357.         if string.sub(gL.com.E,3,3) == "P" then                                 -- PRESSED
  358.             table.insert(gL.KEY.active[device][group],name)
  359.             if gL.poll.on then
  360.                 s = -1  
  361.                 gL.KEY[device][group][name].s = s
  362.             else
  363.                 s = 1
  364.                 gL.KEY[device][group][name].s = s
  365.                 if gL.KEY[device][group][name].d == 1 then -- update details
  366.                     --
  367.                     gL.KEY[device][group][name].togstat = gL.KEY.toggle(gL.KEY[device][group][name].togstat)
  368.                     gL.KEY[device][group][name].tog = gL.KEY[device][group][name].togstat                             -- set toggle
  369.                     if ( gL.runtime() - gL.KEY[device][group][name].last_r ) > gL.KEY.fastclick then
  370.                         gL.KEY[device][group][name].clicks = 0
  371.                     end                                                             -- sclick
  372.                     gL.KEY[device][group][name].last_p = gL.runtime()
  373.                 end
  374.                 gL.com.PlayKey(device,group,name,s)
  375.             end
  376.         else                                                                        -- RELEASED
  377.             if gL.poll.on then
  378.                 if gL.KEY[device][group][name].s == -1 then -- check if key was pressed in the same grid, if so play press event
  379.                     gL.com.PlayKey(device,group,name,1)
  380.                 end
  381.                 s = -3
  382.                 gL.KEY[device][group][name].s = s
  383.             else
  384.                 s = 3
  385.                 gL.KEY[device][group][name].s = s
  386.  
  387.                 if gL.KEY[device][group][name].d == 1 then -- update details
  388.                     --
  389.                     if ( gL.runtime() - gL.KEY[device][group][name].last_p ) <= gL.KEY.fastclick then -- shortclick detection
  390.                         gL.KEY[device][group][name].clicks = gL.KEY[device][group][name].clicks + 1
  391.                     else
  392.                         gL.KEY[device][group][name].mcs = gL.KEY[device][group][name].clicks; gL.KEY[device][group][name].clicks=0
  393.                     end -- multiclick selection
  394.                     gL.KEY[device][group][name].last_r = gL.runtime()
  395.                 end
  396.                 gL.KEY[device][group][name].s = s
  397.                 gL.com.PlayKey(device,group,name,s) -- Play Key Release
  398.                 gL.KEY.removeActive(device,group,name) -- Remove from Active key-list
  399.                 s = 0   -- set state = 0
  400.                 gL.KEY[device][group][name].s = s
  401.             end
  402.         end
  403.     end
  404. end
  405.  
  406. function gL.KEY.catchMML() -- update Modifier and Mouse
  407.     for g,group in pairs({"MB","MO"}) do
  408.         for n,name in pairs(gL.KEY.active.mml[group]) do                                           -- Update active keys -->
  409.             if gL.KEY.mml[group][name].s == 1 then     -- if state is pressed, set to down
  410.                 gL.KEY.mml[group][name].s = 2
  411.                 if gL.KEY.mml[group][name].d == 1 then -- update details
  412.                     gL.KEY.mml[group][name].tog = 0
  413.                     gL.KEY.mml[group][name].time = gL.KEY.mml[group][name].time + gL.poll.grid
  414.                     if gL.KEY.mml[group][name].mcs ~= 0 then gL.KEY.mml[group][name].mcs = 0 end -----------------?
  415.                 end
  416.             elseif gL.KEY.mml[group][name].s == 2 then -- if stae is on, update time
  417.                 if gL.KEY.mml[group][name].d == 1 then gL.KEY.mml[group][name].time = gL.KEY.mml[group][name].time + gL.poll.grid end
  418.             elseif gL.KEY.mml[group][name].s == 3 then -- if state is released, set 0 and remove from active keys
  419.                 gL.KEY.mml[group][name].s = 0
  420.                 gL.KEY.mml[group][name].time = 0
  421.                 table.remove(gL.KEY.active.mml[group],n) -- remove old releases
  422.             end
  423.         end
  424.     end
  425.  
  426.     for v,i in pairs(gL.KEY.setup.mml.LO) do                                         -- set old presses, releases to down/off
  427.         if gL.KEY.mml.LO[i].s == 1 then -- change events 1 and 3
  428.             gL.KEY.mml.LO[i].s = 2
  429.             table.remove(gL.KEY.active.mml.LO,v)-- remove active
  430.         elseif gL.KEY.mml.LO[i].s == 3 then
  431.             gL.KEY.mml.LO[i].s = 0
  432.             table.remove(gL.KEY.active.mml.LO,v)-- remove active
  433.         end
  434.     end
  435.  
  436.     for g,group in pairs({"MB","MO"}) do
  437.         for n,name in pairs(gL.KEY.setup.mml[group]) do                                         -- Mouse Buttons and modifiers
  438.             if gL.KEY.check[group](name) then
  439.                 if gL.KEY.mml[group][name].s == 0 then -- pressed
  440.                     gL.KEY.mml[group][name].s = 1
  441.                     table.insert(gL.KEY.active.mml[group], name)
  442.                     if gL.KEY.mml[group][name].d == 1 then
  443.                         gL.KEY.mml[group][name].togstat = gL.KEY.toggle(gL.KEY.mml[group][name].togstat)
  444.                         gL.KEY.mml[group][name].tog = gL.KEY.mml[group][name].togstat                             -- set toggle
  445.                         if ( gL.com.time - gL.KEY.mml[group][name].last_r ) > gL.KEY.fastclick then
  446.                             gL.KEY.mml[group][name].clicks = 0
  447.                         end                                                             -- sclick
  448.                         gL.KEY.mml[group][name].last_p = gL.com.time
  449.                     end
  450.                 end
  451.             else
  452.                 if gL.KEY.mml[group][name].s > 0 then                                              -- released
  453.                     gL.KEY.mml[group][name].s = 3
  454.                     if gL.KEY.mml[group][name].d == 1 then
  455.                         if ( gL.com.time - gL.KEY.mml[group][name].last_p ) <= gL.KEY.fastclick then -- shortclick detection
  456.                             gL.KEY.mml[group][name].clicks = gL.KEY.mml[group][name].clicks + 1
  457.                         else
  458.                             gL.KEY.mml[group][name].mcs = gL.KEY.mml[group][name].clicks; gL.KEY.mml[group][name].clicks=0
  459.                         end -- multiclick selection
  460.                         gL.KEY.mml[group][name].last_r = gL.com.time
  461.                     end
  462.                 end
  463.             end
  464.         end
  465.     end
  466.     for v,i in pairs(gL.KEY.setup.mml.LO) do                                         -- New Lock Keys
  467.         if IsKeyLockOn(i) then
  468.             if gL.KEY.mml.LO[i].s == 0 then
  469.                 gL.KEY.mml.LO[i].s = 1
  470.                 table.insert(gL.KEY.active.mml.LO, i) -- set active
  471.             end
  472.         else
  473.             if gL.KEY.mml.LO[i].s == 1 or gL.KEY.mml.LO[i].s == 2 then
  474.                 gL.KEY.mml.LO[i].s = 3  
  475.                 table.insert(gL.KEY.active.mml.LO, i) -- set active
  476.             end
  477.         end
  478.     end
  479. end
  480.  
  481. function gL.KEY.removeActive(device,group,name) -- remove special key from active
  482.     local winner --= nil
  483.     for i,n in ipairs(gL.KEY.active[device][group]) do
  484.         if name == n then winner = i end
  485.     end
  486.     if winner ~= nil then table.remove(gL.KEY.active[device][group],winner) end
  487. end
  488.  
  489. function gL.KEY.delActive() -- delete all active keys
  490.     local device,group,name, i,g
  491.     for i,devices in pairs(gL.device) do
  492.         device = devices[1]
  493.         for g = 1,table.getn(gL.KEY.defaults[device]),1 do
  494.             group = gL.KEY.defaults[device][g]
  495.             gL.KEY.active[device][group] = {}
  496.         end
  497.     end
  498. end
  499.  
  500. function gL.KEY.emptyactive() -- initiate empty keys
  501.     gL.KEY.active           = {}
  502.     for i,device in pairs(gL.device) do
  503.         gL.KEY.active[device[1]] = {}
  504.         if i == 0 then
  505.             if device[2] > 0 then gL.KEY.active[device[1]].MB = {} end
  506.             if device[3] > 0 then gL.KEY.active[device[1]].MO = {} end
  507.             if device[4] > 0 then gL.KEY.active[device[1]].LO = {} end
  508.         else
  509.             if device[2] > 0 then gL.KEY.active[device[1]].G = {} end
  510.             if device[3] > 0 then gL.KEY.active[device[1]].M = {} end
  511.         end
  512.     end
  513. end
  514.  
  515. function gL.KEY.updateLO()
  516.     for i,name in pairs(gL.KEY.setup.mml.LO) do
  517.         --local name = gL.KEY.defaults.LO[i]
  518.         if IsKeyLockOn(name) then
  519.             gL.KEY.mml.LO[name].s = 2
  520.         else
  521.             gL.KEY.mml.LO[name].s = 0
  522.         end
  523.     end
  524. end
  525.  
  526. function gL.KEY.toggle(stat)
  527.     if stat == 1 then stat=2 else stat=1 end
  528.     return stat
  529. end
  530.  
  531. function gL.KEY.setdetail(device, group, array)
  532.     local winner = ""
  533.     for n1,name1 in pairs(gL.KEY.setup[device][group]) do
  534.         for n2,name2 in pairs(array) do
  535.             if name1 == name2 then winner = name1 end
  536.         end
  537.         if winner ~= "" then
  538.             gL.KEY[device][group][winner] = {s=0, d=1, tog=0, togstat=2, clicks=0, mcs=0, time=0, last_p=0, last_r=0}
  539.         else
  540.             gL.KEY[device][group][winner] = {s=0, d=0, tog=0, togstat=2, clicks=0, mcs=0, time=0, last_p=0, last_r=0}
  541.         end
  542.     end
  543. end
  544.  
  545. -- Com Functions ...
  546.  
  547. gL.com.ini = function (event) -- Set initial values
  548.     if event == "PROFILE_ACTIVATED" then
  549.         OutputLogMessage("gL Project running ..\n---\n")
  550.         -- Poll Vars
  551.         gL.com.time             = 0                 -- ini: time is 0
  552.         gL.poll.on              = false             -- ini: poll is off
  553.         gL.poll.onLast          = false             -- ni: last poll status is off
  554.         gL.poll.onNext          = false
  555.         -- Start-Parameter for "setpoll" with 2 devices present
  556.         gL.com.Ep               = "Poll"            -- Event poll
  557.         gL.com.Er               = "M_RELEASED"      -- Event real
  558.         gL.com.Es               = "Poll"            -- Event set
  559.         gL.com.PC               = 0                 -- Poll Change
  560.         gL.com.PD               = 0                 -- Poll Delayed
  561.         gL.com.PCM              = 0                 -- Poll Change Mode
  562.         gL.com.PST              = 0                 -- Poll Stop Time
  563.         gL.com.SMKS_set         = 0                 -- SetMKeyState detection
  564.         -- Mstate Array
  565.         gL.com.M                = {}                -- M["kb/lhc"]=1/2/3
  566.         gL.com.AKR              = 1
  567.         -- Key Vars
  568.         gL.KEY.setup            = {}
  569.         gL.KEY.setup.mml        = {}
  570.         gL.KEY.setup.kb         = {}
  571.         gL.KEY.setup.lhc        = {}
  572.         gL.KEY.defaults         = {}
  573.         gL.KEY.defaults.mml     = {"MB","MO","LO"}
  574.         gL.KEY.defaults.kb      = {"G","M"}
  575.         gL.KEY.defaults.lhc     = {"G","M"}
  576.         -- Key Check Functions
  577.         gL.KEY.check = {}
  578.         gL.KEY.check.MB = IsMouseButtonPressed
  579.         gL.KEY.check.MO = IsModifierPressed
  580.         gL.KEY.emptyactive()
  581.         --gL.com.setupkeys() --moved to mode-------------------------------- !!! keys need to be setup in default mode!
  582.         gL.KEY.defaultSetup()
  583.         -- Load User Device Settings
  584.         gL.com.setdevice()
  585.         -- Set initial M-States
  586.         if gL.com.M1 == nil then gL.com.M1 = GetMKeyState(gL.com.family1) end   -- set M-Mode for main device
  587.         if gL.com.M2d == nil then
  588.             gL.com.M2d = GetMKeyState(gL.com.family2)                            -- set M-Mode for Poll device
  589.             if gL.com.M2l == nil then gL.com.M2l = gL.com.M2d end            -- set last M-Modes for Poll device
  590.             if gL.com.M2 == nil then gL.com.M2 = gL.com.M2d end
  591.         end
  592.         gL.com.M["mml"]=gL.com.M1
  593.         gL.com.M[gL.com.family1]=gL.com.M1
  594.         gL.com.M[gL.com.family2]=gL.com.M2
  595.         -- Load default mode settings
  596.         if gL.com.PlayMode == "SET" then gL.mode.default() end
  597.         -- Autostart Poll
  598.         if gL.poll.autostart == 1 then gL.poll.start() end
  599.     end
  600.     -- ANY EVENT
  601.     gL.com.runtime              = nil               -- reset runtime var
  602.     --if gL.poll.on then gL.poll.onNext = true else gL.poll.onNext = false end    -- set poll change varXXXXXXXXXXXXX
  603. end
  604.  
  605. gL.com.setEvents = function(event,arg,family)   -- Set Event Variables sent to OnEvent()
  606.     gL.com.E = event
  607.     gL.com.A = arg
  608.     if family == "" then gL.com.F = "all" else gL.com.F = family end
  609. end
  610.  
  611. gL.com.setMmodes = function() -- set/update M-Modes for devices
  612.     if gL.com.E == "M_PRESSED" and gL.com.F == gL.com.family1 then          -- update M-Mode for main device
  613.         gL.com.M1 = gL.com.A
  614.     elseif gL.poll.on == false and gL.com.E == "M_PRESSED" and gL.com.F == gL.com.family2 then          -- update M-Mode for poll device if not polling
  615.         gL.com.M2 = gL.com.A
  616.     end
  617.     gL.com.M[gL.com.family1] = gL.com.M1
  618.     gL.com.M[gL.com.family2] = gL.com.M2
  619.     gL.com.M["mml"]=gL.com.M1
  620. end
  621.  
  622. function gL.com.setupkeys() -- create SET-assignment-array & KEY-status-array (for all keys set in gL.device)
  623.     local device,group,name,detail,tab, d,m,g,n,s,x
  624.     -- Profile Events
  625.     gL.SET.PA = {}
  626.     gL.SET.PD = {}
  627.     -- Devices
  628.     for d = 0, table.getn(gL.device),1 do
  629.         device = gL.device[d][1]
  630.         gL.SET[device]={}; gL.KEY[device] = {}
  631.         -- M-Modes
  632.         gL.SET[device].M = {}--; gL.KEY[device].M = {}
  633.         for m = 0,3,1 do
  634.             gL.SET[device].M[m] = {}--; gL.KEY[device].M[m] = {}
  635.             for g = 1,table.getn(gL.KEY.defaults[device]),1 do
  636.                 -- Groups
  637.                 group = gL.KEY.defaults[device][g]
  638.                 gL.SET[device].M[m][group] = {}
  639.                 gL.KEY[device][group] = {}
  640.                 for i,name in pairs(gL.KEY.setup[device][group]) do
  641.                     -- Names
  642.                     gL.SET[device].M[m][group][name] = {}
  643.                     gL.SET[device].M[m][group][name].s = {}
  644.                     gL.KEY[device][group][name] = {s=0, d=1, tog=0, togstat=2, clicks=0, mcs=0, time=0, last_p=0, last_r=0}
  645.                     for s = 0,3,1 do
  646.                         -- States
  647.                         gL.SET[device].M[m][group][name].s[s] = 0 -- initial assignment = 0
  648.                     end
  649.                 end
  650.             end
  651.         end
  652.     end
  653.     gL.KEY.updateLO()
  654. end
  655.  
  656. function gL.com.setdevice() -- set the main and polling device keyboards
  657.     if table.getn(gL.device) == 2 then
  658.         gL.com.family1 = gL.device[1][1]
  659.         gL.com.family2 = gL.device[2][1]
  660.     elseif table.getn(gL.device) == 1 then
  661.         if gL.device[1][1] == "lhc" then
  662.             gL.com.family1 = "lhc"
  663.             gL.com.family2 = "kb"
  664.         elseif gL.device[1][1] == "kb" then
  665.             gL.com.family1 = "kb"
  666.             gL.com.family2 = "lhc"
  667.         else
  668.             gL.ERROR("gL.com.setdevice => unknown keyboard family set!")
  669.         end
  670.     else
  671.         gL.ERROR("gL.com.setdevice => illegal amount of keyboards set!") -- add no keyboard support
  672.     end
  673. end
  674.  
  675. gL.com.SMKS = SetMKeyState -- Hijack SetMKeyState to detect SMKS on same M-Mode
  676.  
  677. SetMKeyState = function (state, device) -- Notify gL if SetMKeyState on Poll Device was pressed in same State!
  678.     if gL.poll.on and device == gL.com.family2 and state == gL.com.M[gL.com.family2] then
  679.         gL.com.SMKS_set = state
  680.     end
  681.     gL.com.SMKS(state,device)
  682. end
  683.  
  684. gL.com.setTime = function ()
  685.     gL.com.time = math.ceil( GetRunningTime() / gL.poll.grid ) * gL.poll.grid
  686. end
  687.  
  688. gL.com.showLog = function () -- logging key action to OutputLog // outdated!!!!!!!!!!!
  689.     if gL.com.Es ~= "Poll0" and gL.com.E ~= "Poll" and gL.com.E ~= "ignore" then
  690.         OutputLogMessage(" [ "..gL.com.time.." "..GetRunningTime().."] \t\t\t "..gL.com.E.."\t"..gL.com.A.." "..gL.com.F.." \tM1:"..gL.com.M1.."\tM2:"..gL.com.M2.."\n")
  691.     end
  692. end
  693.  
  694. function gL.runtime()
  695.     if gL.com.runtime == nil then gL.com.runtime = GetRunningTime() end
  696.     return gL.com.runtime
  697. end
  698.  
  699.  
  700. function gL.com.PlayEvents()        -- POLL ONLY! play all stacked events
  701.     local device,group,name,winner, i,g
  702.     winner = 0
  703.     for i,devices in pairs(gL.device) do
  704.         device = devices[1]
  705.         for g = 1,table.getn(gL.KEY.defaults[device]),1 do
  706.             group = gL.KEY.defaults[device][g]
  707.             for i,name in pairs (gL.KEY.active[device][group]) do
  708.                 winner = winner + 1
  709.                 gL.com.PlayKey(device,group,name,gL.KEY[device][group][name].s)
  710.             end
  711.         end
  712.     end
  713.     if winner == 0 then
  714.         if gL.com.AKR == 0 then
  715.             gL.com.AKR = 1
  716.             gL.com.allkeysReleased()
  717.         end
  718.     else
  719.         if gL.com.AKR == 1 then gL.com.AKR = 0 end
  720.     end
  721. end
  722.  
  723. function gL.com.PlayKey(device, group, name, state)         -- Play Mode for Keys ("SET", "OnEvent", User-Function)
  724.     --OutputLogMessage("PlayKey: "..device.." "..group.." "..name.." "..state.." "..gL.KEY[device][group][name].s.."\n")
  725.     if gL.com.PlayMode == "OnEvent" then gL.com.PlayOnEvent(device,group,name,state)
  726.     elseif gL.com.PlayMode == "Script" then gL.com.PlayScript()
  727.     elseif gL.com.PlayMode == "SET" then gL.com.PlaySET(device,group,name,state)
  728.     else
  729.         -- OPTIONAL: dev function ...
  730.     end
  731. end
  732.  
  733. function gL.com.PlayScript()                                -- Play SET Mode
  734.     gL.com.Script()
  735. end
  736.  
  737. function gL.com.PlaySET(device,group,name,state)            -- Play SET Mode
  738.     local MS; if device == "mml" then MS = gL.com.M1 else MS = gL.com.M[device] end  -- set M-Modes
  739.     if type(gL.SET[device].M[0][group][name].s[state]) == "function" then
  740.         gL.SET[device].M[0][group][name].s[state]()
  741.         if gL.KEY.showSET == 1 then OutputLogMessage("gL.SET[%s].M[0].%s[%s].s[%s]() playing!\n", device, group, name, state) end
  742.     elseif type(gL.SET[device].M[MS][group][name].s[state]) == "function" then  
  743.         gL.SET[device].M[MS][group][name].s[state]()
  744.         if gL.KEY.showSET == 1 then OutputLogMessage("gL.SET[%s].M[%s].%s[%s].s[%s]() playing!\n", device, MS, group, name, state) end
  745.     else
  746.         if gL.KEY.showSET == 1 then OutputLogMessage("gL.SET[%s].M[%s].%s[%s].s[%s]() not defined!\n", device, MS, group, name, state) end
  747.     end
  748. end
  749.  
  750. function gL.com.PlayOnEvent(device,group,name,state)        -- Play OnEvent Mode
  751.     local p = "_PRESSED"
  752.     local r = "_RELEASED"
  753.     local d = "_DOWN"
  754.     if state == 1 then
  755.         gL.com.OnEvent(group..p, name, device)
  756.     elseif state == 2 and gL.com.OnEventDown == 1 then
  757.         gL.com.OnEvent(group..d, name, device)
  758.     elseif state == 3 then
  759.         gL.com.OnEvent(group..r, name, device)
  760.     end
  761. end
  762.  
  763. --eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement