Reply
Logi Visitor
Mpyra
Posts: 32
Registered: ‎06-30-2010

How To Script Mouse Buttons? and How To Have Macro Stop If Another Button Is Pressed?

Hello,

          i was wondering how to script mouse buttons and also here to have a macro stop if another button is pressed.

 

I need to script a macro to switch to radar kit, then initiate a radar scan, this is complicated because any time i start a radar scan i stop sprinting, but if i press sprint again it will start sprinting.

 

So a macro is needed to immedietely start sprinting after initaiting a radar scan.

 

A radar scan takes 2 seconds, so the script needs to look something like this:

 

Start, num7 down up (switch to radar kit), mouse1 down(start radar scan), b down(start sprinting),  delay 2 seconds, mouse1 up(stop radar scan), b up (is not needed, i want to keep sprinting once the macro has completed), end.

 

I just need to know what mouse1 is called in scripting language, then this script should work fine.

 

However, i would like to be able to interupt this macro when i press another button, for example mouse 1.

 

This is because, during the 2 seconds that i am performing a radar scan, i may spot an enemy or come under fire, this macro would occupy my mouse1 button which i would need to return fire, thus i need a key that can interrupt this macro either a keyboard key or letter like "y" or my mouse1 button.

 

Is it possible to assign a key to interrupt the script function of a single key?

 

In this case the key is g8, but i am runnign scripts on keys g4 g11 g10 and g12 and i don't want the scripts on them to be interrupted.

 

Logi Visitor
Mpyra
Posts: 32
Registered: ‎06-30-2010

Re: How To Script Mouse Buttons? and How To Have Macro Stop If Another Button Is Pressed?

Okay, i have found out how to script mouse buttons, i read about a pdf that comes with the scripting software and i found it when i clicked scripting api under help in the script editor.

 

It's pressmousebutton(1) for mouse1 down and releasemousebutton(1) for up.

 

I still need to know if there is a way to make a scripting running on a key interruptable by other keys if anyone could help with that, ken?

kgober
Posts: 5,722
Kudos: 772
Solutions: 470
Registered: ‎05-28-2009

Re: How To Script Mouse Buttons? and How To Have Macro Stop If Another Button Is Pressed?

to make the macro interruptible, define it as a macro using the macro editor, but don't bind the macro to any key.  instead, invoke it from Lua using the PlayMacro() function.  if you later want to interrupt the macro, use the AbortMacro() function.  AbortMacro() is only able to interrupt a macro that was started with PlayMacro(), which is why you want to use PlayMacro() rather than binding the macro directly to a G key.

 

this is the easiest way to do it that I know of.

 

-ken

________________________________
I do not work for Logitech. I'm just a user.
Logi Visitor
Mpyra
Posts: 32
Registered: ‎06-30-2010

Re: How To Script Mouse Buttons? and How To Have Macro Stop If Another Button Is Pressed?

[ Edited ]

Okay... lol, as things are i have assigned the G8 key to script and have added the following line to the script in the editor:

 

 

 if (event=="G_PRESSED" and arg==8) then
PressKey("num7"); ReleaseKey("num7"); ReleaseKey("b"); PressMouseButton(1); Sleep(50); PressKey("b");Sleep(1150);ReleaseKey("b"); PressKey("b"); Sleep(4000); ReleaseMouseButton(1)
end

 

 

 

Here is everything that is in the script:

 

 

function OnEvent(event, arg, family)
if (event=="G_PRESSED" and arg==4) then
ReleaseKey("b"); PressKey("b"); PressKey("w")
end
if (event=="G_PRESSED" and arg==27) then
ReleaseKey("b"); PressKey("b"); PressKey("i")
end
if (event=="G_PRESSED" and arg==29) then
ReleaseKey("b"); PressKey("b"); PressKey("c")
end
if (event=="G_PRESSED" and arg==10) then
ReleaseKey("b"); PressKey("b"); PressKey("a")
end
if (event=="G_PRESSED" and arg==11) then
ReleaseKey("b"); PressKey("b"); PressKey("s")
end
if (event=="G_PRESSED" and arg==12) then
ReleaseKey("b"); PressKey("b"); PressKey("d")
end
if (event=="G_PRESSED" and arg==8) then
PressKey("num7"); ReleaseKey("num7"); ReleaseKey("b"); PressMouseButton(1); Sleep(50); PressKey("b");Sleep(1150);ReleaseKey("b"); PressKey("b"); Sleep(4000); ReleaseMouseButton(1)
end
if (event=="G_RELEASED" and arg==4) then
ReleaseKey("w")
end
if (event=="G_RELEASED" and arg==27) then
ReleaseKey("i")
end
if (event=="G_RELEASED" and arg==29) then
ReleaseKey("c")
end
if (event=="G_RELEASED" and arg==11) then
ReleaseKey("s")

 

 

 

