"Backdoor" methods to copy text to the clipboard?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
alancantor
Posts: 76
Joined: 11 Jun 2019, 11:28

"Backdoor" methods to copy text to the clipboard?

05 Aug 2021, 10:17

Some of my AHK scripts perform one action if text is selected, and another action if text is not selected. Here is a general technique that works reliably:

Code: Select all

!W::		; Alt + W

clipboard := ""

SendInput ^c
ClipWait, 1		; Wait for clipboard to populate

if ErrorLevel		; ErrorLevel is TRUE implies clipboard did NOT become populated, which means nothing was selected
	{
	MsgBox NO, there is nothing in the clipboard
	}
Else
	{
	MsgBox YES, there is something in the clipboard
	}
Return
I'm using a quirky application that is causing these kinds of scripts to fail. In this application, issuing a copy instruction copies an entire LINE. It does this even when nothing is selected. So this strategy, which works in every other application I've tried it in, is a bust in this one!

Are there "backdoor" methods for issuing a copy command via AHK? I'm wondering whether this bizarre behaviour is triggered by the built-in methods for copying: pressing Ctrl + C, or choosing Copy from a context menu.
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: "Backdoor" methods to copy text to the clipboard?

05 Aug 2021, 11:06

This is actually fairly common. One workaround is to copy the text, then send Shift+Left, copy that string, and compare the two. The strings or their difference may tell you whether any text was originally selected. AHK also has a command to get the selected text from an edit control, but its success may depend on the specific control. You could try it.

https://www.autohotkey.com/docs/commands/ControlGet.htm#Selected

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 282 guests