Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- A Logitech Script with Polling and Tasks
- -------------------------------------------
- -- Initial setup -->
- EnablePrimaryMouseButtonEvents(true);
- ClearLog();OutputLCDMessage("");ClearLCD();OutputLogMessage("PROFILE: LogitechScript\n");
- LogEvents = false
- -- keyboard (-families)
- mfamily = "lhc" --main family for mode sync
- ofamily = "kb" --secondary family, mode will be sync
- pfamily = "mouse" --mouse family used for polling
- -- polling
- Poll = true; PollRate = 1
- TaskList = {}; TaskActive = -1; TaskCheckLast = -1 ; tPanic = -1
- -- Key-Setup (OnEvent) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - >>>
- function OnEvent(event, arg, family)
- -- Act on keys -->
- if event == "PROFILE_ACTIVATED" then PollIni() end
- if family == "mouse" then -- G502 Mouse -->
- -- on any mode -- Mx Mode -->
- if arg == 8 then -- M8 -->
- if event == "MOUSE_BUTTON_PRESSED" then
- --
- elseif event == "MOUSE_BUTTON_RELEASED" then
- --
- end
- elseif arg == 9 then -- M9 -->
- if event == "MOUSE_BUTTON_PRESSED" then
- --
- elseif event == "MOUSE_BUTTON_RELEASED" then
- --
- end
- elseif arg == 10 then -- M10 (M3-R)-->
- if event == "MOUSE_BUTTON_PRESSED" then
- --
- elseif event == "MOUSE_BUTTON_RELEASED" then
- --
- end
- elseif arg == 11 then -- M11 (M3-L) -->
- if event == "MOUSE_BUTTON_PRESSED" then
- --
- elseif event == "MOUSE_BUTTON_RELEASED" then
- --
- end
- end
- if GetMKeyState(mfamily) == 1 then -- M1 Mode -->
- if arg == 1 then -- M1 -->
- if event == "MOUSE_BUTTON_PRESSED" then
- --
- elseif event == "MOUSE_BUTTON_RELEASED" then
- --
- end
- elseif arg == 3 then -- M3 -->
- if event == "MOUSE_BUTTON_PRESSED" then
- --
- elseif event == "MOUSE_BUTTON_RELEASED" then
- --
- end
- end
- elseif GetMKeyState(mfamily) == 2 then -- M2 Mode -->
- if arg == 4 then -- M4 -->
- if event == "MOUSE_BUTTON_PRESSED" then
- --
- elseif event == "MOUSE_BUTTON_RELEASED" then
- --
- end
- elseif arg == 5 then -- M5 -->
- if event == "MOUSE_BUTTON_PRESSED" then
- --
- elseif event == "MOUSE_BUTTON_RELEASED" then
- --
- end
- elseif arg == 6 then -- M6 -->
- if event == "MOUSE_BUTTON_PRESSED" then
- --
- elseif event == "MOUSE_BUTTON_RELEASED" then
- --
- end
- end
- end
- elseif family == "lhc" then -- G13 Controller -->
- -- on any mode -- Mx Mode -->
- if arg == 1 then -- G1 -->
- if event == "G_PRESSED" then
- --
- elseif event == "G_RELEASED" then
- --
- end
- elseif arg == 3 then -- G3 -->
- if event == "G_PRESSED" then
- --
- elseif event == "G_RELEASED" then
- --
- end
- end
- if GetMKeyState(mfamily) == 1 then -- M1 Mode -->
- if arg == 1 then -- G1 -->
- if event == "G_PRESSED" then
- --
- elseif event == "G_RELEASED" then
- --
- end
- elseif arg == 5 then -- G5 -->
- if event == "G_PRESSED" then
- --
- elseif event == "G_RELEASED" then
- --
- end
- end
- elseif GetMKeyState(mfamily) == 2 then -- M2 Mode -->
- if arg == 1 then -- G1 -->
- if event == "G_PRESSED" then
- --
- elseif event == "G_RELEASED" then
- --
- end
- elseif arg == 2 then -- G2 -->
- if event == "G_PRESSED" then
- --
- elseif event == "G_RELEASED" then
- --
- end
- end
- end
- elseif family == "kb" then -- G-Keyboard -->
- if GetMKeyState(mfamily) == 1 then -- M1 Mode -->
- if arg == 1 then -- G1 --> Example Rapid Fire
- if event == "G_PRESSED" then
- FireKey(event)
- elseif event == "G_RELEASED" then
- FireKey(event)
- end
- elseif arg == 2 then -- G2 --> Example Non-Abort Task
- if event == "G_PRESSED" then
- PressSomeKeys(event)
- elseif event == "G_RELEASED" then
- PressSomeKeys(event)
- end
- elseif arg == 6 then -- G6 --> Panic key (press to stop script, hold to abort script)
- if event == "G_PRESSED" then
- PanicKeyPlay(event)
- elseif event == "G_RELEASED" then
- PanicKeyPlay(event)
- end
- end
- elseif GetMKeyState(mfamily) == 2 then -- M2 Mode -->
- if arg == 4 then -- G4 -->
- if event == "G_PRESSED" then
- --
- elseif event == "G_RELEASED" then
- --
- end
- elseif arg == 11 then -- G11 -->
- if event == "G_PRESSED" then
- --
- elseif event == "G_RELEASED" then
- --
- end
- end
- elseif GetMKeyState(mfamily) == 3 then -- M3 Mode -->
- if arg == 4 then -- G4 -->
- if event == "G_PRESSED" then
- --
- elseif event == "G_RELEASED" then
- --
- end
- elseif arg == 11 then -- G11 -->
- if event == "G_PRESSED" then
- --
- elseif event == "G_RELEASED" then
- --
- end
- end
- end
- end
- --
- PollPlay(event, arg, family)
- end
- function OnGEvent(event, arg, family)
- --
- end
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - >>>
- -- Task Example without abort option (this will run the whole task - no aborting planned)
- function PressSomeKeys(event) --trigger function for key-setup
- if event == "G_PRESSED" or event == "MOUSE_BUTTON_PRESSED" then
- TaskRun("SomeKeysABC", t_PressSomeKeys)
- elseif event == "G_RELEASED" or event == "MOUSE_BUTTON_RELEASED" then
- --
- end
- end
- function t_PressSomeKeys()
- local TaskOn = true -- Always line 1
- PressKey("a")
- TaskOn = TaskSleep(70)
- ReleaseKey("a")
- TaskOn = TaskSleep(100)
- PressKey("b")
- TaskOn = TaskSleep(70)
- ReleaseKey("b")
- TaskOn = TaskSleep(100)
- PressKey("c")
- TaskOn = TaskSleep(70)
- ReleaseKey("c")
- return -1 -- Always last line
- end
- -- Task Example with abort option
- function FireKey(event) --trigger function for key-setup
- if event == "G_PRESSED" or event == "MOUSE_BUTTON_PRESSED" then
- TaskRun("RapidFire", t_RapidFire)
- elseif event == "G_RELEASED" or event == "MOUSE_BUTTON_RELEASED" then
- TaskAbort("RapidFire")
- end
- end
- function t_RapidFire()
- local TaskOn = true -- Always line 1
- local firedown = 60
- local delay = 120
- local dy = 12
- while TaskOn do -- Loop while running (with abort option)
- logg("rf")
- PressKey("rctrl")
- TaskOn = TaskSleep(firedown) -- Sleep (without abort option, always release the key!)
- ReleaseKey("rctrl")
- MoveMouseRelative(0, dy)
- if TaskOn then TaskOn = TaskSleep(delay-firedown) else return -1 end -- Sleep (with abort option)
- end
- return -1
- end
- -- general functions - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- function PK(str, delay)
- delay = delay or 60
- PressKey(str)
- Sleep(delay)
- ReleaseKey(str)
- end
- function PKT(str, delay)
- delay = delay or 60
- TaskRun("pkt_"..str, t_pkt, str, delay)
- end
- function t_pkt(str, delay)
- local TaskOn = true
- PressKey(str)
- TaskSleep(delay)
- ReleaseKey(str)
- return -1
- end
- function TabEscape(event)
- if event == "G_PRESSED" or event == "MOUSE_BUTTON_PRESSED" then
- if not ShiftOn then PressKey("tab") else PressKey("escape") end
- else
- if not ShiftOn then ReleaseKey("tab") else ReleaseKey("escape") end
- end
- end
- function old_logevent(event, arg, family)
- 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
- end
- function logg(...)
- OutputLogMessage(...)
- OutputLogMessage("\n")
- end
- function lcd(str)
- OutputLCDMessage(str, 99999999)
- end
- function round(x)
- return math.floor(x + 0.5)
- end
- function PanicKey()
- --
- TaskAbortAll()
- --PollStop()
- 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")
- ReleaseKey("printscreen", "scrolllock", "pause", "tilde", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "minus", "equal", "backspace", "tab")
- 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")
- 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")
- ReleaseKey("right", "numlock", "numslash", "numminus", "num7", "num8", "num9", "numplus", "num4", "num5", "num6", "num1", "num2", "num3", "numenter", "num0", "appkey", "numperiod") -- add here
- ReleaseMouseButton(1)
- ReleaseMouseButton(2)
- ReleaseMouseButton(3)
- ReleaseMouseButton(4)
- ReleaseMouseButton(5)
- end
- function PanicKey2()
- --
- TaskAbortAll()
- PollStop()
- PanicKey()
- crash()
- end
- function PanicKeyPlay(event)
- if event == "G_PRESSED" or event == "MOUSE_BUTTON_PRESSED" then
- tPanic = GetRunningTime()
- PanicKey()
- else
- if GetRunningTime() - tPanic > 800 then
- PanicKey2()
- end
- end
- end
- function crash()
- logg(foobar)
- end
- -- Polling (loop-engine) functions - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- function PollPlay(event, arg, family)
- if Poll and family == pfamily and event == "M_RELEASED" then
- TaskCheck()
- TaskAbort("PollMS") -- NEW: abort old TaskCheckerLoop
- Sleep(1) -- true Sleep 1ms, to reduce CPU from ~15% to 0.5%
- TaskRun("PollMS", t_PollMS) -- NEW: run new TaskCheckerLoop
- SetMKeyState(1, pfamily)
- elseif Poll and family == pfamily and event == "M_PRESSED" then
- -- nothing here
- else --non-poll-event
- TaskCheck()
- OnGEvent(event, arg, family)
- if LogEvents then logg("event = %s, arg = %s, family =%s", event, arg, family) end
- end
- end
- function PollStart() Poll = true end
- function PollStop() Poll = false end
- function PollIni() SetMKeyState(1, pfamily) end
- function t_PollMS() -- NEW: Loop TaskCheck() function every 1ms
- local TaskOn = true
- while TaskOn do
- TaskCheck()
- TaskOn = TaskSleep(1)
- end
- return -1
- end
- function TaskCheck()
- local t = GetRunningTime()
- if t ~= TaskCheckLast then
- TaskCheckLast = t
- for key, task in pairs(TaskList) do
- if t >= task.time then
- TaskActive = key
- local s, d = coroutine.resume(task.task, task.run)
- TaskActive = false
- if (not s) or ((d or -1) < 0) then
- TaskList[key] = nil
- else
- task.time = task.time + d
- end
- end
- end
- end
- end
- function TaskRun(key, func, ...)
- TaskAbort(key)
- local task = {}
- task.time = GetRunningTime()
- task.task = coroutine.create(func)
- task.run = true
- TaskActive = key
- local s, d = coroutine.resume(task.task, ...)
- TaskActive = false
- if (s) and ((d or -1) >= 0) then
- task.time = task.time + d
- TaskList[key] = task
- end
- end
- function TaskStop(key)
- local task = TaskList[key]
- if task ~= nil then task.run = false end
- end
- function TaskAbort(key)
- local task = TaskList[key]
- if task == nil then return end
- while true do
- local s, d = coroutine.resume(task.task, false)
- if (not s) or ((d or -1) < 0) then
- TaskList[key] = nil
- return
- end
- end
- end
- function TaskAbortAll()
- for key, task in pairs(TaskList) do
- if task.run then logg("Aborting Task: " .. key) end
- TaskAbort(key)
- end
- TaskList={}
- end
- function TaskRunning(key)
- local task = TaskList[key]
- if task == nil then return false end
- return task.run
- end
- function TaskSleep(delay)
- return coroutine.yield(delay)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement