Mouse automation program

Post your working scripts, libraries and tools for AHK v1.1 and older
lavaicecube

Mouse automation program

04 Jul 2015, 21:12

I wrote a simple program to keep my computer from going idle. It uses a loop function to randomly move the mouse around. I want to combine this program with another program that automatically shuts down my computer on a timer but it only moves the mouse and won't continue on to the rest of the code until the loop is complete. Any thoughts:

Code: Select all

WinGetPos,,, desk_width, desk_height, Program Manager
Loop
{
   Random, MySleepTime, 1000, 4500
   Random, posx, 1, desk_width
   Random, posy, 1, desk_height
   Random, MySpeed, 20, 100

   MouseMove, posx, posy, MySpeed

   ;msgbox %MySleepTime%
   ;Sleep, MySleepTime

   If Esc
      break

}
Esc::ExitApp
ExitApp
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Mouse automation program

05 Jul 2015, 15:11

Can you share the other code you are trying to incorporate?

Also, the if Esc...break isn't needed and doesn't do anything. The Esc hotkey will get triggered whether that statement is there or not. Your if statement is just checking for the value of a variable named Esc to have the value of 1, and your code never assigns a value to it. That is not how you check whether a key is pressed.

And you have an extra ExitApp command. The second one will never be executed.

(By the way, this thread should be in the Ask for Help section. Scripts and Functions is meant for sharing complete scripts, not for looking for help on scripts.)
lavaicecube

Re: Mouse automation program

05 Jul 2015, 18:33

Hi, thanks for replying and helping me with this code. Sorry that I posted this question in the wrong area, I'm new to the forum.

This is the other code that I'm trying to incorporate the idle function into. It's not mine, I found it here on the forums. My goal is to make a program that keeps my computer from going idle with mouse movement and at the same time uses a timer to execute a shutdown at a set time. Here is the other code:

Code: Select all

#Persistent
#SingleInstance, Force
#NoEnv
#NoTrayIcon

ListLines, Off
SetBatchLines,-1

ScriptName = System Timer

;------( Gui )----------------------------------------------------------------

#SingleInstance,force
#SingleInstance,force
Gui,Add,Text,x18 y141 w410 h13 Center,Your chosen action will be executed in
Gui,Font,norm s40,Lucida Console
Gui,Add,Text,x15 y164 w410 h61 vTimer_Update Center,0:30:00
Gui,Font
Gui,Add,Button,x20 y231 w90 h23 gTimer_Start,Start
Gui,Add,Button,x120 y231 w90 h23 gTimer_Pause,Pause
Gui,Add,Button,x220 y231 w90 h23 gTimer_Stop,Stop
Gui,Add,Button,x320 y231 w90 h23 gTimer_Exit,Exit
Gui,Font,norm s20,Arial
Gui,Add,Text,x80 y3 w300 h50,Auto Idle and Shutdown
Gui,Font,norm bold c0xFF0000 s12,Arial
Gui,Add,Text,x139 y38 w209 h22,Press escape to exit
Gui,Font
Gui,Add,Groupbox,x10 y57 w420 h76,Set Timer:
Gui,Add,Edit,x21 y76 w399 h20 vTimer_Minutes gTimer_Preview,30
Gui,Add,DropDownList,x20 y103 w400 vTimer_Action,Sleep|Hibernate|Reboot|Shutdown|
Gui,Show,x718 y275 w440 h274 ,

return


