How to pause or temporarily disable OnClipboardChange? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
xypha
Posts: 24
Joined: 24 Apr 2020, 04:14

How to pause or temporarily disable OnClipboardChange?

31 Dec 2023, 07:56

How to pause or temporarily disable OnClipboardChange?

Code: Select all

#SingleInstance force
#Persistent
#Requires AutoHotkey v1.1.37

clipboard := "" ; don't trigger ToolTip on start

OnClipboardChange:

if A_EventInfo != 1 ; if clipboard is empty or contains something entirely non-text such as a picture
    return ; do nothing and don't trigger ToolTip on start

; various actions here such as
; SomeVar := SomeFunction(clipboard)

Tooltip, "Clip is triggered"
SetTimer, RemoveToolTip, -5000
Return

RemoveToolTip:
ToolTip
return

; ======

!s::
; some clipboard operation that triggers OnClipboardChange and ToolTip
return

!r::
pasteText := "text: 1000 words" 
PasteText(pasteText) ; use clipboard to send big chunks of text - DONT trigger OnClipboardChange and ToolTip
return

PasteText(pasteText) {
suspend On ; Disables all hotkeys and hotstrings.
pause On, 1 ; Pauses the script's current thread, but continues execution of function
tmp := ClipboardAll
Clipboard = %pasteText%
Send ^v
; sleep 500
; Clipboard := tmp
; pause Off
; suspend Off
return
}
On start, ToolTip is not triggered.
^c triggers OnClipboardChange and ToolTip (default behaviour).
!r sends "text" and triggers OnClipboardChange and ToolTip.

How do i stop !r from triggering OnClipboardChange?
What should i add to PasteText function to prevent OnClipboardChange from triggering?

I tried to use pause and suspend in various combinations, but none achieve my goal.

The goal -
PasteText function should never trigger OnClipboardChangem (pause or temporarily disable OnClipboardChange),
and other hotkeys/strings/functions (like !s) should trigger OnClipboardChange and ToolTip if clipboard is altered (keep default behaviour).

Any help would be appreciated.
Happy New Year! :)
User avatar
boiler
Posts: 17384
Joined: 21 Dec 2014, 02:44

Re: How to pause or temporarily disable OnClipboardChange?  Topic is solved

31 Dec 2023, 08:54

You should use the OnClipboardChange() function instead of the deprecated OnClipboardChange label. Then use the AddRemove parameter to enable/disable it calling the callback function.
User avatar
xypha
Posts: 24
Joined: 24 Apr 2020, 04:14

Re: How to pause or temporarily disable OnClipboardChange?

31 Dec 2023, 10:56

boiler wrote:
31 Dec 2023, 08:54
You should use the OnClipboardChange() function instead of the deprecated OnClipboardChange label.
Done. Thanks for this. :dance:
See spoiler.
One additional problem...
Spoiler
boiler wrote:
31 Dec 2023, 08:54
Then use the AddRemove parameter to enable/disable it calling the callback function.
This works if OnClipboardChange is in Script #1 and
!r and PasteText functions are in Script #2.

Pressing !r triggers PasteText in Script #2, but also triggers OnClipboardChange in Script #1, even with AddRemove parameter.
Is there a solution to this? :headwall:

(I don't want to ExitApp script #1 when PasteText starts and Run script #1 once PasteText finishes because it will reset all variables in script #1)
GEV
Posts: 1005
Joined: 25 Feb 2014, 00:50

Re: How to pause or temporarily disable OnClipboardChange?

31 Dec 2023, 11:27

Clipboard = %pasteText%
Where do you have the value of the variable pasteText from?
I would use a function like this for pasting long text.
User avatar
boiler
Posts: 17384
Joined: 21 Dec 2014, 02:44

Re: How to pause or temporarily disable OnClipboardChange?

31 Dec 2023, 11:29

I don’t know what you mean by the two different scripts. If you really are running two different script files, post each in their own code box so it’s clear what is in each one.
User avatar
xypha
Posts: 24
Joined: 24 Apr 2020, 04:14

Re: How to pause or temporarily disable OnClipboardChange?

31 Dec 2023, 12:56

I am a novice at AHK, and still learning. Apologies for the confusion.
GEV wrote:
31 Dec 2023, 11:27
Where do you have the value of the variable pasteText from?
It is generated from other hotkeys/function that pull data from text file.
GEV wrote:
31 Dec 2023, 11:27
I would use a function like this for pasting long text.
Your Send(text){} function uses clipboard and triggers OnClipboardChange -- I encounter the same problem I have with my example script.
boiler wrote:
31 Dec 2023, 11:29
I don’t know what you mean by the two different scripts. If you really are running two different script files, post each in their own code box so it’s clear what is in each one.
Same code as earlier, but split into two scripts.

Script #1 runs OnClipboardChange and other functions.

Code: Select all

#SingleInstance force
#Persistent
#Requires AutoHotkey v1.1.37

clipboard := "" ; don't trigger ToolTip on start
OnClipboardChange("ClipChanged")
return

ClipChanged(DataType) {

if DataType != 1 ; if clipboard is empty or contains something entirely non-text such as a picture
    return ; do nothing and don't trigger ToolTip on start

; various actions here such as
; SomeVar := SomeFunction(clipboard)

Tooltip, "Clip is triggered"
SetTimer, RemoveToolTip, -500
}

RemoveToolTip:
ToolTip
return

; ======

!s::
; some clipboard operation that triggers OnClipboardChange and ToolTip
return
Script #2 runs !r, PasteText and other functions.

Code: Select all

#SingleInstance force
#Persistent
#Requires AutoHotkey v1.1.37

!p::
; pull data from text file and perform function to generate 1000 words
return

!r::
pasteText := "text: 1000 words"
PasteText(pasteText) ; use clipboard to send big chunks of text
return

PasteText(pasteText) {
; OnClipboardChange("ClipChanged",0) ; commented out because "Error:  Parameter #1 invalid."
tmp := ClipboardAll
Clipboard = %pasteText% ; DONT want to trigger OnClipboardChange in test_1.ahk
Send ^v
sleep 500
Clipboard := tmp
; OnClipboardChange("ClipChanged",1)
return
}
Running Script #1 and Script #2 on same pc at the same time.
Pressing !r triggers PasteText in Script #2, but also triggers OnClipboardChange in Script #1, even with AddRemove parameter.
Is there a solution to this?
User avatar
boiler
Posts: 17384
Joined: 21 Dec 2014, 02:44

Re: How to pause or temporarily disable OnClipboardChange?

31 Dec 2023, 14:19

xypha wrote: Running Script #1 and Script #2 on same pc at the same time.
Pressing !r triggers PasteText in Script #2, but also triggers OnClipboardChange in Script #1, even with AddRemove parameter.
Is there a solution to this?
Yes, don’t have two scripts. Turn off the OnClipboardChange from anyplace in that script you need to. Why split it into two scripts?
User avatar
xypha
Posts: 24
Joined: 24 Apr 2020, 04:14

Re: How to pause or temporarily disable OnClipboardChange?

31 Dec 2023, 14:28

boiler wrote:
31 Dec 2023, 14:19
Why split it into two scripts?
I am slowly working on converting my messy AHK v1 script to AHK v2, resulting in two scripts.
The scripts posted on this topic are AHK v1 for ease of testing. Anyway, thanks for your help. really appreciate it. :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: apeironn, Bing [Bot], fiaztv1, Pianist and 185 guests