copy selected text from any application

hi,

How do you copy a selected text from any application?

regards
LVL 3
MutsopAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MutsopAuthor Commented:
euhm I don't quite get your response... This is just a basic SetText/GetText which only works with the current application.

I want to copy the selected text from ANY application ( whether it is word, excel, Firefox, chrome, notepad, .... ).
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

dsineCommented:
if you are trying to access the selected text in another application am afraid that is not possible afaik. You can access clipboard so if someone copies items from other applications this might/is available to you.
MutsopAuthor Commented:
For further explenation:

What I want is when I selected a text (which in my case would be a phone number) and press Ctrl+W it would call that number using the TAPI library
dsineCommented:
applications like cisco click to call etc have to register with the applications for them to work. there is some literature online that suggests using accessibility features to look at texts etc in other apps. but am afraid my knowledge on teh subject is limited.
MutsopAuthor Commented:
how about simulating Ctrl+C press? How can this be done
dsineCommented:
perhaps.. some onlien documentation on that.

http://inputsimulator.codeplex.com/
CodeCruiserCommented:
Did you look at http:#37243173 ?
MutsopAuthor Commented:
K so after reading a coupld of articles I came created following code:
Public Class Main
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
    Private Sub TimerHotKey_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerHotKey.Tick
        CheckForHotKeys()
    End Sub

    Public Sub CheckForHotKeys()
        If GetAsyncKeyState(Keys.ControlKey) And GetAsyncKeyState(Keys.B) Then
            SendKeys.Send("^c")
            'If TypeOf (Clipboard.GetText) Is String Then
            TextBoxNumber.Text = Clipboard.GetText
            'Else
            MessageBox.Show("Geselecteerde tekst is geen geldige String")
            'End If
        End If
    End Sub
End Class

Open in new window


But I get following error:
"A call to PInvoke function 'Tapi.Business!Tapi.Business.Main::GetAsyncKeyState' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."

any ideas what it might be?
sarabandeCommented:
to retrieve the text of a control in another process, send a WM_GETTEXT message to the hwnd of the control.

the hwnd of the current control could be get by calling GetForegroundWindow.


Sara
MutsopAuthor Commented:
@sarabande: "retrieving a text of a control in another process": Would it work for any program? Like the Office programs, browsers, ...

If not is there a way that anybody can help me out with previous problem? ID: 37243464

thanks
sarabandeCommented:
it would work with all programs which use standard windows text controls for the selected text. so if the user selected text in a chart or grid or even an image it would not work.

you could try whether it works by calling winapi function GetForegroundWindow. if it returns hwnd not zero you could check with spy whether it is the window you were looking for.

Sara
sarabandeCommented:
when the user selects text in a word document or an excel sheet you hardly will get that selection using windows api (you could make a screenshot image and analyse using ocr ...). but for those programs and also the browsers you could provide add-ons which would do what you want. so after detecting the key-stroke you could check which application is in the foreground and you would try to receive text from text control only if it is not one of the known applications where you provided an add-on.

Sara
MutsopAuthor Commented:
So simulating a Ctrl-c wouldn't work? That doesn't really make any sense.
sarabandeCommented:
manipulating key-input of another application only works with a hook. installing a hook requires admin privileges for good reasons. you surely don't want yourself another program monitoring your keys while typing a pin or tan for online banking.

Sara
sarabandeCommented:
note, most programs would have customization such that to start a macro using a key-combination, for example ctrl+w. they also would allow to give ctrl+w the same meaning as ctrl+c.

Sara
MutsopAuthor Commented:
Well, the application will only be used inside our company.
The idea was to make some sort of skype number calling possibility. But instead that the number is reconized automaticly, the user has to press Ctrl-b to call a selected number.

As most phonenumbers are retrieved from browsers (other company websites) or pdf/word templates, simulating Ctrl-c would be the best choice.

as if we needed to istall an addon for IE, FF, Chrome, Word, Adobe Reader, it would take a hell of a long time.
sarabandeCommented:
as told you could install a global hook which catches the ctrl+b. it is also possible for most keyboards to define new key-combinations. and i remember that there are tools - at least in older windows versions - which support global accelerators which would do what you want.

i personally don't like such manipulations cause with the time their existence and reasons tend to become unknown. so new users or users who don't use that feature may try to use the ctrl+b for other reasons or have a program where the ctrl+b has other meaning and that would fail and there is no one who could help them.

Sara
sarabandeCommented:
for the skype feature you could implement the drop part of drag-and-drop into your application. that way users could select the number and drag it with the mouse to your app, same as they could do it within the browser.

Sara
MutsopAuthor Commented:
drag-and-drop? Is it a ToolBox element? as I can't seem to find it.
Or do you mean simply dragging and dropping it in a textbox?
sarabandeCommented:
yes, i mean standard dragging and dropping. you could make it that a user selects the number and drags it with the mouse over an icon in the taskbar, then you open a window and let the user drop the number into a control. the mouse cursor would turn to a + symbol as it is with many applications.

Sara

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MutsopAuthor Commented:
I guess I should use the drag and drop :)
I hoped though for the Ctrl-c to work.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.