Reply
Logi Nu
Drugoli
Posts: 2
Registered: ‎03-20-2011
Accepted Solution

Scripting help

I have just made a Rapid Fire macro for my G11 keyboard, it uses the G15 button, and is made with the simple macro creater.

I have tried for some time now, to edit the script for the MW2 profile, so that I can just press my left mouse button, and it would use the macro.

 

function OnEvent(event, arg)
    OutputLogMessage("event = %s, arg = %s\n", event, arg)
	
    if IsMouseButtonPressed(1) then
	PlayMacro("Rapid Fire");
    end

    if not IsMouseButtonPressed(1) then
	AbortMacro();
    end

end

Can someone please help me, I am not very good at scripting, and I would really appreciate the help.

 

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

Re: Scripting help

hi drugoli,

this cant work ...

- IsMouseButton.. or IsModifier... are only detected during an event (e.g. G_PRESSED or G_RELEASED. so you could check if a mouse or modifier is pressed while another G-key is pressed like this.

=> in your case, the if IsMouse... will never turn true.

 

- secondly if it would work, the abort macro handler would always be true, if the mouse is not pressed and therfore trigger every time an event takes place.

 

=> you need something like a "while-loop" or "M-Poll" to script on the mouse/modifiers ... and then need a small handler, to treat mouse and modifiers like keys with events like (PRESSED and RELEASED) ...

you'll find plenty of samples here ... if you need more help you ll find it here!

-------------- UPIA --------------->
kgober
Posts: 5,869
Kudos: 797
Solutions: 483
Registered: ‎05-28-2009

Re: Scripting help

actually, I think that script would work, as long as you kept pressing G-keys.  it wouldn't matter which ones, you just need to keep generating events to activate the script with.  you could even do it by pressing M-keys.  so you could pay your baby brother to spam the G1 key while you play, for example.

 

but yeah, it won't work the way I assume you want it to, which is to act based solely on the mouse button state without having to spam G-keys.  in normal operation, you can use mouse button state to modify the action of a G-key (do one thing if the G-key is pressed without a mouse button held, or something different if it is), but you can't use mouse events to cause things to happen directly.

 

-ken

________________________________
I do not work for Logitech. I'm just a user.
Logi Guru
bystander
Posts: 1,930
Registered: ‎06-04-2010

Re: Scripting help

[ Edited ]

I made a script the other day for someone wanting to do almost exactly what you want.  You can just insert what you want to happen at the appropriate spot and alter it to work for the right buttons.  This uses a technique called polling, my _OnEvent() function will be called about 40 times per second.  _OnActivation is called once when the script is loaded.

 

At the top, you might need to change family to "lhc" if this is to be used with a left hand controller by logitech, otherwise leave it as "kb" for a keyboard.

 

Edit: one last thing.  Unless your PlayMacro does something more than just hit a single key or two in rapid succession, you likely don't need an AbortMacro.

 

 

p = { family = "kb"; delay = 25; presses = 0; mKeyState = nil } -- polling variables
_GetRunningTime = nil


function _OnEvent(event, arg, family)
--
-- ADD EVENT FUNCTIONALITY HERE
--
   
    if IsModifierPressed("lshift") then
        -- modifiers can be lshift, rshift, shift, lalt, ralt, alt, lctrl, rctrl, ctrl

        -------------------------------------------------------------------
        -- This will be called about 40 times per second while it's held.
        -- Insert code to be spammed here
        -------------------------------------------------------------------

        if lshiftPressed == nil then
            lshiftPressed = true
            --------------------------------------
            -- SIMILAR TO LSHIFT_PRESSED
            -- This gets called once when pressed
            --------------------------------------
            OutputLogMessage("lshift pressed\n")
        end
    else
        if lshiftPressed then
            --------------------------------------
            -- SIMILAR TO LSHIFT_RELEASED
            -- This gets called once when released
            --------------------------------------
            OutputLogMessage("lshift released\n")
        end
        lshiftPressed = nil
    end

    if IsMouseButtonPressed(1) then
        -- Mouse buttons can be 1-5.  Mouse 1 is left, 2 is right, 3 is middle, 4 and 5 are default actions on additional buttons

        -------------------------------------------------------------------
        -- This will be called about 40 times per second while it's held.
        -- Insert code to be spammed here
        -------------------------------------------------------------------

        if button1Pressed == nil then
            button1Pressed = true
            --------------------------------------
            -- SIMILAR TO MB1_PRESSED
            -- This gets called once when pressed
            --------------------------------------
            OutputLogMessage("Mouse button 1 pressed\n")
        end
    else
        if button1Pressed then
            --------------------------------------
            -- SIMILAR TO MB1_RELEASED
            -- This gets called once when released
            --------------------------------------
            OutputLogMessage("Mouse button 1 released\n")
        end
        button1Pressed = nil
    end
end

function _OnActivation(event, arg, family)
--
-- ADD ANY START UP ROUTINES HERE
--

end

-----------------------------------------------
-- NOTHING SHOULD BE NEEDED BELOW THIS POINT --
-----------------------------------------------

function OnEvent(event, arg, family)
    _GetRunningTime = GetRunningTime()
-- POLLING INITIATED HERE
    if event == "PROFILE_ACTIVATED" then
        _OnActivation(event, arg, family)
        initPolling()
        poll(event, arg, family)
    end
-- POLLING ROUTINES BELOW
    if family == p.family then
        poll(event, arg, family)
    end
    _OnEvent(event, arg, family)  -- Runs _OnEvent to compartmentalize
end

-- POLLING FUNCTIONS BELOW HERE
function poll(event, arg, family)
    if event == "M_PRESSED" then
        p.presses = p.presses + 1
    elseif event == "M_RELEASED" or event == "PROFILE_ACTIVATED" then
        if p.presses == 1 then
            SetMKeyState(GetMKeyState(p.family),p.family)
            Sleep(p.delay)
        end
        p.presses = p.presses - 1
    end
end

function initPolling()
    ClearLog()
    p.mKeyState = GetMKeyState(p.family)
    p.presses = 1       -- 1 press to start the system
end

 

 

Logi Nu
Drugoli
Posts: 2
Registered: ‎03-20-2011

Re: Scripting help

[ Edited ]

Well, I tried your version. And it worked with the Shift key, but not with the mouse button. Could it be because I have already set the mouse button to fire or something?

 

 

EDIT: Just tried removing Left Mouse button as the fire button in the MW2 options menu. And now it works, hank you very much :smileyvery-happy: