Advertisement
Guest User

script for logitech g602 repeat g-key press

a guest
Jan 9th, 2018
1,753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.35 KB | None | 0 0
  1. Courtesy of the internet way back machine after following the brilliant link you left above. Here it is:
  2.  
  3. @[Denis2027]
  4.  
  5. Instructions:
  6.  
  7. FIRST:
  8. Right-click on the profile icon, choose Scripting from the popup menu, then paste the script above into the Script Editor window, then save.
  9.  
  10. SCRIPT TO PASTE BELOW:
  11. --BEGIN COPY HERE
  12. -- all special functions must be defined BEFORE being assigned to a button
  13.  
  14. -- start/stop a repeat key task
  15. function fn_Repeat(event, gkey, family, delay, ...)
  16.     local taskname = "Repeat" .. gkey
  17.     if event == "G_PRESSED" or event == "MOUSE_BUTTON_PRESSED" then
  18.         if TaskRunning(taskname) then AbortTask(taskname) end
  19.         RunTask(taskname, t_Repeat, delay, ...)
  20.     elseif event == "G_RELEASED" or event == "MOUSE_BUTTON_RELEASED" then
  21.         StopTask(taskname)
  22.     end
  23. end
  24.  
  25. -- t_Repeat is the function that actually performs the repeat task
  26. function t_Repeat(delay, ...)
  27.     local f = true
  28.     local t1, t2 = 20, delay - 20
  29.     if t2 <= 0 then t1, t2 = delay / 2, delay / 2 end
  30.     while f do
  31.         PressKey(...)
  32.         f = TaskSleep(t1)
  33.         ReleaseKey(...)
  34.         f = f and TaskSleep(t2)
  35.     end
  36. end
  37.  
  38.  
  39. -- G602 Mouse Bindings
  40.  
  41. G602 = {[0] = {}}
  42.  
  43. G602[1] = {
  44.     [10] = {fn_Repeat, 100, 0x130}, -- 0x130 is the keycode for 'vol up', 100ms delay = 10 repeats/sec
  45.     [11] = {fn_Repeat, 100, 0x12e}, -- 0x12e is the keycode for 'vol down', 100ms delay = 10 repeats/sec
  46. }
  47.  
  48.  
  49. -- G13 Advanced Gameboard Bindings
  50.  
  51. G13 = {[0] = {}}
  52.  
  53. G13[1] = {  -- Mode M1
  54. }
  55.  
  56. G13[2] = {  -- Mode M2
  57. }
  58.  
  59. G13[3] = {  -- Mode M3
  60. }
  61.  
  62.  
  63. -- G510 Keyboard Bindings
  64.  
  65. G510 = {[0] = {}}
  66.  
  67. G510[1] = { -- Mode M1
  68. }
  69.  
  70. G510[2] = { -- Mode M2
  71. }
  72.  
  73. G510[3] = { -- Mode M3
  74. }
  75.  
  76.  
  77. -- G930 Headset Bindings
  78.  
  79. G930 = {[0] = {}}
  80.  
  81.  
  82. -- Choose which device(s) you are using.  The LGS Lua environment supports only one device for each category.
  83.  
  84. Map = {
  85.     ["lhc"] = G13,
  86.     ["kb"] = G510,
  87.     ["mouse"] = G602,
  88.     [""] = G930,
  89. }
  90.  
  91.  
  92. -- everything below this line is "framework" code and shouldn't normally be changed
  93.  
  94. function OnEvent(event, arg, family)
  95.     local st = StateTimer
  96.     if event == "G_PRESSED" or event == "MOUSE_BUTTON_PRESSED" then
  97.         local mode = GetMKeyState(family)
  98.         local map = Map[family][mode] or {}
  99.         local action = map[arg]
  100.         local t = type(action)
  101.         if t == "function" then
  102.             action(event, arg, family)
  103.         elseif t == "table" then
  104.             local f = action[1]
  105.             if type(f) == "function" then
  106.                 f(event, arg, family, unpack(action, 2))
  107.             else
  108.                 PressKey(unpack(action))
  109.             end
  110.         elseif t == "string" or t == "number" then
  111.             PressKey(action)
  112.         end
  113.         Map[family][0][arg] = action
  114.     elseif event == "G_RELEASED" or event == "MOUSE_BUTTON_RELEASED" then
  115.         local map = Map[family][0]
  116.         local action = map[arg]
  117.         local t = type(action)
  118.         if t == "function" then
  119.             action(event, arg, family)
  120.         elseif t == "table" then
  121.             local f = action[1]
  122.             if type(f) == "function" then
  123.                 f(event, arg, family, unpack(action, 2))
  124.             else
  125.                 ReleaseKey(unpack(action))
  126.             end
  127.         elseif t == "string" or t == "number" then
  128.             ReleaseKey(action)
  129.         end
  130.         map[arg] = nil
  131.     elseif event == "PROFILE_ACTIVATED" then
  132.         EnablePrimaryMouseButtonEvents(true)
  133.         InitPolling()
  134.     elseif event == "PROFILE_DEACTIVATED" then
  135.         ReleaseAllKeys()
  136.     end
  137.     DoTasks()
  138.     Poll(event, arg, family, st)
  139. end
  140.  
  141. POLL_FAMILY = "mouse"   -- current mice don't have M-states, so this is a good choice
  142. POLL_INTERVAL = 10  -- delay (in milliseconds) before next loop, used to throttle polling rate
  143. POLL_DEADTIME = 100 -- settling time (in milliseconds) during which old poll events are drained
  144.  
  145. function InitPolling()
  146.     ActiveState = GetMKeyState_Hook(POLL_FAMILY)
  147.     SetMKeyState_Hook(ActiveState, POLL_FAMILY)
  148. end
  149.  
  150. function Poll(event, arg, family, st)
  151.     if st == nil and StateTimer ~= nil then return end
  152.     local t = GetRunningTime()
  153.     if family == POLL_FAMILY then
  154.         if event == "M_PRESSED" and arg ~= ActiveState then
  155.             if StateTimer ~= nil and t >= StateTimer then StateTimer = nil end
  156.             if StateTimer == nil then ActiveState = arg end
  157.             StateTimer = t + POLL_DEADTIME
  158.         elseif event == "M_RELEASED" and arg == ActiveState then
  159.             Sleep(POLL_INTERVAL)
  160.             SetMKeyState_Hook(ActiveState, POLL_FAMILY)
  161.         end
  162.     end
  163. end
  164.  
  165. GetMKeyState_Hook = GetMKeyState
  166. GetMKeyState = function(family)
  167.     family = family or "kb"
  168.     if family == POLL_FAMILY then
  169.         return ActiveState
  170.     else
  171.         return GetMKeyState_Hook(family)
  172.     end
  173. end
  174.  
  175. SetMKeyState_Hook = SetMKeyState
  176. SetMKeyState = function(mkey, family)
  177.     family = family or "kb"
  178.     if family == POLL_FAMILY then
  179.         if mkey == ActiveState then return end
  180.         ActiveState = mkey
  181.         StateTimer = GetRunningTime() + POLL_DEADTIME
  182.     end
  183.     return SetMKeyState_Hook(mkey, family)
  184. end
  185.  
  186.  
  187. -- Task Management functions
  188.  
  189. TaskList = {}
  190.  
  191. function DoTasks()
  192.     local t = GetRunningTime()
  193.     for key, task in pairs(TaskList) do
  194.         if t >= task.time then
  195.             local s, d = coroutine.resume(task.task, task.run)
  196.             if (not s) or ((d or -1) < 0) then
  197.                 TaskList[key] = nil
  198.             else
  199.                 task.time = task.time + d
  200.             end
  201.         end
  202.     end
  203. end
  204.  
  205. function RunTask(key, func, ...)
  206.     AbortTask(key)
  207.     local task = {}
  208.     task.time = GetRunningTime()
  209.     task.task = coroutine.create(func)
  210.     task.run = true
  211.     local s, d = coroutine.resume(task.task, ...)
  212.     if (s) and ((d or -1) >= 0) then
  213.         task.time = task.time + d
  214.         TaskList[key] = task
  215.     end
  216. end
  217.  
  218. function StopTask(key)
  219.     local task = TaskList[key]
  220.     if task ~= nil then task.run = false end
  221. end
  222.  
  223. function AbortTask(key)
  224.     local task = TaskList[key]
  225.     if task == nil then return end
  226.     while true do
  227.         local s, d = coroutine.resume(task.task, false)
  228.         if (not s) or ((d or -1) < 0) then
  229.             TaskList[key] = nil
  230.             return
  231.         end
  232.     end
  233. end
  234.  
  235. function TaskRunning(key)
  236.     local task = TaskList[key]
  237.     if task == nil then return false end
  238.     return task.run
  239. end
  240.  
  241. function TaskSleep(delay)
  242.     return coroutine.yield(delay)
  243. end
  244.  
  245.  
  246. -- PressKey / ReleaseKey enhancements
  247. -- if PressKey is called n times for a key, don't release until ReleaseKey has been called n times
  248. -- allow arguments to be tables
  249. -- release keys in reverse order of how they were pressed
  250. -- allow "_", "^" and "@" prefixes for key-specific Shift, Ctrl and Alt modifiers
  251. -- allow "mb1" through "mb5" for mouse buttons
  252.  
  253. MODIFIER_PREDELAY = 20  -- 20ms delay to allow Shift, Ctrl or Alt to register
  254. MODIFIER_POSTDELAY = 10 -- 10ms delay before releasing Shift, Ctrl or Alt
  255.  
  256. KeyCounts = {}
  257.  
  258. PressKey_Hook = PressKey
  259. PressKey = function(...)
  260.     local n = select("#", ...)
  261.     for i = 1, n do
  262.         local arg = select(i, ...)
  263.         if type(arg) == "table" then
  264.             PressKey(unpack(arg))
  265.         elseif arg ~= nil then
  266.             local fShift, fCtrl, fAlt
  267.             local c = string.sub(arg, 1, 1)
  268.             while c == "_" or c == "^" or c == "@" do
  269.                 if c == "_" then
  270.                     fShift = true
  271.                 elseif c == "^" then
  272.                     fCtrl = true
  273.                 else
  274.                     fAlt = true
  275.                 end
  276.                 arg = string.sub(arg, 2)
  277.                 c = string.sub(arg, 1, 1)
  278.             end
  279.             c = KeyCounts[arg] or 0
  280.             c = c + 1
  281.             if c == 1 then
  282.                 if fShift then PressKey_Hook("lshift") end
  283.                 if fCtrl then PressKey_Hook("lctrl") end
  284.                 if fAlt then PressKey_Hook("lalt") end
  285.                 if fShift or fCtrl or fAlt then Sleep(MODIFIER_PREDELAY) end
  286.                 if string.sub(arg, 1, 2) == "mb" then
  287.                     arg = string.sub(arg, 3)
  288.                     PressMouseButton(arg)
  289.                 else
  290.                     PressKey_Hook(arg)
  291.                 end
  292.                 if fShift or fCtrl or fAlt then Sleep(MODIFIER_POSTDELAY) end
  293.                 if fAlt then ReleaseKey_Hook("lalt") end
  294.                 if fCtrl then ReleaseKey_Hook("lctrl") end
  295.                 if fShift then ReleaseKey_Hook("lshift") end
  296.             end
  297.             KeyCounts[arg] = c
  298.         end
  299.     end
  300. end
  301.  
  302. ReleaseKey_Hook = ReleaseKey
  303. ReleaseKey = function(...)
  304.     local n = select("#", ...)
  305.     for i = n, 1, -1 do
  306.         local arg = select(i, ...)
  307.         if type(arg) == "table" then
  308.             ReleaseKey(unpack(arg))
  309.         elseif arg ~= nil then
  310.             local c = string.sub(arg, 1, 1)
  311.             while c == "_" or c == "^" or c == "@" do
  312.                 arg = string.sub(arg, 2)
  313.                 c = string.sub(arg, 1, 1)
  314.             end
  315.             c = KeyCounts[arg] or 1
  316.             c = c - 1
  317.             if c == 0 then
  318.                 c = nil
  319.                 if string.sub(arg, 1, 2) == "mb" then
  320.                     arg = string.sub(arg, 3)
  321.                     ReleaseMouseButton(arg)
  322.                 else
  323.                     ReleaseKey_Hook(arg)
  324.                 end
  325.             end
  326.             KeyCounts[arg] = c
  327.         end
  328.     end
  329. end
  330.  
  331. function ReleaseAllKeys()
  332.     for arg, _ in pairs(KeyCounts) do
  333.         KeyCounts[arg] = nil
  334.         if string.sub(arg, 1, 2) == "mb" then
  335.             arg = string.sub(arg, 3)
  336.             ReleaseMouseButton(arg)
  337.         else
  338.             ReleaseKey_Hook(arg)
  339.         end
  340.     end
  341. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement