Reply
Logi Browser
Hella
Posts: 7
Registered: ‎03-18-2011
0
Accepted Solution

g510 Script Help

Hello,

 

I am trying to create a script that will press the m key, delay for a random amount of time, release the key, then delay for another amount of time and make it loop for however many times I put for i.

 

this is what I have, but it doesn't seem to work

 

 

function OnEvent(event, arg)
randomsleep1=math.random(335, 798)
randomsleep2=math.random(35489, 42784)
i=5
if (event == "G_PRESSED" and arg == "18") then
loop=i
PressKey("m")
Sleep(randomsleep1)
ReleaseKey("m")
Sleep(randomsleep2)
end
end
end

Logi Journeyman
h0rse
Posts: 430
Registered: ‎08-13-2010

Re: g510 Script Help

[ Edited ]

the arg variable is an integer! .. so it should be arg == 18

this should work: (maybe use some smaller rnd-sleeps for testing)

ClearLog();OutputLogMessage("Script loaded!\n")
i=5
function OnEvent(event, arg)
	--OutputLogMessage("event = %s, arg = %s\n", event, arg);
    if (event == "G_PRESSED" and arg == 18) then
        for c = 1,i do
            randomsleep1=math.random(335, 798)
            randomsleep2=math.random(35489, 42784)
            --OutputLogMessage(c.."\n")
            PressKey("m")
            Sleep(randomsleep1)
            ReleaseKey("m")
            Sleep(randomsleep2)
        end
    end
end
--eof

 NOTE: during the loop (no other G-Keys will be recoginzed BY SCRIPT, since OnEvent() is still running and cannot be called again

-------------- UPIA --------------->
Logi Browser
Hella
Posts: 7
Registered: ‎03-18-2011
0

Re: g510 Script Help

So if I wanted to stop the script instantly, do I just press G18 again? or would more need to be added?

Logi Guru
bystander
Posts: 1,121
Registered: ‎06-04-2010

Re: g510 Script Help

Without the use of polling, you could not do that with a script.  The eastiest option is to check for modifiers to stop it.

Logi Browser
Hella
Posts: 7
Registered: ‎03-18-2011
0

Re: g510 Script Help

[ Edited ]

Hmmm, how about if the Active Profile isn't the 'focus" anymore? Like alt-tabbing to a different program. Does that stop the script automatically?

Logi Guru
bystander
Posts: 1,121
Registered: ‎06-04-2010

Re: g510 Script Help

It would, but not the most ideal.

Logi Guru
bystander
Posts: 1,121
Registered: ‎06-04-2010

Re: g510 Script Help

[ Edited ]

Here is an example of using the above script modified to stop if a modifier is pressed.  terminateKey is assigned to the modifier which will stop the script.  In this case, it's right-ctrl, but can be modified to what ever you want.

 

ClearLog();OutputLogMessage("Script loaded!\n")
i=5
minDelay = 16
terminateKey = "rctrl"
function OnEvent(event, arg)
	--OutputLogMessage("event = %s, arg = %s\n", event, arg);
    if (event == "G_PRESSED" and arg == 18) then
        RandomizedDelayKeyPress("m")
    end
end

function RandomizedDelayKeyPress(key)
    for c = 1,i do
        randomsleep1=math.random(335, 798)
        randomsleep2=math.random(35489, 42784)

        PressKey(key)
        local ret = Wait(randomsleep1, terminateKey)
        ReleaseKey(key)
        if ret == "aborted" then
            return
        end
        ret = Wait(randomsleep2, terminateKey)
        if ret == "aborted" then
            return
        end
    end
end

function Wait(time, abortModifier)
    local ts = GetRunningTime()
    while GetRunningTime() - ts < time do
        if IsModifierPressed(abortModifier) then
            return "aborted"
 end Sleep(minDelay)
end return true end --eof

 

Logi Browser
Hella
Posts: 7
Registered: ‎03-18-2011
0

Re: g510 Script Help

Thank you very much for educating me :smileyhappy: