Help with ScrCmp() please Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Help with ScrCmp() please

Post by wer » 01 Dec 2022, 18:37

hi there,
i found a script written by skan:
ScrCmp() : Detects changes in screen by comparing two screenshots
viewtopic.php?f=6&t=82812
the function is if screen change detected, do something
and my question is, if want
change occurred --do nothing
no change for a while --do some thing
how to achieve this with this script
please give me some guidance, thanks in advance!

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Help with ScrCmp() please

Post by mikeyww » 01 Dec 2022, 18:59

When you have a question about a post, you can always reply to the post itself.

One answer is below.

Code: Select all

#Persistent
SetTimer, NoChange, -3000
If changed := ScrCmp(100, 100, 200, 200,, 1000, 200) {
 SetTimer, NoChange, Off
 MsgBox, Do nothing!
}
Return

NoChange:
A_Args.ScrCmp.Wait := False
MsgBox, Do some thing!
Return

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: Help with ScrCmp() please

Post by wer » 01 Dec 2022, 19:46

mikeyww wrote:
01 Dec 2022, 18:59
When you have a question about a post, you can always reply to the post itself.

One answer is below.
thank you sir you so kind
i do replied and wait a long time, no answer, so

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Help with ScrCmp() please

Post by mikeyww » 01 Dec 2022, 20:33

OK. I had not seen it earlier. :thumbup:

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: Help with ScrCmp() please

Post by wer » 01 Dec 2022, 23:47

mikeyww wrote:
01 Dec 2022, 20:33
OK. I had not seen it earlier. :thumbup:
sir, could you tell me how to keep watching selected area please
i combine your code and region selector and ScrCmp, it run once, then stop
cant figure it out...

Code: Select all

#Persistent
#NoEnv
#Warn
#SingleInstance, Force
SetBatchLines, -1
CoordMode, Mouse, Screen

InputRect(vX1, vY1, vX2, vY2)

vW := vX2-vX1, vH := vY2-vY1
if (vInputRectState = -1)
	return
sleep, 200

SetTimer, NoChange, -3000
If changed := ScrCmp(vX1, vY1, vW, vH,, 1000, 200) {
 SetTimer, NoChange, Off
 msgbox,,, just keep watching, 1
}
Return

NoChange:
A_Args.ScrCmp.Wait := False
msgbox,,, gonna do some thing then keep watching, 1
Return

InputRect(ByRef vX1, ByRef vY1, ByRef vX2, ByRef vY2)
{
}

;==================================================

; Paste ScrCmp() below

ScrCmp( X, Y, W, H, Hwnd:=0x0, Sleep* )  
{
}

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Help with ScrCmp() please

Post by mikeyww » 02 Dec 2022, 06:29

My first question is whether my script works when run as is, with no other changes. That will tell you whether you are starting with a working script.

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: Help with ScrCmp() please

Post by wer » 02 Dec 2022, 06:40

mikeyww wrote:
02 Dec 2022, 06:29
My first question is whether my script works when run as is, with no other changes. That will tell you whether you are starting with a working script.
yes it works right, but just do detect once, not continuously

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Help with ScrCmp() please

Post by mikeyww » 02 Dec 2022, 07:39

Ideas for you:

Code: Select all

SetTimer, NoChange, 3000
Loop {
 changed := False
 If changed := ScrCmp(100, 100, 200, 200,, 1000, 200)
  Msgbox,,, just keep watching, 1
}
Return

NoChange:
If !changed
 MsgBox,,, Gonna do some thing then keep watching, 1
Return
You can also fine-tune this by stopping and restarting the timer if needed at any point.

You now have the tools to adjust. The function waits for a change. In the meantime, the timer keeps running. You can add code to either section.

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: Help with ScrCmp() please

Post by wer » 02 Dec 2022, 09:40

mikeyww wrote:
02 Dec 2022, 07:39
Ideas for you:

Code: Select all

SetTimer, NoChange, 3000
Loop {
 changed := False
 If changed := ScrCmp(100, 100, 200, 200,, 1000, 200)
  Msgbox,,, just keep watching, 1
}
Return

NoChange:
If !changed
 MsgBox,,, Gonna do some thing then keep watching, 1
Return
You can also fine-tune this by stopping and restarting the timer if needed at any point.

You now have the tools to adjust. The function waits for a change. In the meantime, the timer keeps running. You can add code to either section.
sorry to bother you sir, i tested both kind of area with this code,
for a unchanged area, it popup "Gonna do some thing then keep watching" per 3 seconds, seems fine, but
for a constantly changing area, it popup both "just keep watching"/"Gonna do some thing then keep watching" alternatively, seems not right

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Help with ScrCmp() please  Topic is solved

Post by mikeyww » 02 Dec 2022, 20:20

I already mentioned that you can stop and restart the timer.

Code: Select all

#SingleInstance Force
SoundBeep, 1500
Loop {
 SetTimer, NoChange, 3000
 ScrCmp(100, 100, 200, 200,, 250, 100)
 SetTimer, NoChange, Off
 Msgbox, 48, Changed, just keep watching, 2
}
NoChange:
MsgBox, 64, Unchanged, Gonna do some thing then keep watching, 2
Return
You may need to tune this further.

EDIT: I removed the duplicate line.

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: Help with ScrCmp() please

Post by wer » 05 Dec 2022, 04:09

mikeyww wrote:
02 Dec 2022, 20:20
I already mentioned that you can stop and restart the timer.



You may need to tune this further.

