Is there an API or command line utility that returns the currently selected text from either active window or even globally, like the linux utility "xsel" ?
- I don't mind getting less than 100% success.
- i know that each window can have its own text selection, but it's negligible for now.
- For now the only solution/workaround i have is to sendkeys "ctrl+c" and read from clipboard, but that's bad solution for 2 obvious reasons.
- I know how to do it on MS-Word, but that's 1% of the cases.
thanks
edit
From this discussion i learn tha开发者_如何学Got there are too many technologies to select text. so i'll fall back to using clipboard. thanks anyway.
i'm leaving this question open for a while in case somebody have a miracle.
this is a solution i compiled from several sources.
- it will work only on simple text controls. for the other types of text there are other solutions, but for simplicity i'll use the clipboard.
(for a complete code, declarations and dependencies, google for "SendMessage hWndCaret")
If hWndCaret <> 0 Then
'first, get all text
nLength = SendMessage(hWndCaret, WM_GETTEXTLENGTH, 0&, ByVal 0&)
If nLength <> 0 Then
buff = Space$(nLength + 1)
res = SendMessage(hWndCaret, WM_GETTEXT, nLength + 1, ByVal buff)
If res <> 0 Then Txt = Left$(buff, res)
End If
' then
If nLength <> 0 Then
buff = Space$(nLength + 1)
res = SendMessage(hWndCaret, EM_GETSEL, VarPtr(StartPos), EndPos)
selection = Mid(Txt, StartPos + 1, EndPos - StartPos)
End If
End If
Check if the program supports accessible interface like IAccessible/IAccClientDocMgr or TextPattern_GetSelection/TextRange_GetText. Many software need to implement accessible to sell to the US government because of the Americans with Disabilities Act. You can call AccessibleObjectFromWindow or AutomationElement::FromHandle on a window.
It looks like nobody documents their accessible object tree, and if there is an API exists, then the API is the preferred way to get information from the program. E.g. you should use Q249232 to get IHTMLDocumnent2 if the application is IE. There are significant changes in IE7 and IE8's assessible tree when inspected in UI spy.
For other programs you may be out of luck. I cannot find the selection in the login email edit when using UISpy. Accessiblility depends on the good will of programmers to implement accessibility in their programs.
精彩评论