How to check if Clipboard image changed? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scr1pter
Posts: 1275
Joined: 06 Aug 2017, 08:21
Location: Germany

How to check if Clipboard image changed?

27 Aug 2018, 12:43

Hello,

I would like to create a script which takes a screenshot and compares if it's the same as the Clipboard content.
By checking the AHK docs, I found this command:

OnClipboardChange
https://autohotkey.com/docs/commands/On ... Change.htm

Even though it looks promising, I achieved nothing with it.
I'm not even sure if I understood its functionality at all.

What I also tried was using a variable which gets the content of the Clipboard.
E.g.

Code: Select all

F4::
Send !{PrintScreen} ; Take a screenshot
Sleep, 100
a := Clipboard ; Assign variable to Clipboard
Sleep, 100
Clipboard = ; Clear Clipboard
Sleep, 100
Clipboard := a ; Clipboard = Variable
return
After pressing Ctrl+V nothing happened, so I assume I cannot use variables to capture screenshots.
Probably this is only useful for text.

Any idea?

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: How to check if Clipboard image changed?

27 Aug 2018, 12:55

Replace a := Clipboard to a := ClipboardAll.

Code: Select all

F4::
	Clipboard := ""
	Send !{PrintScreen} ; Take a screenshot
	ClipWait,, 1

	a := ClipboardAll ; Assign variable to Clipboard
	Sleep, 100
	Clipboard = ; Clear Clipboard
	Sleep, 100
	Clipboard := a ; Clipboard = Variable

	ToolTip, done! Try pressing ctrl+v in mspaint
return
User avatar
Scr1pter
Posts: 1275
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: How to check if Clipboard image changed?

27 Aug 2018, 15:40

Thank you, this did work.

However, I'm still not able to fix my problem.
By pressing F4 and after that F5, I expected to read the MsgBox.

Code: Select all

#SingleInstance force
;ClipboardImageTest
global a

F4::
Clipboard := ""
Send !{PrintScreen} ; Take a screenshot
ClipWait,, 1
a := ClipboardAll ; Assign variable to Clipboard
Sleep, 100
return

F5::
Send !{PrintScreen} ; Take a screenshot
Sleep, 100
ClipWait,, 1
if Clipboard = %a% ; If Clipboard is the same as a
{
  MsgBox, yes it's the same!
}
return
I also tried if ClipboardAll = %a%, and I also removed the global a.

What must I do?

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: How to check if Clipboard image changed?

27 Aug 2018, 23:15

AutoHotkey Help wrote:Variables to which ClipboardAll has been assigned can be compared to each other (but not directly to ClipboardAll) by means of the <> and = operators.

Code: Select all

#SingleInstance force

F4::
	Clipboard := ""
	Send !{PrintScreen} ; Take a screenshot
	ClipWait,, 1
	a := ClipboardAll ; Assign variable to Clipboard
	Sleep, 100
return

F5::
	Clipboard := ""
	Send !{PrintScreen} ; Take a screenshot
	ClipWait,, 1
	b := ClipboardAll
	if (b = a)
		MsgBox, yes it's the same!
return
Also note that it might failed if the window you are taking screenshot has cursor blinking.
User avatar
Scr1pter
Posts: 1275
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: How to check if Clipboard image changed?

28 Aug 2018, 17:15

Hi and thanks for your support!

I tested the script (exactly as you wrote it).
Unfortunately, it always tells me that it's the same.

I'm in Notepad++ and press F4.
I go to Firefox and press F5, and the MsgBox appears.

After that, I added these lines to check if the Clipboard contents are correct:

Code: Select all

F6::
Clipboard := a
return

F7::
Clipboard := b
return
When I take screenshots with F4 and F5,
F6 and F7 show me the correct screenshots.
Pressing F6 and then Ctrl+V in IrfanView = Screenshot I took with F4.
For F7 it's the F5 screenshots.

Maybe the if statement is wrong/inacurrate?

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to check if Clipboard image changed?

29 Aug 2018, 01:49

Edit: This post contains misinformation, see next post.
I think the docs of clipboardall is misleading, = will only compare up to the first binary zero, you can do memcmp instead,

Code: Select all

clipcmp(byref clp1, byref clp2){
	static wa := a_isunicode ? 2 : 1 
	local
	s1 := strlen(clp1)
	s2 := strlen(clp2)
	if (s1 != s2)
		return false
	return !memcmp(&clp1, &clp2, s1 * wa)
}
memcmp(buf1, buf2, count){
	; https://msdn.microsoft.com/en-us/library/zyaebf12.aspx
	return dllcall("MSVCRT.dll\memcmp", "ptr", buf1, "ptr", buf2, "ptr", count, "cdecl int")
}
Cheers.
Last edited by Helgef on 29 Aug 2018, 02:10, edited 1 time in total.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: How to check if Clipboard image changed?

29 Aug 2018, 02:07

AutoHotkey Help wrote:Variables to which ClipboardAll has been assigned can be compared to each other (but not directly to ClipboardAll) by means of the <> and = operators. In the following example, the length of each variable is checked first. If that is not enough to make the determination, the contents are compared to break the tie:

Code: Select all

if ClipSaved1 <> %ClipSaved2%  ; This must be an old-style IF statement, not an expression.
    MsgBox The two saved clipboards are different.
ahh, I didn't notice the sentence "This must be an old-style IF statement, not an expression."

So change to:

Code: Select all

if b = %a%
	MsgBox, yes it's the same!
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to check if Clipboard image changed?

29 Aug 2018, 02:08

Ah, very good. (In v2, clipboardall is replaced by clipboardall() which has less limitations, and == does memcmp in any expression(edit: when comparing strings and binary values )
Last edited by Helgef on 29 Aug 2018, 02:43, edited 2 times in total.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: How to check if Clipboard image changed?

29 Aug 2018, 02:19

Interesting. I like v2 and h, but I am not ready or have time to switch.
User avatar
Scr1pter
Posts: 1275
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: How to check if Clipboard image changed?

30 Aug 2018, 11:01

It works, that's really great!
Only a small issue:
It seems Alt+Screenshot can mess up the command bar in some programs.
In Notepad++ for example "Makro" is missing for no reason.
Image

This doesn't happen always, but sometimes.

Do you know a way to take a screenshot of the active window without Alt+PrintScreen?
I googled, but couldn't find anything yet.
Ctrl+Screenshot is not the same.

When I use the normal screenshot, which captures everything, the problem doesn't occur.
However, a normal screenshot might have problems like changing time in windows taskbar etc.
So I prefer capturing the active window only.

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: How to check if Clipboard image changed?  Topic is solved

30 Aug 2018, 11:57

Scr1pter wrote:Do you know a way to take a screenshot of the active window without Alt+PrintScreen?
You can use GDIP.

Code: Select all

pToken := Gdip_Startup()

; pBitmap := Gdip_BitmapFromHWND( WinExist("A") )
WinGetPos, x, y, w, h, A
pBitmap := Gdip_BitmapFromScreen(x "|" y "|" w "|" h)
Gdip_SetBitmapToClipboard(pBitmap)
Gdip_DisposeImage(pBitmap)

Gdip_Shutdown(pToken)
User avatar
Scr1pter
Posts: 1275
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: How to check if Clipboard image changed?

31 Aug 2018, 09:03

Thank you very much, tmplinshi!
It works perfectly now.

And also thanks to Helgef!

Best regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww and 336 guests