;------( Convert input into HH:MM:SS for prevew

Timer_Preview:
	Gui, Submit, Nohide
	If !! Timer
	{
		SetTimer, Timer_Start, Off
		MsgBox, ,% ScriptName, Shutdown Timer Stopped. Restart timer with new time
		Timer=
	}
	Timer_Update:="", Timer_UpdateLast:="", TimeSet:="", Flash:="", PauseTime:=""
	SetFormat, Float, 0.0
	TimeSet:=A_TickCount, TimeSet:=FormatSeconds(((Timer_Minutes*60*1000) - (A_TickCount - TimeSet))/1000)
	If TimeSet = :00:00
		GuiControl, Text, Timer_Update, 0:00:00
	Else
		GuiControl, Text, Timer_Update, % TimeSet
Return

;------( Start

Timer_Start:
	If Pause
	{
		GoSub, Timer_Pause
		Return
	}
	If ! TimerRunning ; Whether the Timer is ON
	{
		Gui, Submit, Nohide
		If (!Timer_Minutes || !Timer_Action)
		{
			SetTimer, Timer_Start, Off
			MsgBox, ,% ScriptName, No time or action input.
		}
		Else
		{
			TimeSet:=A_TickCount
			Timer_Minutes:=(Timer_Minutes * 60) * 1000
			SetTimer, Timer_Start, 50
			TimerRunning := 1
		}
	}
	Else
	{
		SetFormat, Float, 0.0

		Timer_Update:=FormatSeconds((Timer_Minutes - (A_TickCount - TimeSet))/1000)

		If (Timer_Update="" || Timer_Update <> Timer_UpdateLast)
		{
			GuiControl, Text, Timer_Update, % Timer_Update
			Timer_UpdateLast:=Timer_Update
		}
		If % (A_TickCount - TimeSet) > Timer_Minutes
		{
			SetTimer, Timer_Start, Off

			If Timer_Action=Sleep
				DllCall("PowrProf\SetSuspendState", "int", 0, "int", 1, "int", 1)
			If Timer_Action=Hibernate
				DllCall("PowrProf\SetSuspendState", "int", 1, "int", 1, "int", 1)
			If Timer_Action=Reboot
				Shutdown, 6
			If Timer_Action=Shutdown
				Shutdown, 5
		}

		; Flash Window
		If (Timer_Minutes - (A_TickCount - TimeSet))/1000 < 60
		{
			If ! Flash
				WinSet, Alwaysontop, On, % ScriptName
			If (Flash="" || (A_TickCount-Flash) > 400)
			{
				Gui, Flash
				Flash:=A_TickCount
			}
		}
	}
Return

;------( Pause

Timer_Pause:
	If % Pause:= (Pause = "") ? "1" : ""
	{
		SetTimer, Timer_Start, Off
		PauseTime:=A_TickCount
	}
	Else
	{
		TimeSet:=TimeSet+(A_TickCount-PauseTime), PauseTime:=""
		SetTimer, Timer_Start, 50
	}
Return

;------( Stop

Timer_Stop:
	Reload
Return

;------( Exit

Timer_Exit:
	ExitApp
Return

;------( Functions )----------------------------------------------------------

FormatSeconds(NumberOfSeconds) {
	Time = 19990101
	Time += %NumberOfSeconds%, seconds
	FormatTime, mmss, %time%, mm:ss
	Return NumberOfSeconds//3600 ":" mmss
}
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Mouse automation program

05 Jul 2015, 22:43

Insert all of the first code except for the Esc::ExitApp before the first return statement in the second code (after the Gui, Show), and insert the Esc::ExitApp after the return statement.
lavaicecube

Re: Mouse automation program

15 Jul 2015, 19:05

Thank you for the help. That does work, however, I only want to mouse to begin when I click the start button.
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Mouse automation program

15 Jul 2015, 20:16

sorry, but what is the 'start' button? :oops: :morebeard:
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: Mouse automation program

17 Jul 2015, 09:42

Guest10 wrote:sorry, but what is the 'start' button? :oops: :morebeard:
The auto-idle script he linked creates a GUI, and one of the buttons is labelled "Start". I guess he means that. Or he could mean the Start button at the bottom left of the windows desktop, but I doubt it.
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: Mouse automation program

17 Jul 2015, 09:54

Here you go, here is the mouse wiggle integrated into the shutdown app:

Code: Select all

#Persistent
#SingleInstance, Force
#NoEnv
#NoTrayIcon
 
ListLines, Off
SetBatchLines,-1
 
ScriptName = System Timer
TimerRunning := 0

;------( Gui )----------------------------------------------------------------
 
#SingleInstance,force
#SingleInstance,force
Gui,Add,Text,x18 y141 w410 h13 Center,Your chosen action will be executed in
Gui,Font,norm s40,Lucida Console
Gui,Add,Text,x15 y164 w410 h61 vTimer_Update Center,0:30:00
Gui,Font
Gui,Add,Button,x20 y231 w90 h23 gTimer_Start,Start
Gui,Add,Button,x120 y231 w90 h23 gTimer_Pause,Pause
Gui,Add,Button,x220 y231 w90 h23 gTimer_Stop,Stop
Gui,Add,Button,x320 y231 w90 h23 gTimer_Exit,Exit
Gui,Font,norm s20,Arial
Gui,Add,Text,x80 y3 w300 h50,Auto Idle and Shutdown
Gui,Font,norm bold c0xFF0000 s12,Arial
Gui,Add,Text,x139 y38 w209 h22,Press escape to exit
Gui,Font
Gui,Add,Groupbox,x10 y57 w420 h76,Set Timer:
Gui,Add,Edit,x21 y76 w399 h20 vTimer_Minutes gTimer_Preview,30
Gui,Add,DropDownList,x20 y103 w400 vTimer_Action,Sleep|Hibernate|Reboot|Shutdown|
Gui,Show,x718 y275 w440 h274 ,
 
return
 
 
;------( Convert input into HH:MM:SS for prevew
 
Timer_Preview:
	Gui, Submit, Nohide
	If !! Timer
	{
		SetTimer, Timer_Start, Off
		MsgBox, ,% ScriptName, Shutdown Timer Stopped. Restart timer with new time
		Timer=
	}
	Timer_Update:="", Timer_UpdateLast:="", TimeSet:="", Flash:="", PauseTime:=""
	SetFormat, Float, 0.0
	TimeSet:=A_TickCount, TimeSet:=FormatSeconds(((Timer_Minutes*60*1000) - (A_TickCount - TimeSet))/1000)
	If TimeSet = :00:00
		GuiControl, Text, Timer_Update, 0:00:00
	Else
		GuiControl, Text, Timer_Update, % TimeSet
Return
 
;------( Start
 
Timer_Start:
	If Pause
	{
		GoSub, Timer_Pause
		Return
	}
	If ! TimerRunning ; Whether the Timer is ON
	{
		Gui, Submit, Nohide
		If (!Timer_Minutes || !Timer_Action)
		{
			SetTimer, Timer_Start, Off
			MsgBox, ,% ScriptName, No time or action input.
		}
		Else
		{
			TimeSet:=A_TickCount
			Timer_Minutes:=(Timer_Minutes * 60) * 1000
			SetTimer, Timer_Start, 50
			TimerRunning := 1
            SetTimer, Wiggle_Start, -0
		}
	}
	Else
	{
		SetFormat, Float, 0.0
 
		Timer_Update:=FormatSeconds((Timer_Minutes - (A_TickCount - TimeSet))/1000)
 
		If (Timer_Update="" || Timer_Update <> Timer_UpdateLast)
		{
			GuiControl, Text, Timer_Update, % Timer_Update
			Timer_UpdateLast:=Timer_Update
		}
		If % (A_TickCount - TimeSet) > Timer_Minutes
		{
			SetTimer, Timer_Start, Off
 
			If Timer_Action=Sleep
				DllCall("PowrProf\SetSuspendState", "int", 0, "int", 1, "int", 1)
			If Timer_Action=Hibernate
				DllCall("PowrProf\SetSuspendState", "int", 1, "int", 1, "int", 1)
			If Timer_Action=Reboot
				Shutdown, 6
			If Timer_Action=Shutdown
				Shutdown, 5
		}
 
		; Flash Window
		If (Timer_Minutes - (A_TickCount - TimeSet))/1000 < 60
		{
			If ! Flash
				WinSet, Alwaysontop, On, % ScriptName
			If (Flash="" || (A_TickCount-Flash) > 400)
			{
				Gui, Flash
				Flash:=A_TickCount
			}
		}
	}
Return
 
;------( Pause
 
Timer_Pause:
	If % Pause:= (Pause = "") ? "1" : ""
	{
		SetTimer, Timer_Start, Off
		PauseTime:=A_TickCount
        TimerRunning := 0
	}
	Else
	{
		TimeSet:=TimeSet+(A_TickCount-PauseTime), PauseTime:=""
		SetTimer, Timer_Start, 50
        TimerRunning := 1
	}
Return
 
;------( Stop
 
Timer_Stop:
   TimerRunning := 0
   Reload
Return
 
;------( Exit
 
Timer_Exit:
	ExitApp
Return

Wiggle_Start:
   WinGetPos,,, desk_width, desk_height, Program Manager
   Loop
   {
      if (!TimerRunning)
      {
         break
      }
      Random, MySleepTime, 1000, 4500
      Random, posx, 1, desk_width
      Random, posy, 1, desk_height
      Random, MySpeed, 20, 100

      MouseMove, posx, posy, MySpeed
   }
   return

Esc::
   Gosub, Timer_Stop
   return

;------( Functions )----------------------------------------------------------
 
FormatSeconds(NumberOfSeconds) {
	Time = 19990101
	Time += %NumberOfSeconds%, seconds
	FormatTime, mmss, %time%, mm:ss
	Return NumberOfSeconds//3600 ":" mmss
}
I would, however, recommend not using the Reload command to stop the timer. If the user moves the window somewhere, when you hit Esc, the window moves back to the default position.
I suppose you could store the window location in an INI file or something, but I would recommend trying to code it that you can stop the timer without reloading the whole script.
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: Mouse automation program

17 Jul 2015, 09:58

Also, I think the mouse movement is a bit over-done.
You only really need to move the mouse left/right by 1px surely to fool the OS into thinking that there is activity.

By randomly moving the mouse so much, you can change what is on the screen (eg if it goes over the task bar, the screen can go into "Aero Peek" mode) and also you make it impossible to use the mouse to click Stop.

I would recommend changing the mouse movement to a relative move of +/- 1px; that way the user still has control of the mouse to a large degree.

I would recommend instead, that each time you move the mouse, move it 1px left, then 1px right very quickly. That way, the net effect should be no mouse movement (Unless the mouse is at one of the screen edges) - the user would just see an occasional slight wobble.
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Mouse automation program

17 Jul 2015, 11:44

Thanks, I'll take it for a test ride. :)

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: tidbit and 112 guests