New issues with OnClipboardChange

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

New issues with OnClipboardChange

Post by milkygirl90 » 31 Aug 2021, 21:40

These days, I notice even with turning on/off of OnClipboardChange, sometimes it will result in "sticky keys", E.g. some key (I don't know which) will be held down such that none of my modifier keys will work unless I fully exit the script.

The other issue is that any other lines of code that involve my %clipboard% variable, the newly copied item does not get stored and it pastes the previous item instead. I don't understand why just based on the code below..

Code: Select all

OnClipboardChange("CleanLink") ;in auto-exe section

CleanLink() {
if !(InStr(Clipboard, "google.com") || InStr(Clipboard, "fbcdn.net") || RegExMatch(Clipboard, "\.[a-z]{2,4}/")) ; match any domain ending with 2 to 4 characters and a forward slash (if there's no /, then no cleaning is required)
		return
	OnClipboardChange("CleanLink", 0)
	clipsave := ClipboardAll
		if RegExMatch(Clipboard, "https://youtu.*?\K\d+$", Seconds) {
		Time = 20210101000000
		Time += Seconds, Seconds
		FormatTime, Time, %Time%, H:mm:ss
	Clipboard := Time
	}
	Clipboard := RegExReplace(Clipboard, "google.com.*\K&oq=.*")
			Clipboard := RegExReplace(Clipboard, "(?<=search\?q=)[a-z\+]+", "%s")
	Clipboard := StrReplace(Clipboard, "&source=lnt")
	Clipboard := StrReplace(Clipboard, "?usp=sharing")
	Clipboard := RegExReplace(Clipboard, "fbcdn.net.*\K&bytestart=.*")
		if !Clipboard
		Clipboard := clipsave
	OnClipboardChange("CleanLink", 1)
}
msgbox shows empty regardless of sleep values

Code: Select all

$^d::   ;Font Header RED 
Click 3
OnClipboardChange("CleanLink", 0)
Sleep 200
SendInput ^d
WinWaitActive,Font ahk_exe Evernote.exe,,4
Sleep 300
If WinExist("Font ahk_exe Evernote.exe"){
SendInput {tab}Bold{tab}15
SendInput {tab 3}r{Enter}
ClipboardOld := ClipboardAll
Clipboard := ""
Sleep 1500
SendInput ^c
;SendInput {appskey}{Down 2}{Enter}
;SendInput {ctrl down}c{ctrl up}
msgbox, %clipboard%
}
return

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: New issues with OnClipboardChange

Post by milkygirl90 » 08 Sep 2021, 04:17

Any help will be much appreciated!

User avatar
boiler
Posts: 16770
Joined: 21 Dec 2014, 02:44

Re: New issues with OnClipboardChange

Post by boiler » 08 Sep 2021, 05:13

The Sleep, 1500 does nothing where you have it. It would have to be after you send ^c, not before. But you should be using ClipWait instead of Sleep anyway:

Code: Select all

$^d::   ;Font Header RED 
Click 3
OnClipboardChange("CleanLink", 0)
Sleep 200
SendInput ^d
WinWaitActive,Font ahk_exe Evernote.exe,,4
Sleep 300
If WinExist("Font ahk_exe Evernote.exe"){
SendInput {tab}Bold{tab}15
SendInput {tab 3}r{Enter}
ClipboardOld := ClipboardAll
Clipboard := ""
SendInput ^c
ClipWait, 1
if ErrorLevel
	return
msgbox, %clipboard%
}
return

By the way this subroutine turns off the OnClipboardChange but never turns it back on at the end. Is that what you want? I would think you want to do this:

Code: Select all

$^d::   ;Font Header RED 
Click 3
OnClipboardChange("CleanLink", 0)
Sleep 200
SendInput ^d
WinWaitActive,Font ahk_exe Evernote.exe,,4
Sleep 300
If WinExist("Font ahk_exe Evernote.exe"){
SendInput {tab}Bold{tab}15
SendInput {tab 3}r{Enter}
ClipboardOld := ClipboardAll
Clipboard := ""
SendInput ^c
ClipWait, 1 
if ErrorLevel {
	OnClipboardChange("CleanLink", 1)
	return
}
msgbox, %clipboard%
}
OnClipboardChange("CleanLink", 1)
return

Also, you never restore the original contents of the clipboard, so why are you assigning them to ClipboardOld?
Last edited by boiler on 08 Sep 2021, 05:26, edited 2 times in total.

just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: New issues with OnClipboardChange

Post by just me » 08 Sep 2021, 05:24

As far as I understand the AHK source each use of the variable Clipboard reads/writes content from/to the system's clipboard. You should try to reduce this as far as possible. For your script it should suffice to read/write the clipboard only once:

Code: Select all

OnClipboardChange("CleanLink") ;in auto-exe section

CleanLink() {
   Clip := Clipboard
   If !(InStr(Clip, "google.com") || InStr(Clip, "fbcdn.net") || RegExMatch(Clip, "\.[a-z]{2,4}/")) ; match any domain ending with 2 to 4 characters and a forward slash (if there's no /, then no cleaning is required)
      Return
   OnClipboardChange("CleanLink", 0)
   Clipsave := ClipboardAll
   If RegExMatch(Clip, "https://youtu.*?\K\d+$", Seconds) {
      Time = 20210101000000
      Time += Seconds, Seconds
      FormatTime, Time, %Time%, H:mm:ss
      Clip := Time
   }
   Else { ; the following seems useless after Clip := Time
      Clip := RegExReplace(Clip, "google.com.*\K&oq=.*")
      Clip := RegExReplace(Clip, "(?<=search\?q=)[a-z\+]+", "%s")
      Clip := StrReplace(Clip, "&source=lnt")
      Clip := StrReplace(Clip, "?usp=sharing")
      Clip := RegExReplace(Clip, "fbcdn.net.*\K&bytestart=.*")
   }
   Clipboard := Clip <> "" ? Clip : Clipsave
   OnClipboardChange("CleanLink", 1)
}

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: New issues with OnClipboardChange

Post by milkygirl90 » 08 Sep 2021, 21:23

Thank you both. Let me monitor the revised code and see how it works out..

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: New issues with OnClipboardChange

Post by milkygirl90 » 11 Sep 2021, 20:30

just me wrote:
08 Sep 2021, 05:24
As far as I understand the AHK source each use of the variable Clipboard reads/writes content from/to the system's clipboard. You should try to reduce this as far as possible. For your script it should suffice to read/write the clipboard only once:

Code: Select all

OnClipboardChange("CleanLink") ;in auto-exe section

CleanLink() {
   Clip := Clipboard
   If !(InStr(Clip, "google.com") || InStr(Clip, "fbcdn.net") || RegExMatch(Clip, "\.[a-z]{2,4}/")) ; match any domain ending with 2 to 4 characters and a forward slash (if there's no /, then no cleaning is required)
      Return
   OnClipboardChange("CleanLink", 0)
   Clipsave := ClipboardAll
   If RegExMatch(Clip, "https://youtu.*?\K\d+$", Seconds) {
      Time = 20210101000000
      Time += Seconds, Seconds
      FormatTime, Time, %Time%, H:mm:ss
      Clip := Time
   }
   Else { ; the following seems useless after Clip := Time
      Clip := RegExReplace(Clip, "google.com.*\K&oq=.*")
      Clip := RegExReplace(Clip, "(?<=search\?q=)[a-z\+]+", "%s")
      Clip := StrReplace(Clip, "&source=lnt")
      Clip := StrReplace(Clip, "?usp=sharing")
      Clip := RegExReplace(Clip, "fbcdn.net.*\K&bytestart=.*")
   }
   Clipboard := Clip <> "" ? Clip : Clipsave
   OnClipboardChange("CleanLink", 1)
}
I just monitored it, and everything else under this portion doesn't work anymore as opposed to my original script:

Code: Select all

 Else { ; the following seems useless after Clip := Time
      Clip := RegExReplace(Clip, "google.com.*\K&oq=.*")
      Clip := RegExReplace(Clip, "(?<=search\?q=)[a-z\+]+", "%s")
      Clip := StrReplace(Clip, "&source=lnt")
      Clip := StrReplace(Clip, "?usp=sharing")
      Clip := RegExReplace(Clip, "fbcdn.net.*\K&bytestart=.*")
   }

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: New issues with OnClipboardChange

Post by milkygirl90 » 15 Sep 2021, 16:06

any advice above please?

braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: New issues with OnClipboardChange

Post by braunbaer » 15 Sep 2021, 16:32

Make test output to see what your script does exactly. You can use msgbox for that.
And at any time, you can see the last executed script lines as well as the content of all global variables by doubleclicking the script icon in the system tray.
That should help you to locate the error.

just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: New issues with OnClipboardChange

Post by just me » 16 Sep 2021, 08:54

milkygirl90 wrote:I just monitored it, and everything else under this portion doesn't work anymore as opposed to my original script:
In your original script the clipboard variable is set to a time in H:mm:ss format if the RegEx on line 8 has a match:

Code: Select all

if RegExMatch(Clipboard, "https://youtu.*?\K\d+$", Seconds) {
	Time = 20210101000000
	Time += Seconds, Seconds
	FormatTime, Time, %Time%, H:mm:ss
	Clipboard := Time
}
What shall the lines

Code: Select all

Clipboard := RegExReplace(Clipboard, "google.com.*\K&oq=.*")
Clipboard := RegExReplace(Clipboard, "(?<=search\?q=)[a-z\+]+", "%s")
Clipboard := StrReplace(Clipboard, "&source=lnt")
Clipboard := StrReplace(Clipboard, "?usp=sharing")
Clipboard := RegExReplace(Clipboard, "fbcdn.net.*\K&bytestart=.*")
replace in this case?

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: New issues with OnClipboardChange

Post by milkygirl90 » 21 Sep 2021, 19:56

Hello @just me, my original code didn't include the Else braces, and I followed your post on 8 Sep.

For the codes above, I treat them as individual conditions, i.e.

if it's a YouTube link, give me the time
if it's a google link, do X replacement
if it's something else, do Y replacement

not sure what's wrong currently..

Post Reply

Return to “Ask for Help (v1)”