The above line of script is the only method i've found of achieving the function that i need from the key, i only wish for the key to be interuptable by certain keys as i i would be using my jump key, possibly crouch key, possibly directional keys which are script themselves.

 

I would need to line of script to be interruptable by the keys i use to switch weapons which are not scripts, just regular keys like "h" and "j", as this is primary reason i could forsee myself wishing to cancel the execution of the script, encountering an enemy and needing to switch to a weapon.

 

Can it be done?

 

Thanks again as always ken

 

Edit:

 

Okay, i thought i'd include a bit more information about the keys that i would want to use to interupt the macro, these are the scroll buttons on my mouse, which is a Razer Mamba, i assigned the scroll upwards function to a macro which types "=" then "." which switches to close ranged weapons and drops them, or takes out fists if all closed ranged weapons have been dropped.

 

I drop all of my closed ranged weapons so that i can quickly and conveniently switch to my fists for faster movement.

 

Downwards is assigned to "-" which switches to my assault rifle.

 

So i would need either "-" or "=" to interrupt the line of script.

 

 

kgober
Posts: 5,722
Kudos: 772
Solutions: 470
Registered: ‎05-28-2009

Re: How To Script Mouse Buttons? and How To Have Macro Stop If Another Button Is Pressed?

yes, it can be done, but you will have to switch the keys you want to interrupt (e.g. 'h' and 'j') to use scripts invoked by G keys as well.

 

take the part that you want to be able to interrupt and put it into a macro instead of using script commands.  remember to include all the delays in the macro.  let's suppose you call that macro "RadarScan".

 

keep the G8 key assigned to the 'Script' action, and use this in your script:

 

    if (event=="G_PRESSED" and arg==8) then
        PlayMacro("RadarScan")
    end

 

 let's suppose you want to use G3 for 'h'.  bind G3 to the 'Script' action, and use this:

 

    if (event=="G_PRESSED" and arg==3) then
        AbortMacro("RadarScan")
        PressKey("h")
    end
    if (event=="G_RELEASED" and arg==3) then
        ReleaseKey("h")
    end

 

 

do the same for your 'j' key.  give that a try and go from there.

 

 

-ken

 

________________________________
I do not work for Logitech. I'm just a user.
Logi Visitor
Mpyra
Posts: 32
Registered: ‎06-30-2010

Re: How To Script Mouse Buttons? and How To Have Macro Stop If Another Button Is Pressed?

[ Edited ]

LoL, you posted that 1 minute after my lastest edit, take a look at what i added to my last post and see what you think...

 

If not, i guess this is good enough, i'll just assign a g key to interrupt then switch to my weapon but if it's possible to code it so that i do not need to press an intermedietary key i'd be willing to learn a programming language to do it, so is it?

 

Edit:

 

As a side note, the Razer Mamba macros are stored as MXM files, if that makes any difference...

kgober
Posts: 5,722
Kudos: 772
Solutions: 470
Registered: ‎05-28-2009

Re: How To Script Mouse Buttons? and How To Have Macro Stop If Another Button Is Pressed?

in general, you won't be able to detect keypresses generated by the Razer drivers and act on them from your scripts.

 