EDIT: I removed the duplicate line.
the settimer reset trick looks right in logic, but it works not completely right,
i use it to keep watching an area which does changed about per 10 seconds,
but it still run nochange label no matter how much scrcmp delay or settimer period i set.
cant figure out why, so i modified some code, now it seems fine:

Code: Select all

SetTimer, isazero, % timeout
Loop {
 MouseTip(x1, y1, width, height)
 ScrCmp(x1, y1, width, height,, between, 200)
 a++
 Msgbox, , , % "a=" a, 2
}

isazero() {
  if (a=0) {
    try {
      sendmail(ett)
    }
  } else a:=0
  Msgbox, , , % "a=" a, 2
}

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: Help with ScrCmp() please

Post by wer » 06 Dec 2022, 05:07

mikeyww wrote:
02 Dec 2022, 20:20
well, now seems i figure out why, the msgbox must be exist,
or after a while, things goes wrong
so i use tooltip in stead of msgbox for less interrupt
just let you know, sir

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Help with ScrCmp() please

Post by mikeyww » 06 Dec 2022, 06:08

This must be the part about the fine tuning.... 8-)

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: Help with ScrCmp() please

Post by wer » 07 Dec 2022, 04:56

mikeyww wrote:
06 Dec 2022, 06:08
This must be the part about the fine tuning.... 8-)
by now, seems there must be a msgbox after scrcmp statement,
or things goes wrong soon or later
through my tests, show the tooltip did not work, must be msgbox
still want to find a way could keep scrcmp works right and with less interrupt

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Help with ScrCmp() please

Post by mikeyww » 07 Dec 2022, 05:32

The timing is likely to matter.
Sleep1 is duration (in ms) to wait between consecutive screen shots. When a screen change is detected, an additional screen shot is taken to confirm that change didn't occur because of a flicker. Sleep2 is duration (in ms) to wait between a regular screen shot and the additional confirmation screen shot.
My script disables the timer, but you have decided not to do that. This means that the timer in your script will continue to run.

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: Help with ScrCmp() please

Post by wer » 07 Dec 2022, 08:00

mikeyww wrote:
07 Dec 2022, 05:32
The timing is likely to matter.
Sleep1 is duration (in ms) to wait between consecutive screen shots. When a screen change is detected, an additional screen shot is taken to confirm that change didn't occur because of a flicker. Sleep2 is duration (in ms) to wait between a regular screen shot and the additional confirmation screen shot.
My script disables the timer, but you have decided not to do that. This means that the timer in your script will continue to run.
oh, now the question is, if the scrcmp statement does not followed by a msgbox command, i.e. totally background commands
things will go wrong after a while inevitably, so i mean, your method or my method are both right on themselves
all i know is msgbox could avoid error occurs, maybe it is because of i run this script on a pc without monitor, till now i cant figure out why

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Help with ScrCmp() please

Post by mikeyww » 07 Dec 2022, 09:03

A timed cycle is repeated only after the previous cycle is completed. A MsgBox interrupts the cycle, because the script will not continue until the MsgBox is resolved.

Like I said, you need to fix the timing, and consider whether you want to disable the timer as well.

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: Help with ScrCmp() please

Post by wer » 07 Dec 2022, 22:43

mikeyww wrote:
07 Dec 2022, 05:32
The timing is likely to matter.
Sleep1 is duration (in ms) to wait between consecutive screen shots. When a screen change is detected, an additional screen shot is taken to confirm that change didn't occur because of a flicker. Sleep2 is duration (in ms) to wait between a regular screen shot and the additional confirmation screen shot.
My script disables the timer, but you have decided not to do that. This means that the timer in your script will continue to run.
thanks for the reply, sir
sorry i didnt make it clear before
the thing like this,
if i dont leave a msgbox here
you code:

Code: Select all

Loop {
 MouseTip(x1, y1, width, height)
 SetTimer, NoChange, % timeout
 ScrCmp(x1, y1, width, height,, between, 200)
 SetTimer, NoChange, Off
 ;Msgbox, 48, Changed, just keep watching, 2
}
NoChange:
try {
 no change thing
}
or my code:

Code: Select all

SetTimer, isazero, % timeout
Loop {
 MouseTip(x1, y1, width, height)
 ScrCmp(x1, y1, width, height,, between, 200)
 a++
 ;Msgbox, , , % "a=" a, 1
}

isazero() {
  if (a=0) {
    try {
      no change thing
    }
  } else a:=0
}
they both worked at the beginning, lets say, maybe the first hour or half a hour,
then they start to do "no change thing" again and again which they should not to
(consider the specific area still keep changing), to adjust the timer couldnt work in this case
add the msgbox command back, the script back to normal, remove the msgbox command, things will go wrong after a while, no matter yours or mine, both couldnt work without msgbox
so i want to find a more silent way, maybe like replace the msgbox command with some background command if possible, unfortunately the tooltip couldnt work, dont know if there is another way could

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Help with ScrCmp() please

Post by mikeyww » 08 Dec 2022, 04:11

Since the MsgBox creates a two-second delay, you can try a sleep for that duration.

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: Help with ScrCmp() please

Post by wer » 09 Dec 2022, 03:54

mikeyww wrote:
08 Dec 2022, 04:11
Since the MsgBox creates a two-second delay, you can try a sleep for that duration.
tried, sadly still cant replace the msgbox
also try to replace msgbox command with the command below, consider the script run on a pc without monitor, nothing improved, strange

Code: Select all

SendMessage, 0x0112, 0xF170, 2,, Program Manager

Post Reply

Return to “Ask for Help (v1)”