Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Looping script to move the mouse up and down


  • Please log in to reply
25 replies to this topic
Centigrade
  • Members
  • 11 posts
  • Last active: Jan 17 2015 04:44 PM
  • Joined: 14 Jan 2015

Every few seconds, I would like the mouse to move up and then back down to the original position. This needs to repeat every few seconds, and it needs to toggle with a key press. How to do this?



GEV
  • Members
  • 1364 posts
  • Last active:
  • Joined: 23 Oct 2013
toggle := 0

F1::
MouseGetPos, MouseX, MouseY
if toggle := !toggle
 gosub, MoveTheMouse
else
 SetTimer, MoveTheMouse, off
return

MoveTheMouse:
MouseMove, 200, 200
SetTimer, MoveTheMouse, -3000  ; every 3 seconds 
MouseMove, %MouseX%, %MouseY%
return


wuvixx
  • Members
  • 140 posts
  • Last active: Mar 12 2015 10:31 AM
  • Joined: 15 Sep 2014

The code which GEV posted works, but the mouse moves over to the left side as well. If you want the mouse to move straight up, then you'll not change the x-axis. Replace with this: MouseMove, %MouseX%, 200


“Je suis Charlie”


Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

A bit different of an approach to GEV's.

toggle:=0
direction:=0
 
F6::
If toggle:=!toggle
SetTimer, MM, 3000
else
SetTimer, MM, Off
return
 
MM:
If direction := !direction
MouseMove, 0, 200,, R
else
MouseMove, 0, -200,, R
return


Centigrade
  • Members
  • 11 posts
  • Last active: Jan 17 2015 04:44 PM
  • Joined: 14 Jan 2015

Is there a way to combine this with a simple toggle autoclicker so that both functions toggle with the same keybind? i.e., 

 

And is there a way to set exactly how much the mouse moves in terms of pixels or something? 

For context, I am trying to look slightly up and then slightly down again in minecraft, to aggro endermen by looking at them and looking away.

i:=0 
F8::SetTimer, Spam, % (i:=!i) ? "100" : "Off" ; uses ternary

Spam: 
   Click
   Send a
return


Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

Sure. You can run two timers simultaneously. So just add a second timer to one of our functions. If you want it to go up, quickly go back down, and then wait before going up and down again, then both mine and GEV's code isn't what you wanted. You'd want something like this:

 

MM:
MouseMove, 0, 200,,R
Sleep 50
MouseMove, 0, -200,,R

 

I encourage you to try your hand at it. (And maybe someone more versed in ternary could teach me if you could trigger two SetTimers using the one variable (i) and ternary? Just repeating the line should mean you activate the first and disable the second, as the value of i will go from 0 to 1, then 1 to 0.)



Centigrade
  • Members
  • 11 posts
  • Last active: Jan 17 2015 04:44 PM
  • Joined: 14 Jan 2015

Sure. You can run two timers simultaneously. So just add a second timer to one of our functions. If you want it to go up, quickly go back down, and then wait before going up and down again, then both mine and GEV's code isn't what you wanted. You'd want something like this:

MM:
MouseMove, 0, 200,,R
Sleep 50
MouseMove, 0, -200,,R

I encourage you to try your hand at it. (And maybe someone more versed in ternary could teach me if you could trigger two SetTimers using the one variable (i) and ternary? Just repeating the line should mean you activate the first and disable the second, as the value of i will go from 0 to 1, then 1 to 0.)

How would I put this right here on a hotkey toggle to spam? I can just use a separate script from my autoclicker, with a separate hotkey. Trying to fold this into my autoclicker results in the desired up-and-down behaviour, but for some reason it will sometimes fail to go back down resulting in looking straight up within a minute or so.



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

You may need to increase the sleep.

 

And to put it in with your spam a toggle, check this idea out:

 

F8::
SetTimer, Label1, 1000
SetTimer, Label2, 300
return
 
Label1:
Send a
return
 
Label2:
Send b
return

 

Run that code to see how that works, and adopt the idea into your own script.

 

If you really want the scripts to be separate and you want two scripts running at the same time, you could try the ~ modifier. This is a "pass through" modifier, so AHK doesn't completely intercept the keystroke but will let other programs, which I believe means other AHK scripts, to receive it. (You may need to put this modifier on both scripts because you may not be able to well control which script will receive the keystroke first. If it's one without a pass through, then the other script won't ever receive the keystroke.)



Centigrade
  • Members
  • 11 posts
  • Last active: Jan 17 2015 04:44 PM
  • Joined: 14 Jan 2015

I ended up with the following, which still creeps (more slowly than the previous iteration) upward through repetition and does not seem to be able to toggle. Is there perhaps a way to define a desired initial mouse position and have it reset to that every repetition, instead of just looking up 50 then down 50? That might be able to fix the drift. Also need a toggle.

