Distinguish source of the clipboard change

Viewed 47

I wanted to log clipboard content whenever it changes, so I created this code:

#Persistent
FileEncoding, UTF-8
OnClipboardChange("ClipChanged")
return

ClipChanged(Type) {
    now := A_Now
    FormatTime, time, now, dd.MM.yyyy HH:mm:ss
    FileAppend, % Format("{}   '{}'`n", time, Clipboard), D:\clipboard_log.txt
}

Now, I would like to ignore scenarios when clipboard has been changed by me. I want to log clipboard changes made only by external applications. I came up with an idea:

#Persistent
FileEncoding, UTF-8
OnClipboardChange("ClipChanged")
global clipChangedByMe := false
return

ClipChanged(Type) {
    if (clipChangedByMe)
    {
        clipChangedByMe := false
    }
    else
    {
        now := A_Now
        FormatTime, time, now, dd.MM.yyyy HH:mm:ss
        FileAppend, % Format("{}   '{}'`n", time, Clipboard), D:\clipboard_log.txt
    }
}

^c::
    clipChangedByMe := true
    Clipboard := ClipboardAll
return

For some reason, ClipChanged event is not fired after line: Clipboard := ClipboardAll. Whats wrong there?

1 Answers

At the first glance I see a couple of issues here. If you want the original function of Ctrl + C preserved you have to use the tilde prefix: ~^c. Secondly, both Clipboard and ClipboardAll are built in variables and you are assigning one to the other for some reason?

Update: Well, the following seems to work:

Global clipChangedByMe := false
OnClipboardChange("ClipChanged")

~^c::clipChangedByMe := true

ClipChanged() {
    If (clipChangedByMe)
        clipChangedByMe := false
    else {
        FormatTime, time,, dd.MM.yyyy HH:mm:ss
        FileAppend, % Format("{}`n{}`n`n", time, Clipboard), D:\clipboard.log
    }
}
Related