-1

Im brand new at scripting and have no idea what im doing. Found the below code online and want it to make adjustments. So basically, in fallout 4 lets say a gun's recoil goes to up and to the left originally, but then halfway into shooting it goes to the right. I want the script to initially be able to pull the mouse down and to the right, and then (when the guns recoil starts to go in the other direction) go to the left. Is this possible?


EnablePrimaryMouseButtonEvents (true);

function OnEvent(event,arg)
   if IsKeyLockOn("numlock")then
      if IsMouseButtonPressed(3)then
         repeat
            if IsMouseButtonPressed(1) then
               repeat
                  MoveMouseRelative(-1,13)
                  Sleep(75)
               until not IsMouseButtonPressed(1)
            end
         until not IsMouseButtonPressed(3)
      end
   end
end

Above is a script I use to move my mouse down when pressing the shooting buttons in Fallout 4 because I have problems which prevent me from countering recoil.

I want the script to not only move down and slightly to the left (MoveMouseRelative(-1,13)) but I want to be able to specify that after a certain amount of time, I then want the script to move in a different direction that again, I can specify.

How would I do this? I believe this is a LUA script or something, and im using a logitech mouse

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
Jimmy Ban
  • 1
  • 1
  • 2

1 Answers1

0
EnablePrimaryMouseButtonEvents(true)

function OnEvent(event, arg)

   if event == "MOUSE_BUTTON_PRESSED" and arg == 1 and IsMouseButtonPressed(3) and IsKeyLockOn("numlock") then
      -- first group of moves: 10 steps in direction (-1,13), total group time = 750 ms
      for i = 1, 10 do
         MoveMouseRelative(-1,13)
         Sleep(75)
         if not IsMouseButtonPressed(1) then return end
      end
      -- second group of moves: 5 steps in direction (1,12), total group time = 375 ms
      for i = 1, 5 do
         MoveMouseRelative(1,12)
         Sleep(75)
         if not IsMouseButtonPressed(1) then return end
      end
      -- you can add more groups
   end

   if event == "MOUSE_BUTTON_PRESSED" and arg == 1 and IsMouseButtonPressed(3) and IsKeyLockOn("scrolllock") then
      -- first group of moves: 10 steps in direction (-1,13), total group time = 750 ms
      for i = 1, 10 do
         MoveMouseRelative(-1,13)
         Sleep(75)
         if not IsMouseButtonPressed(1) then return end
      end
      -- second group of moves: 5 steps in direction (1,12), total group time = 375 ms
      for i = 1, 5 do
         MoveMouseRelative(1,12)
         Sleep(75)
         if not IsMouseButtonPressed(1) then return end
      end
      -- you can add more groups
   end

end

UPDATE:

