Advertisement
L4k

Battlefield V

L4k
May 20th, 2019
3,768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.30 KB | None | 0 0
  1. -- A Logitech Script with Polling and Tasks
  2. -------------------------------------------
  3.  
  4. -- Initial setup -->
  5. EnablePrimaryMouseButtonEvents(true);
  6. ClearLog();OutputLCDMessage("");ClearLCD();OutputLogMessage("PROFILE: LogitechScript\n");
  7.  
  8. LogEvents = false
  9.  
  10. -- keyboard (-families)
  11. mfamily = "lhc"     --main family for mode sync
  12. ofamily = "kb"  --secondary family, mode will be sync
  13. pfamily = "mouse"   --mouse family used for polling
  14.  
  15. -- polling
  16. Poll = true; PollRate = 1
  17. TaskList = {}; TaskActive = -1; TaskCheckLast = -1  ; tPanic = -1
  18.  
  19. -- Key-Setup (OnEvent) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - >>>
  20. function OnEvent(event, arg, family)
  21.     -- Act on keys -->
  22.     if event == "PROFILE_ACTIVATED" then PollIni() end
  23.     if family == "mouse" then                           -- G502 Mouse -->
  24.         -- on any mode                                                  -- Mx Mode -->
  25.             if arg == 8 then                                                                -- M8 -->
  26.                 if event == "MOUSE_BUTTON_PRESSED" then
  27.                     --
  28.                 elseif event == "MOUSE_BUTTON_RELEASED" then
  29.                     --
  30.                 end
  31.             elseif arg == 9 then                                                            -- M9 -->
  32.                 if event == "MOUSE_BUTTON_PRESSED" then
  33.                     --
  34.                 elseif event == "MOUSE_BUTTON_RELEASED" then
  35.                     --
  36.                 end
  37.             elseif arg == 10 then                                                           -- M10 (M3-R)-->
  38.                 if event == "MOUSE_BUTTON_PRESSED" then
  39.                     --
  40.                 elseif event == "MOUSE_BUTTON_RELEASED" then
  41.                     --
  42.                 end
  43.             elseif arg == 11 then                                                           -- M11 (M3-L) -->
  44.                 if event == "MOUSE_BUTTON_PRESSED" then
  45.                     --
  46.                 elseif event == "MOUSE_BUTTON_RELEASED" then
  47.                     --
  48.                 end
  49.             end
  50.         if GetMKeyState(mfamily) == 1 then                                  -- M1 Mode -->
  51.             if arg == 1 then                                                                -- M1 -->
  52.                 if event == "MOUSE_BUTTON_PRESSED" then
  53.                     --
  54.                 elseif event == "MOUSE_BUTTON_RELEASED" then
  55.                     --
  56.                 end
  57.             elseif arg == 3 then                                                            -- M3 -->
  58.                 if event == "MOUSE_BUTTON_PRESSED" then
  59.                     --
  60.                 elseif event == "MOUSE_BUTTON_RELEASED" then
  61.                     --
  62.                 end
  63.             end
  64.         elseif GetMKeyState(mfamily) == 2 then                              -- M2 Mode -->
  65.             if arg == 4 then                                                                -- M4 -->
  66.                 if event == "MOUSE_BUTTON_PRESSED" then
  67.                     --
  68.                 elseif event == "MOUSE_BUTTON_RELEASED" then
  69.                     --
  70.                 end
  71.             elseif arg == 5 then                                                            -- M5 -->
  72.                 if event == "MOUSE_BUTTON_PRESSED" then
  73.                     --
  74.                 elseif event == "MOUSE_BUTTON_RELEASED" then
  75.                     --
  76.                 end
  77.             elseif arg == 6 then                                                            -- M6 -->
  78.                 if event == "MOUSE_BUTTON_PRESSED" then
  79.                     --
  80.                 elseif event == "MOUSE_BUTTON_RELEASED" then
  81.                     --
  82.                 end
  83.             end
  84.         end
  85.     elseif family == "lhc" then                         -- G13 Controller -->
  86.         -- on any mode                                                  -- Mx Mode -->
  87.             if arg == 1 then                                                                -- G1 -->
  88.                 if event == "G_PRESSED" then
  89.                     --
  90.                 elseif event == "G_RELEASED" then
  91.                     --
  92.                 end
  93.             elseif arg == 3 then                                                            -- G3 -->
  94.                 if event == "G_PRESSED" then
  95.                     --
  96.                 elseif event == "G_RELEASED" then
  97.                     --
  98.                 end
  99.             end
  100.         if GetMKeyState(mfamily) == 1 then                                  -- M1 Mode -->
  101.             if arg == 1 then                                                                -- G1 -->
  102.                 if event == "G_PRESSED" then
  103.                     --
  104.                 elseif event == "G_RELEASED" then
  105.                     --
  106.                 end
  107.             elseif arg == 5 then                                                            -- G5 -->
  108.                 if event == "G_PRESSED" then
  109.                     --
  110.                 elseif event == "G_RELEASED" then
  111.                     --
  112.                 end
  113.             end
  114.         elseif GetMKeyState(mfamily) == 2 then                              -- M2 Mode -->
  115.             if arg == 1 then                                                                -- G1 -->
  116.                 if event == "G_PRESSED" then
  117.                     --
  118.                 elseif event == "G_RELEASED" then
  119.                     --
  120.                 end
  121.             elseif arg == 2 then                                                            -- G2 -->
  122.                 if event == "G_PRESSED" then
  123.                     --
  124.                 elseif event == "G_RELEASED" then
  125.                     --
  126.                 end
  127.             end
  128.         end
  129.     elseif family == "kb" then                          -- G-Keyboard -->
  130.         if GetMKeyState(mfamily) == 1 then                                  -- M1 Mode -->
  131.             if arg == 1 then                                                                -- G1 --> Example Rapid Fire
  132.                 if event == "G_PRESSED" then
  133.                     FireKey(event)
  134.                 elseif event == "G_RELEASED" then
  135.                     FireKey(event)
  136.                 end
  137.             elseif arg == 2 then                                                            -- G2 --> Example Non-Abort Task
  138.                 if event == "G_PRESSED" then
  139.                     PressSomeKeys(event)
  140.                 elseif event == "G_RELEASED" then
  141.                     PressSomeKeys(event)
  142.                 end
  143.             elseif arg == 6 then                                                            -- G6 --> Panic key (press to stop script, hold to abort script)
  144.                 if event == "G_PRESSED" then
  145.                     PanicKeyPlay(event)
  146.                 elseif event == "G_RELEASED" then
  147.                     PanicKeyPlay(event)
  148.                 end
  149.             end
  150.         elseif GetMKeyState(mfamily) == 2 then                              -- M2 Mode -->
  151.             if arg == 4 then                                                                -- G4 -->
  152.                 if event == "G_PRESSED" then
  153.                     --
  154.                 elseif event == "G_RELEASED" then
  155.                     --
  156.                 end
  157.             elseif arg == 11 then                                                           -- G11 -->
  158.                 if event == "G_PRESSED" then
  159.                     --
  160.                 elseif event == "G_RELEASED" then
  161.                     --
  162.                 end
  163.             end
  164.         elseif GetMKeyState(mfamily) == 3 then                              -- M3 Mode -->
  165.             if arg == 4 then                                                                -- G4 -->
  166.                 if event == "G_PRESSED" then
  167.                     --
  168.                 elseif event == "G_RELEASED" then
  169.                     --
  170.                 end
  171.             elseif arg == 11 then                                                           -- G11 -->
  172.                 if event == "G_PRESSED" then
  173.                     --
  174.                 elseif event == "G_RELEASED" then
  175.                     --
  176.                 end
  177.             end
  178.         end
  179.     end
  180.     --
  181.     PollPlay(event, arg, family)
  182. end
  183.  
  184. function OnGEvent(event, arg, family)
  185.     --
  186. end
  187. -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - >>>
  188.  
  189. -- Task Example without abort option (this will run the whole task - no aborting planned)
  190. function PressSomeKeys(event) --trigger function for key-setup
  191.     if event == "G_PRESSED" or event == "MOUSE_BUTTON_PRESSED" then
  192.         TaskRun("SomeKeysABC", t_PressSomeKeys)
  193.     elseif event == "G_RELEASED" or event == "MOUSE_BUTTON_RELEASED" then
  194.         --
  195.     end
  196. end
  197.  
  198. function t_PressSomeKeys()
  199.     local TaskOn = true                                     -- Always line 1
  200.     PressKey("a")
  201.     TaskOn = TaskSleep(70)                                     
  202.     ReleaseKey("a")
  203.     TaskOn = TaskSleep(100)
  204.     PressKey("b")
  205.     TaskOn = TaskSleep(70)
  206.     ReleaseKey("b")
  207.     TaskOn = TaskSleep(100)
  208.     PressKey("c")
  209.     TaskOn = TaskSleep(70)
  210.     ReleaseKey("c")
  211.     return -1                                                   -- Always last line
  212. end
  213.  
  214. -- Task Example with abort option
  215. function FireKey(event) --trigger function for key-setup
  216.     if event == "G_PRESSED" or event == "MOUSE_BUTTON_PRESSED" then
  217.         TaskRun("RapidFire", t_RapidFire)
  218.     elseif event == "G_RELEASED" or event == "MOUSE_BUTTON_RELEASED" then
  219.         TaskAbort("RapidFire")
  220.     end
  221. end
  222. function t_RapidFire()
  223.     local TaskOn = true                                                 -- Always line 1
  224.     local firedown = 60
  225.     local delay = 120
  226.     local dy = 12
  227.     while TaskOn do                                                     -- Loop while running (with abort option)
  228.     logg("rf")
  229.         PressKey("rctrl")
  230.         TaskOn = TaskSleep(firedown)                                        -- Sleep (without abort option, always release the key!)
  231.         ReleaseKey("rctrl")
  232.         MoveMouseRelative(0, dy)
  233.         if TaskOn then TaskOn = TaskSleep(delay-firedown) else return -1 end        -- Sleep (with abort option)
  234.     end
  235.     return -1
  236. end
  237.  
  238.  
  239.  
  240. -- general functions - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  241.  
  242. function PK(str, delay)
  243.     delay = delay or 60
  244.     PressKey(str)
  245.     Sleep(delay)
  246.     ReleaseKey(str)
  247. end
  248. function PKT(str, delay)
  249.     delay = delay or 60
  250.     TaskRun("pkt_"..str, t_pkt, str, delay)
  251. end
  252. function t_pkt(str, delay)
  253.     local TaskOn = true
  254.     PressKey(str)
  255.     TaskSleep(delay)
  256.     ReleaseKey(str)
  257.     return -1
  258. end
  259. function TabEscape(event)
  260.     if event == "G_PRESSED" or event == "MOUSE_BUTTON_PRESSED" then
  261.         if not ShiftOn then PressKey("tab") else PressKey("escape") end
  262.     else
  263.         if not ShiftOn then ReleaseKey("tab") else ReleaseKey("escape") end
  264.     end
  265. end
  266. function old_logevent(event, arg, family)
  267.     if LogEvents and not ( family == pfamily and (event == "M_PRESSED" or event == "M_RELEASED") ) then OutputLogMessage("event = %s, arg = %s, family =%s\n", event, arg, family) end
  268. end
  269. function logg(...)
  270.     OutputLogMessage(...)
  271.     OutputLogMessage("\n")
  272. end
  273. function lcd(str)
  274.     OutputLCDMessage(str, 99999999)
  275. end
  276. function round(x)
  277.     return math.floor(x + 0.5)
  278. end
  279. function PanicKey()
  280.     --
  281.     TaskAbortAll()
  282.     --PollStop()
  283.     ReleaseKey("escape", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12") --, "f13", "f14", "f15", "f16", "f17", "f18", "f19", "f20", "f21", "22", "f23", "f24")
  284.     ReleaseKey("printscreen", "scrolllock", "pause", "tilde", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "minus", "equal", "backspace", "tab")
  285.     ReleaseKey("q", "w", "e", "r", "t", "z", "u", "i", "o", "p", "lbracket", "rbracket", "backslash", "capslock", "a", "s", "d", "f", "g", "h", "j", "k", "l", "semicolon", "quote", "enter", "lshift")
  286.     ReleaseKey("non_us_slash", "z", "x", "c", "v", "b", "n", "m", "comma", "period", "slash", "rshift", "lctrl", "lgui", "lalt", "rgui", "appkey", "rctrl", "insert", "home", "pageup", "delete", "end", "pagedown", "up", "left", "down")
  287.     ReleaseKey("right", "numlock", "numslash", "numminus", "num7", "num8", "num9", "numplus", "num4", "num5", "num6", "num1", "num2", "num3", "numenter", "num0", "appkey", "numperiod") -- add here
  288.     ReleaseMouseButton(1)
  289.     ReleaseMouseButton(2)
  290.     ReleaseMouseButton(3)
  291.     ReleaseMouseButton(4)
  292.     ReleaseMouseButton(5)
  293. end
  294. function PanicKey2()
  295.     --
  296.     TaskAbortAll()
  297.     PollStop()
  298.     PanicKey()
  299.     crash()
  300. end
  301. function PanicKeyPlay(event)
  302.     if event == "G_PRESSED" or event == "MOUSE_BUTTON_PRESSED" then
  303.         tPanic = GetRunningTime()
  304.         PanicKey()
  305.     else
  306.         if GetRunningTime() - tPanic > 800 then
  307.             PanicKey2()
  308.         end
  309.     end
  310. end
  311. function crash()
  312.     logg(foobar)
  313. end
  314. -- Polling (loop-engine) functions - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  315. function PollPlay(event, arg, family)
  316.     if Poll and family == pfamily and event == "M_RELEASED" then
  317.         TaskCheck()
  318.         TaskAbort("PollMS")                                             -- NEW: abort old TaskCheckerLoop
  319.         Sleep(1)                                                            -- true Sleep 1ms, to reduce CPU from ~15% to 0.5%
  320.         TaskRun("PollMS", t_PollMS)                                         -- NEW: run new TaskCheckerLoop
  321.         SetMKeyState(1, pfamily)
  322.     elseif Poll and family == pfamily and event == "M_PRESSED" then
  323.         -- nothing here
  324.     else                                                                    --non-poll-event
  325.         TaskCheck()
  326.         OnGEvent(event, arg, family)
  327.         if LogEvents then logg("event = %s, arg = %s, family =%s", event, arg, family) end
  328.     end
  329. end
  330. function PollStart() Poll = true end
  331. function PollStop() Poll = false end
  332. function PollIni() SetMKeyState(1, pfamily) end
  333. function t_PollMS() -- NEW: Loop TaskCheck() function every 1ms
  334.     local TaskOn = true
  335.     while TaskOn do
  336.         TaskCheck()
  337.         TaskOn = TaskSleep(1)
  338.     end
  339.     return -1
  340. end
  341. function TaskCheck()
  342.     local t = GetRunningTime()
  343.     if t ~= TaskCheckLast then
  344.         TaskCheckLast = t
  345.         for key, task in pairs(TaskList) do
  346.             if t >= task.time then
  347.                 TaskActive = key
  348.                 local s, d = coroutine.resume(task.task, task.run)
  349.                 TaskActive = false
  350.                 if (not s) or ((d or -1) < 0) then
  351.                     TaskList[key] = nil
  352.                 else
  353.                     task.time = task.time + d
  354.                 end
  355.             end
  356.         end
  357.     end
  358. end
  359. function TaskRun(key, func, ...)
  360.     TaskAbort(key)
  361.     local task = {}
  362.     task.time = GetRunningTime()
  363.     task.task = coroutine.create(func)
  364.     task.run = true
  365.     TaskActive = key
  366.     local s, d = coroutine.resume(task.task, ...)
  367.     TaskActive = false
  368.     if (s) and ((d or -1) >= 0) then
  369.         task.time = task.time + d
  370.         TaskList[key] = task
  371.     end
  372. end
  373. function TaskStop(key)
  374.     local task = TaskList[key]
  375.     if task ~= nil then task.run = false end
  376. end
  377. function TaskAbort(key)
  378.     local task = TaskList[key]
  379.     if task == nil then return end
  380.     while true do
  381.         local s, d = coroutine.resume(task.task, false)
  382.         if (not s) or ((d or -1) < 0) then
  383.             TaskList[key] = nil
  384.             return
  385.         end
  386.     end
  387. end
  388. function TaskAbortAll()
  389.     for key, task in pairs(TaskList) do
  390.         if task.run then logg("Aborting Task: " .. key) end
  391.         TaskAbort(key)
  392.     end
  393.     TaskList={}
  394. end
  395. function TaskRunning(key)
  396.     local task = TaskList[key]
  397.     if task == nil then return false end
  398.     return task.run
  399. end
  400. function TaskSleep(delay)
  401.     return coroutine.yield(delay)
  402. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement