Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

lua - How do make a random.math int populate with every iteration of a function?

I'm creating my first ever coding project, a macro for my Logitech mouse using Lua, and I have a repeating function (autoclick) that I would like to generate a random delay between clicks.

I want it to be:

  • toggle-able with "scroll lock"
  • adjustable delay on the fly
  • to simply click and hold left-click to use

My code is as follows:

    --Button used to turn rapid fire on/off
rfLock="scrolllock"


--Rapid fire values used in randomization/realism, in ms (milleseconds).
rfLo=45
rfHi=75

EnablePrimaryMouseButtonEvents(true);

function RapidFire()
    if (rfLock==true) then
    if (IsMouseButtonPressed(1)==true)then
    repeat
    ReleaseMouseButton(1)
    sleep (math.random(rfLo,rfHi))
    PressMouseButton(1)
    until not IsMouseButtonPressed(1)

function OnEvent(event, arg)    

if IsKeyLockOn(rfLock)then
        rFire=true
    else
        rFire=false
    end
    
end

Is there another way I have to approach this? It currently does not do anything. I'm still trying to figure it out obviously, just thought I could get the knowledge from you guys here. Thanks in advance.

question from:https://stackoverflow.com/questions/65930157/how-do-make-a-random-math-int-populate-with-every-iteration-of-a-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I assume you are using GHUB (but the same could be done in LGS).

Step 0.
You are about to modify the behavior of Left Mouse Button.
This is a potentially dangerous operation: you can do nothing on your computer without LMB.
So you must create a "spare LMB".
For example, if you don't use Mouse Button 8, you can make it acting like a clone on LMB.
Go to GHUB (mouse device, "Assignments" screen, SYSTEM tab).
Bind "Primary click" to your physical MB#8.
Now, if something goes wrong and your LMB stops working, you can press MB#8 instead of LMB.

It might happen that you don't want to expend a mouse button for "spare LMB" because all the mouse buttons are in use for something important.
If you have a Logitech keyboard you can use one of its G-buttons as "spare LMB" (keyboard device, "Assignments" screen, SYSTEM tab).


Step 1.
Do you use Mouse Button 4 ("back") in the game?

  • If YES (some action is assigned to MB#4 in the game), proceed to "Step 2".
  • If NO (the game ignores MB#4 press), skip "Step 2" and proceed to "Step 3".

Step 2.
You have to remap game action from MB#4 to some other key.
Do the following:

  • choose keyboard button you don't currently use in the game
    (let's assume the F12 key is not currently used)
  • go to GHUB (mouse device, "Assignments" screen, KEYS tab);
    bind F12 to your physical MB#4
  • go to game options;
    assign the game action to F12 instead of MB#4

Now when you press physical MB#4, the game receives F12 and activates the game action.
Skip "Step 3" and proceed to "Step 4".


Step 3.
Go to GHUB (mouse device, "Assignments" screen).
Unbind "Back" from physical MB#4 (click and select DISABLE from the drop-down menu).
Disabled MB#4 will look like a white circle with black inside.


Step 4.
Set the script (see below).


Step 5.
Go to GHUB (mouse device, "Assignments" screen, SYSTEM tab).
Bind "Back" to your physical LMB.
You will see a warning about a potentially dangerous operation.
Allow this operation because you have the "spare LMB".

--Button used to turn rapid fire on/off
local rfLock = "scrolllock"

--Rapid fire values used in randomization/realism, in ms (milleseconds).
local rfLo = 45
local rfHi = 75

function OnEvent(event, arg)
   if event == "PROFILE_ACTIVATED" then
      EnablePrimaryMouseButtonEvents(true)
   elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
      PressMouseButton(1)
      if IsKeyLockOn(rfLock) then
         repeat
            Sleep(math.random(rfLo,rfHi))
            if not IsMouseButtonPressed(4) then break end
            ReleaseMouseButton(1)
            Sleep(math.random(rfLo,rfHi))
            PressMouseButton(1)
         until not IsMouseButtonPressed(4)  -- 4 = "Back"
      end
   elseif event == "MOUSE_BUTTON_RELEASED" and arg == 1 then
      ReleaseMouseButton(1)
   end
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...