except that it might work if the Razer is generating things like shift, ctrl or alt.  also scripts can detect mouse buttons being held (but probably not if the mouse driver remaps the mouse button to something else like a key press, although I haven't tested this myself)

 

look up "IsModifierPressed" and "IsMouseButtonPressed" in the scripting api document for more info on this.

 

-ken

________________________________
I do not work for Logitech. I'm just a user.
Logi Visitor
Mpyra
Posts: 32
Registered: ‎06-30-2010

Re: How To Script Mouse Buttons? and How To Have Macro Stop If Another Button Is Pressed?

[ Edited ]

Okay thanks ken, it seems that the macro doesn't actiaully interfere with shooting in any case, i just have to press the fire button twice instead of once to begin firing.

 

However, there is a small issue of the macro repeating itself if i press it more than once, if i press twice it will repeat twice, three times three times thrice etc..

 

So for this reason i really need a key to cancel the macro, as having the macro repeat is actually quite useful, but i'd be interested to know why it repeats the amount of times i press the key.-

 

Here is the script i am running:

 

 

function OnEvent(event, arg, family)
    if (event=="G_PRESSED" and arg==4) then
        ReleaseKey("b"); PressKey("b"); PressKey("w")
    end
    if (event=="G_PRESSED" and arg==27) then
        ReleaseKey("b"); PressKey("b"); PressKey("i"); PressKey("d")
    end
    if (event=="G_PRESSED" and arg==29) then
        ReleaseKey("b"); PressKey("b"); PressKey("c"); PressKey("a")
    end
    if (event=="G_PRESSED" and arg==10) then
        ReleaseKey("b"); PressKey("b"); PressKey("a")
    end
    if (event=="G_PRESSED" and arg==11) then
        ReleaseKey("b"); PressKey("b"); PressKey("s")
    end
    if (event=="G_PRESSED" and arg==12) then
        ReleaseKey("b"); PressKey("b"); PressKey("d")
    end
    if (event=="G_PRESSED" and arg==8) then
        PlayMacro("RadarScan")
    end
    if (event=="G_PRESSED" and arg==14) then
        AbortMacro("RadarScan")
        PressKey("u")
    end
    if (event=="G_RELEASED" and arg==4) then
        ReleaseKey("w")
    end  
    if (event=="G_RELEASED" and arg==27) then
        ReleaseKey("i"); ReleaseKey("d")
    end
    if (event=="G_RELEASED" and arg==29) then
        ReleaseKey("c"); ReleaseKey("a")
    end
    if (event=="G_RELEASED" and arg==11) then
        ReleaseKey("s")
    end
    if (event=="G_RELEASED" and arg==10) then
        ReleaseKey("a")
    end
    if (event=="G_RELEASED" and arg==12) then
        ReleaseKey("d")
    end
    if (event=="G_RELEASED" and arg==14) then
        ReleaseKey("u")
    end

end

 

Pressing g14 does not stop the macro repeating or stop me from having to press the fire button twice to commence firing.

 

Edit:

 

Oh and the G13 macro editors does detect when i press the various buttons on my mamba that are assigned to press keys and perform macros, if i start recording and scroll up it will detect = . or if i scroll down it will detect -, however i don't see how i could assign these keys to abort a macro, as the method you've listed seems to require a g key, i couldn't have something like (event=="=PRESSED") then or something like that could i?

 

Thanks x1000000

 

 

Logi Visitor
Mpyra
Posts: 32
Registered: ‎06-30-2010

Re: How To Script Mouse Buttons? and How To Have Macro Stop If Another Button Is Pressed?

Also, i have just realised the the function that i assigned to "u" doesn't work with the above script and when i press g14 the letter u does not type in word pad or this box i am writing in now, i have also tried the following variation of that line of script:

 

 

  if (event=="G_PRESSED" and arg==14) then
        AbortMacro("RadarScan"); PressKey("u")
    end

 

Which does not result in "u" typing or functioning anywhere either...

 

 

 

kgober
Posts: 5,722
Kudos: 772
Solutions: 470
Registered: ‎05-28-2009

Re: How To Script Mouse Buttons? and How To Have Macro Stop If Another Button Is Pressed?

yes, the Macro Editor can see mouse-generated keypresses.  the problem is that your Lua script will only be called for Lua events, and Lua events are only generated for G key and M key actions (i.e. event == "G_PRESSED" or "G_RELEASED" or "M_PRESSED" or "M_RELEASED", but never any events for regular keys).  so you can't use a regular keyboard key to trigger any kind of action in a Lua script (excepting the modifier keys: shift, ctrl and alt, and that can only be done in conjunction with another G key or M key action).

 

I suspect the repeat problem is because repeated presses of G8 cause multiple PlayMacro() calls to 'stack up', but AbortMacro() only aborts one of them (rather than all of them), but I'm not certain.  if so, you can set a timer on the first press of G8 that you can use to define a 'blackout' period during which further presses of G8 would be ignored.

 

something like this, for a 5.2 second blackout period:

 

    if (event=="G_PRESSED" and arg==8) then
        local t = GetRunningTime()
        if (t - (G8_LastTime or 0) > 5200) then
            PlayMacro("RadarScan")
            G8_LastTime = t
        end
    end

 

also, if you abort the macro then you can cancel the blackout timer:

 

    if (event=="G_PRESSED" and arg==14) then
        AbortMacro("RadarScan")
        PressKey("u")
        G8_LastTime = 0
    end

 

 

of special note is the Lua idiom (G8_LastTime or 0), which means "whatever G8_LastTime is set to, or 0 if it is currently nil".  this is useful because all Lua variables start with a value of nil until a non-nil value is assigned to them.

 

-ken

 

________________________________
I do not work for Logitech. I'm just a user.