Clipboard working but ClipboardAll not working?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scoox
Posts: 125
Joined: 11 May 2014, 09:12
Location: China

Clipboard working but ClipboardAll not working?

21 Feb 2019, 04:06

Hi, I'm working on a hotkey that swaps the current clipboard with the prior one. This is handy for swapping words in a text with two clicks, for example, to swap the words "hello" and "autohotkey" in the following string: "hello world autohotkey" >> "autohotkey world hello".

Here is my script, but for some reason it's not working. However it works if I replace ClipboardAll with Clipboard:

Code: Select all

LButton & c::
	PriorClipboard() ;Save clipboard first
	SendInput, ^c
Return

LButton & r::  ;restore prior clipboard
	Clipboard := PriorClipboard()
Return

PriorClipboard()
{
	Static ClipPrior := "(prior clipboard was empty)"
	Temp := ClipPrior
	ClipPrior := ClipboardAll ;Changing this to  just "Clipboard" works, why?
	Return Temp
}
TBH I've always found ClipboardAll to act funny. I'm hoping to finally come to terms with it, thanks in advance! :thumbup:
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Clipboard working but ClipboardAll not working?

21 Feb 2019, 05:48

Hi,

ClipboardAll variables are marked as "binary" internally. AHK needs this information to restore the clipboard's content properly. It might be that this information gets lost when you return the contents of such a variable from a function.

Does the following work for you?

Code: Select all

#NoEnv
ClipPrior := "(prior clipboard was empty)"
Return

LButton & c::
	ClipPrior := ClipboardAll ; Save clipboard first
	SendInput, ^c
Return

LButton & r::  ; restore prior clipboard
	Temp := ClipPrior
	ClipPrior := ClipboardAll
	Clipboard := Temp
   Temp := ""
Return
User avatar
Scoox
Posts: 125
Joined: 11 May 2014, 09:12
Location: China

Re: Clipboard working but ClipboardAll not working?

21 Feb 2019, 06:16

Further digging, there's something odd going on with functions returning (or rather, NOT returning) a value originating from ClipboardAll. Look at the following code which works:

Code: Select all

!v::
	Clipboard := ;Clear clipboard
	Clipboard := MyFunc() ;MyFunc() returns "hello world", write this string to clipboard
	SendInput, ^v ;Paste clipboard contents (should paste "hello world")
Return

MyFunc()
{
	Clipboard := "hello world" ;Put "hello world" in Clipboard
	MyVar := Clipboard ;Write Clipboard to MyVar. MyVar now contains "hello world"
	Return MyVar ;Return MyVar i.e. function returns "hello world"
}
When I press !v in Notepad, "hello world" is typed. If I change that says MyVar := Clipboard to MyVar := ClipboardAll, it no longer works and pressing !v won't type anything. I'm perplexed.
User avatar
Scoox
Posts: 125
Joined: 11 May 2014, 09:12
Location: China

Re: Clipboard working but ClipboardAll not working?

21 Feb 2019, 06:22

just me wrote:
21 Feb 2019, 05:48
Does the following work for you?
Yeah, that does. I was just about to say that, if I put this code inside a function, it works fine, as long as the clipboard is written to inside the body of the function. I'll use this for the time being. Do you reckon it's a bug? Thanks
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Clipboard working but ClipboardAll not working?

21 Feb 2019, 06:46

I'm not sure whether this can be called a bug. The ability to pass a ClipboardAll variable to y function by value was added with AHK v1.0.46.
User avatar
Scoox
Posts: 125
Joined: 11 May 2014, 09:12
Location: China

Re: Clipboard working but ClipboardAll not working?

21 Feb 2019, 08:24

just me wrote:
21 Feb 2019, 06:46
I'm not sure whether this can be called a bug. The ability to pass a ClipboardAll variable to y function by value was added with AHK v1.0.46.
In theory the data in ClipboardAll is no different from the data any regular variable is able to store. If this works:

Code: Select all

MyVar := ClipboardAll
it kind of beats logic that this doesn't:
MyVar := MyFunc()

MyFunc()
{
Temp := ClipboardAll
Return MyVar
}
In the second snippet, MyVar is empty. I'll report it just in case. Anyway, luckily there are workarounds. Thanks for chiming in :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee and 121 guests