Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

How to Get Selected Text without Using the Clipboard


  • Please log in to reply
14 replies to this topic
YetAnotherSuperhero
  • Members
  • 10 posts
  • Last active: Nov 29 2011 05:29 PM
  • Joined: 28 Feb 2011
I'm stumped...

I'm trying to get some text that is selected (in, word, on the internet, etc.) WITHOUT using the clipboard.

How do I do this?

Thanks

adabo
  • Members
  • 385 posts
  • Last active: Jul 30 2015 07:52 PM
  • Joined: 13 May 2010
PostMessage / SendMessage perhaps? In conjunction with EM_GETSELTEXT.

YetAnotherSuperhero
  • Members
  • 10 posts
  • Last active: Nov 29 2011 05:29 PM
  • Joined: 28 Feb 2011
Thanks for the reply, but I'm still sort of new to this...

I have NO idea how to use this...

adabo
  • Members
  • 385 posts
  • Last active: Jul 30 2015 07:52 PM
  • Joined: 13 May 2010
Rajat made a nice tutorial for using windows messages. Also study the examples in the PostMessage / SendMessage section.

Feel free to ask if you need further assistance. It does get a bit tricky, with MSDN's lack of documentation for messages.

YetAnotherSuperhero
  • Members
  • 10 posts
  • Last active: Nov 29 2011 05:29 PM
  • Joined: 28 Feb 2011
Awesome, I'll read up.

Thanks again!

sumon
  • Moderators
  • 1317 posts
  • Last active: Dec 05 2016 10:14 PM
  • Joined: 18 May 2010
For most purposes, why not using the Clipboard? Given that you restore it afterwards, that is. Someone (Learning One?) has a nice Get Selected Text function, GST().

nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010

For most purposes, why not using the Clipboard? Given that you restore it afterwards, that is. Someone (Learning One?) has a nice Get Selected Text function, GST().

sumon has an excellent point here. Not only is the clipboard fast, but it works in places where messages don't.
The clipboard can be restored, so I don't see any reason to not use it, only reasons to not use SendMessage.

adabo
  • Members
  • 385 posts
  • Last active: Jul 30 2015 07:52 PM
  • Joined: 13 May 2010
Is it when we don't see a reason for something that we give up helping finding a solution?

YetAnotherSuperhero
  • Members
  • 10 posts
  • Last active: Nov 29 2011 05:29 PM
  • Joined: 28 Feb 2011
Yeah, the reason I can't use the clipboard is that sometimes a program will give me the clipboard cannot be accessed (or something like that).

adabo
  • Members
  • 385 posts
  • Last active: Jul 30 2015 07:52 PM
  • Joined: 13 May 2010
Ah. Yes, that's certainly a reason to find an alternative. I figured your question was specific for a reason ;)

sumon
  • Moderators
  • 1317 posts
  • Last active: Dec 05 2016 10:14 PM
  • Joined: 18 May 2010

Is it when we don't see a reason for something that we give up helping finding a solution?


Based on the first post, there was no indication as to why the non-clipboard request.

If someone needs basic help, but does not realize which kind of help he/she needs, it is better to help them in a way that is actually useful than to (sometimes in vain) try to help them in a far too advanced way.

For example, if I saw someone ask "Hey, how can I make a script in C++ that lets me type "I am the king of the world" when I press windows+k?", I would tell them to use Autohotkey for the task instead. If someone on the other hand described a problem where AHK was clearly not the appropiate solution (let's say they were making an application and wanted to implement a specific function that would be easier to implement in AHK, but the program was already in another language...), then I would of course simply not reply.

Answer 'nuff?

adabo
  • Members
  • 385 posts
  • Last active: Jul 30 2015 07:52 PM
  • Joined: 13 May 2010
@sumon: Agreed. No sense in wasting time :) As for me making assumptions, I don't mind. I have too much time on my hands, so I don't mind going with my instinct (even if it is a waste) on first post. I'm also bi-polar and will probably go months without helping here. So I do what I can while I can.

Solar
  • Members
  • 345 posts
  • Last active: Jan 15 2012 08:11 PM
  • Joined: 03 May 2009
Using the clipboard is going to be your most reliable solution due to wanting to use it for multiple applications. However, you can set up cases to get text from specific control types and have the clipboard option as a backup.

Here's an example that gets text from edit controls using ControlGet. It has a clipboard backup for non-edit types:

WinActive("A") ; sets last found window
ControlGetFocus, ctrl
if (RegExMatch(ctrl, "A)Edit\d+"))
    ControlGet, text, Selected,, %ctrl%
else {
    clipboardOld := Clipboard
    Send, ^c
    if (Clipboard != clipboardOld) {
        text := Clipboard
        Clipboard := clipboardOld
    }
}
MsgBox % text


sumon
  • Moderators
  • 1317 posts
  • Last active: Dec 05 2016 10:14 PM
  • Joined: 18 May 2010

@sumon: Agreed. No sense in wasting time :) As for me making assumptions, I don't mind. I have too much time on my hands, so I don't mind going with my instinct (even if it is a waste) on first post. I'm also bi-polar and will probably go months without helping here. So I do what I can while I can.


Besides, it might be fun to come up with a solution to the requested problem instead of just giving basic advice. :)

Seems Solar has some nice advice too.

Saruman
  • Members
  • 1 posts
  • Last active: Apr 07 2014 03:25 PM
  • Joined: 03 Apr 2014

As for a reason not to use the clipboard, I have found a use-case for those who assumed that since they can't think of a use-case, one must not exist:

 

Suppose you wanted to copy all the lines from a QuickBooks invoice into an Excel spreadsheet (AHK could be a great way to overcome Intuit's vendor lock-in). Using AutoHotKey, you can copy each line into Excel using the Clipboard. There is only one problem: There's no way for AHK to tell when the end of the QuickBooks invoice has been reached.

 

You could try to stop copying when you've copied a blank line, but QuickBooks puts a stop to this: If you try to copy a blank line, QuickBooks simply ignores the ^C input, so the Clipboard will not become empty. Instead, it'll still have whatever was on the Clipboard before.

 

If you could look at the active selection without needing to get QuickBooks to copy it to the Clipboard, you could check if that selection was empty or nonexistent, and use that to terminate the loop, but there is absolutely no way to achieve this using just the Clipboard.

 

So, how do we get the selected text without relying on the Clipboard?