Step 0.
You are about to modify the behavior of Left Mouse Button.
This is a potentially dangerous operation: you can do almost 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 the big mouse picture in LGS and assign command "Left Click" to your physical MB#8.
Now, if something goes wrong and your LMB stops working, you can press MB#8 instead of LMB.


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

  • If YES (some action is set 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 key you don't currently use in the game
    (let's assume the F12 key is not currently used)
  • go to the big mouse picture in LGS and assign command F12 to your physical MB#4
  • go to your game settings and set the game action to F12 instead of MB#4

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


Step 3.
Go to the big mouse picture in LGS.
Unassign standard command "Back" from physical MB#4 (select "Unassign" from the drop-down menu).


Step 4.
Set the script (see below).


Step 5.
Go to the big mouse picture in LGS.
Assign command "Back" to your physical LMB.
You will see a warning about a potentially dangerous operation.
Allow this operation because you have the "spare LMB" if something goes wrong.


-- rapid fire (CAPSLOCK) is independent from anti-recoil (NUMLOCK and SCROLLLOCK)

local rapid_fire_interval = 30  -- milliseconds between LMB press/release simulation

local LMB_down, rapid_fire, prev_time, next_LMB_time

local function PressOrReleaseLMB(only_release)
   if LMB_down then
      ReleaseMouseButton(1)
      LMB_down = false
   elseif not only_release then
      PressMouseButton(1)
      LMB_down = true
   end
end

local function Sleep_with_rapid_fire(ms)
   -- returns true if LMB was released by user
   prev_time = prev_time + ms
   while GetRunningTime() < prev_time do
      Sleep(10)
      if not IsMouseButtonPressed(4) then
         return true
      end
      while rapid_fire and GetRunningTime() >= next_LMB_time then
         next_LMB_time = next_LMB_time + rapid_fire_interval
         PressOrReleaseLMB()  -- press LMB (if it's up) or release LMB (if it's down)
      end
   end
end

function OnEvent(event, arg)
   if event == "PROFILE_ACTIVATED" then
      EnablePrimaryMouseButtonEvents(true)
   elseif event == "PROFILE_DEACTIVATED" or event == "MOUSE_BUTTON_RELEASED" and arg == 1 then
      PressOrReleaseLMB(true)  -- release LMB
   elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
      PressOrReleaseLMB()  -- press LMB
      prev_time = GetRunningTime()
      next_LMB_time = prev_time + rapid_fire_interval
      rapid_fire = IsKeyLockOn("capslock")
      if IsMouseButtonPressed(3) then  -- RMB pressed
         if IsKeyLockOn("numlock") then

            -- first group of moves: 10 steps in direction (-1,13), total group time = 750 ms
            for i = 1, 10 do
               MoveMouseRelative(-1,13)
               if Sleep_with_rapid_fire(75) then return end
            end
            -- second group of moves: 5 steps in direction (1,12), total group time = 375 ms
            for i = 1, 5 do
               MoveMouseRelative(1,12)
               if Sleep_with_rapid_fire(75) then return end
            end
            -- you can add more groups

         elseif IsKeyLockOn("scrolllock") then

            -- first group of moves: 10 steps in direction (-1,13), total group time = 750 ms
            for i = 1, 10 do
               MoveMouseRelative(-1,13)
               if Sleep_with_rapid_fire(75) then return end
            end
            -- second group of moves: 5 steps in direction (1,12), total group time = 375 ms
            for i = 1, 5 do
               MoveMouseRelative(1,12)
               if Sleep_with_rapid_fire(75) then return end
            end
            -- you can add more groups

         end
      end
      Sleep_with_rapid_fire(math.huge)  -- until user released LMB
   end
end
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
  • What does the "for i = 1, 10 do" part mean? And how would I go about making changes? Like say For 2 seconds I would want the mouse to pull downward, and then after 2 seconds I want it to pull upward? – Jimmy Ban Dec 24 '20 at 11:37
  • `What does the "for i = 1, 10 do" part mean?` - It means "repeat 10 times". `10*75 = 750ms` If you need 2 seconds, try `27*75` – Egor Skriptunoff Dec 24 '20 at 11:45
  • `MoveMouseRelative(0,12)` - downward, `MoveMouseRelative(0,-12)` - upward – Egor Skriptunoff Dec 24 '20 at 11:47
  • I just wanna say I really appreciate the help and so for any dumb questions hahah! How would I make the script just continue the very last 'MoveMouseRelative' infinitely until I let go of the Mouse 1 and 3 buttons? In the original script I had the MoveMouseRelative just went on forever until I changed let go of mouse 1 and 3 which Is what I would also like to acheiev here – Jimmy Ban Dec 24 '20 at 11:52
  • `How would I make the script just continue the very last 'MoveMouseRelative' infinitely` - Set very big loop limit, for example, `for i = 1, 1000000 do` – Egor Skriptunoff Dec 24 '20 at 12:04
  • I've noticed while trying to tune it once I get to about the 3rd set of x,y cords for the mouse, the cursor starts to become very stuttery. I've even lowered the sleep to 1 and its still stuttery? why is this? – Jimmy Ban Dec 24 '20 at 12:27
  • For very short sleep duration use [FastSleep()](https://stackoverflow.com/q/65356366/1847592) – Egor Skriptunoff Dec 24 '20 at 12:34
  • But the stuttering was occuring regardless how long or short I put the sleep duration at – Jimmy Ban Dec 24 '20 at 12:38
  • Use `FastSleep(5)` and `MoveMouseRelative(1,1)` to reduce stuttering. – Egor Skriptunoff Dec 24 '20 at 12:40
  • Again, really appreciate the help. Would there be a way to add to the script so as to where I can 'activate' two different movements of the mouse by pressing 2 different buttons? So If i turn numlock on for example the mouse goes left, but turning off numlock and turning on scrolllock for example could pull the mouse left? -- the mouse movements themselves are just random but is there a way to do something like this? – Jimmy Ban Dec 25 '20 at 21:00
  • Yes, you can add one more independent `if event = ... end` block for scrolllock after existing block for numlock (both are inside single OnEvent function). – Egor Skriptunoff Dec 25 '20 at 22:51
  • Every time I try and add it, the scripts is saying that there is an error. Any chance you'd be able to edit the script you originally wrote with a new section that I can turn on seperately using the scrollock key? – Jimmy Ban Dec 26 '20 at 11:29
  • Thank you so much you've been a huge help! I It kept coming up saying there was a syntax error on the line with the 'end' no matter what I did. Wasnt sure why it wasnt working. What program do you use to edit this in? I've just been making changes through the logitech gaming software – Jimmy Ban Dec 26 '20 at 22:37
  • You can use any programming text editor to edit the script, and later copy-paste the code to the LGS script editor window. I use SciTE, but notepad++ is also good. – Egor Skriptunoff Dec 26 '20 at 23:49
  • Hi again,is there a way to make the original script I posted do two actions at the same time? so for example have MouseMoveRelative(-1,3) at Sleep(30) while ALSO have the mouse move (1,0) at Sleep(50)? ------------ – Jimmy Ban Jan 08 '21 at 01:10
  • Do you want to independently control both actions? – Egor Skriptunoff Jan 08 '21 at 03:56
  • Hey Egor, Thanks for the help with the script. I've recently moved over to controller but cant aim for crap. I was wondering if there was a way to basically use the same script but for a Ps4 controller plugged into my pc? --- So basically when I press the aim and shoot buttons at the same time they do the same thing as the no recoil script did when using keyboard and mouse? – Jimmy Ban Jun 07 '21 at 07:37
  • I never used it, but it is probably achievable with AutoHotKey software. Create new question and set `autohotkey` tag. – Egor Skriptunoff Jun 07 '21 at 09:44
  • Ahhh okay fair enough. Do you know how I would otherwise use the script you provided me originally to make when I hold the left mouse click in it would actually click the shoot button over and over and over until I let go of left mouse? – Jimmy Ban Jun 09 '21 at 21:59
  • `click the shoot button` - which button is the "shoot button"? – Egor Skriptunoff Jun 09 '21 at 22:03
  • Left mouse click held down – Jimmy Ban Jun 16 '21 at 06:05
  • It would be more simple to bind rapidfire to other button than LMB (to avoid collision between a button monitored and a button simulated). – Egor Skriptunoff Jun 16 '21 at 16:37
  • Well basically i want it to kick in similar to the other script you wrote. So for it to only rapid before when i hold down left mouse button? – Jimmy Ban Jun 17 '21 at 07:47
  • I do not understand what exactly you mean – Egor Skriptunoff Jun 17 '21 at 07:53
  • Okay basically like when I press and hold down the left mouse button, my hope would be that a script would be able to emulate me pressing the left mouse button over and over and over (while in reality im just holding the mouse button down). so that way in games with a semi-auto gun i can just press the left mouse button and it will essentially shoot like an automatic gun – Jimmy Ban Jun 17 '21 at 08:01
  • It would be more simple to implement rapidfire on another button. For example, while you press and hold MB#4 a script simulates pressing LMB repeatedly until you release MB#4. Is it OK for you? – Egor Skriptunoff Jun 17 '21 at 08:21
  • Unfortunately I'd need to be activate when the left mouse button is pressed and deactivate when its lets go. Is something like that not possible? – Jimmy Ban Jun 17 '21 at 08:26
  • ok, how to distinguish between usual LMB and rapidfire LMB? – Egor Skriptunoff Jun 17 '21 at 09:15
  • Im not sure. Wouldnt u be able to write something like when the LMB is held down it just emulates the LMB clicks over and over? Similar to how you would make a script would when holding down the left and right MB before in the original script – Jimmy Ban Jun 17 '21 at 10:43
  • OK, always rapidfire. The last question: LGS or GHUB? – Egor Skriptunoff Jun 17 '21 at 13:25
  • Yeah, one that works with my logitech mouse so LGS. really appreciate you helping with this. Btw, is there some sort of way to bascially implement this into the existing script you already made for me a while back that allowed for two movements of the mouse? – Jimmy Ban Jun 17 '21 at 23:52
  • `implement this into the existing script` - does this mean you want to turn rapidfire on only if both LMB and RMB are pressed similar to the existing script? – Egor Skriptunoff Jun 18 '21 at 12:47
  • Not necessarily. I think im meaning like, if CAPSLOCK is turned on, then in addition to the existing script you wrote for me before (with scrolllock and numlock pulling the mouse down), then when the left mouse button is held down it will automatically click the left mouse as fast as possible. – Jimmy Ban Jun 19 '21 at 04:50
  • Should rapidfire be able to work simultaneously with antirecoil? If yes, should mouse pulling speed be the same as usual with scrolllock and numlock? – Egor Skriptunoff Jun 19 '21 at 11:31
  • Hey again. Yeah, ideally the anti recoil would remain the same and be able to work independantly from the recoil stuff. maybe have it so i can press PAGE UP or something and that will give rapid fire, while then also allowing me to then seperately control the no recoil stuff with numlock and scroll lock as you've said. thanks again! – Jimmy Ban Jun 22 '21 at 23:35
  • One more question to simplify the solution. Each game has an ability to set an alternative key for the same action. Please try to set keyboard key (for example `P`) as the alternative for usual "shoot button" LMB. Make sure both of them work. After that please try the following: keep LMB pressed and then press `P`. Does `P` shoot when LMB is pressed? I expect you will get two shots: first from LMB and second from `P`. – Egor Skriptunoff Jun 24 '21 at 08:02
  • Okay ill give it a try – Jimmy Ban Jun 30 '21 at 12:47
  • I made P the shoot button but when I hold down LMB and press P the gun does not shoot – Jimmy Ban Jul 01 '21 at 01:44