i:=0 
F8::
 SetTimer, Look, 300
 SetTimer, Spam, 100
return

Look:
 MouseMove, 0, 50,,R
 Sleep 1000
 MouseMove, 0, -50,,R
 send a
return

Spam:
 Click
 send b
return


TravisQ
  • Members
  • 86 posts
  • Last active: Jun 26 2015 05:39 PM
  • Joined: 25 Oct 2014
F8::goto % (t:=!t) ? "start" : "stop"
start:
	SetTimer,look,-1
	SetTimer,spam,-1
  return
stop:
	SetTimer,look,Off
	SetTimer,spam,Off
  return
look:
	SetTimer,look,300
	MouseMove, 0, 50,, R
	sleep,1000
	MouseMove, 0, -50,, R
	send,a
  return
spam:
	SetTimer,spam,100
	Click
	send,b
  return
Esc::ExitApp


Centigrade
  • Members
  • 11 posts
  • Last active: Jan 17 2015 04:44 PM
  • Joined: 14 Jan 2015

Still drifts over time. Could the issue be that I am playing in a maximized window rather than fullscreen? Or that I am using multiple monitors?



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

That may be an issue. You can try setting at the top of your script in the auto-execute section CoordMode, Mouse, Screen.

 

But I encourage you to try out a solution similar to GEV's. He uses MouseGetPos to get your current position, moves the mouse with MouseMove, then moves the mouse again but uses the coordinates retrieved in MouseGetPos.



bigdeoz
  • Members
  • 148 posts
  • Last active: Feb 04 2015 03:43 PM
  • Joined: 06 Sep 2013
Hi,

Let s clarify a little (i m not used to minecraft, sorry xD), u want the script to slowly move y axis up then fast down to starting position? Also by toggling it with a key, u mean u want to keep pressing it or use it like a caps lock?

Centigrade
  • Members
  • 11 posts
  • Last active: Jan 17 2015 04:44 PM
  • Joined: 14 Jan 2015

OK, let's start over. From start to finish, this is what I want to happen.

 

I would like to put my mouse at a starting position, let's call this position A.

 

I would then like to press a button, say F8, to begin a script which does the following while running:

 

• At all times, spam the left click at least once per second.

• While running, alternate between the following:

. ◘ After some duration, let's say 5 seconds, move the mouse 20 pixels relative to Position A (let's call this Position B).

. ◘ After some duration, let's say 2 seconds, move the mouse back to Position A.

 

I would like to be able to pause the script, without exiting it, by pressing the same keybind which started it, and be able to resume it from the start by pressing it again; i.e., I would like a toggle to reset the script. The repeating mouse movements should not cause the mouse cursor (or in my case, the first-person camera position) to drift over time; i.e., after 500 repetitions the starting Position A and the resulting Position B should still be the same pixels. My issue with the scripts I have tried is that the mouse will, after several repetitions, drift away from its starting position.

Perhaps a script could be used to identify the mouse's current position with a keypress, store that X.Y value, and then use that stored X,Y value (Position A) as a way to make sure the mouse always returns to the desired starting position with every repetition? 

 

Hi,

Let s clarify a little (i m not used to minecraft, sorry xD), u want the script to slowly move y axis up then fast down to starting position? Also by toggling it with a key, u mean u want to keep pressing it or use it like a caps lock?

 

Minecraft uses a first-person perspective view with a cross-hair in the middle of the screen; think of any First Person Shooter game as an example. Mouse movements change where the player is aiming and looking. There is an enemy which becomes hostile when a player looks at it, but does not move toward the player to attack until the player looks away. I am wanting to use this script to look at that enemy at Position A for a couple of seconds, then look away to Position B for a couple of seconds, all the while spamming the attack button (default: click). I know what motions I want the script to simulate, because I have simplified what I need to do to activate and kill these enemies en masse when I am at the computer, but would like to automate it so that I do not have to physically click and move my mouse dozens of times per minute.

 

This is basically an AFK experience grinding script. 



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

toggle:=0
CoordMode, Mouse, Screen
 
F8::
toggle!:=toggle
If toggle
{
SetTimer, Clicker, 1000
SetTimer, Mover, 7100
MouseGetPos, X, Y ; Not in the Mover Timer because the drift could be caused by resetting these values constantly, but the issue you describe isn't fully normal
}
If !toggle
{
SetTimer, Clicker, Off
SetTimer, Move, Off
}
return
 
Clicker:
Click
return
 
Mover:
MouseMove, 0, 20,,R
Sleep 2000
MouseMove, %X%, %Y%
return

 

How does this work for you?