wo52616111

How to get the selected text without send("^c")?

26 posts in this topic

Many IDEs has their hotkeys, like Ctrl+C will copy one line. So if I need my ahk working in IDE, I can't get selected text by send("^c") (and ^{insert}).

How to get selected text without using sendinput? Sendmessage? Dllcall?

Func copyWithoutCtrlC()
    Local $c=ControlGetHandle("[ACTIVE]","","")
    _SendMessage($c,0x301) ;WM_COPY
    MsgBox(0,"",ClipGet())
EndFunc

This script is works in some windows(notepad, SciTE...), but did't work in IDEs and many other windows.

 Thanks   so much.

Share this post


Link to post
Share on other sites



Hi, wo52616111, welcome to the forum. When you say "many other windows" I doubt you are going to find a single method that works in absolutely every instance. You can, however, check out WinGetText and ControlGetText from the help file to give you some ideas.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

wo52616111,

Just to make clear - the name of the language is AutoIt, not ahk as you used above.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Hi, wo52616111, welcome to the forum. When you say "many other windows" I doubt you are going to find a single method that works in absolutely every instance. You can, however, check out WinGetText and ControlGetText from the help file to give you some ideas.

 I know WinGetText and ControlGetText, but they can't just get the selected text(they get all text), can they? 

wo52616111,

Just to make clear - the name of the language is AutoIt, not ahk as you used above.

M23

Sorry, I can not get what you mean.

Share this post


Link to post
Share on other sites

wo52616111,

You said this:

So if I need my ahk working in IDE

and I pointed out that you used the wrong name for the language - the code you posted (and the language supported by this forum ) is AutoIt.

Clearer now?

M23 


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

wo52616111,

You said this:

and I pointed out that you used the wrong name for the language - the code you posted (and the language supported by this forum ) is AutoIt.

Clearer now?

M23 

Oh! I see! Sorry for that. And my code is AutoIt, that's no problem.

Edited by wo52616111

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

wo52616111, you are correct they would get all text. You would then have to parse that text for the substring you're after.

Can you elaborate on how the text is being selected? I am assuming the it is up to the end user to highlight text with a mouse or SHIFT + ARROW keys and then you want whatever is highlighted to be copied, is that correct?

Edit: I had to rack my brain because it is an older topic, but this snippet from SmOke_N might provide some ideas for you. It still works for me on WIN 7 and 8.

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Can you elaborate on how the text is being selected? I am assuming the it is up to the end user to highlight text with a mouse or SHIFT + ARROW keys and then you want whatever is highlighted to be copied, is that correct?

Yes, you are correct. The text you get when you send("^c").

Share this post


Link to post
Share on other sites

 

Edit: I had to rack my brain because it is an older topic, but this snippet from SmOke_N might provide some ideas for you. It still works for me on WIN 7 and 8.

It seems is not what I want. That script still get text by using send("^c").

In this context: you send Ctrl+C(Ctrl+insert) when you highlight nothing, you will get that line of text where the cursor in. Many IDEs(visual studio, sublime text...) have this feature.

Now, I want to do this:

Func f()
If some text highlighted
    Local $text='@'&$textHighlighted&'@' ;if you highlight a word "apple", $text="@apple@"
    ClipPut($text) 
    Send("^v") ;"@apple@" instead of "apple"
Else
    MsgBox(0,"","NOTHING")
EndFunc

But you will found that, you never see the MsgBox if you get hightlight text by using send("^c").

That's the problem.

Share this post


Link to post
Share on other sites

ClipGet() https://www.autoitscript.com/autoit3/docs/functions/ClipGet.htm After you send ^c you can set the clipboard data to a variable with ClipGet. 

Send("^c")

Func f()
$Data = ClipGet();gets data from clipboard
If $Data <> "" Then
    Local $text='@'&$textHighlighted&'@' ;if you highlight a word "apple", $text="@apple@"
    ClipPut($text) 
    Send($Data) ;"@apple@" instead of "apple"
Else
    MsgBox(0,"","NOTHING")
EndFunc

 


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

ClipGet() https://www.autoitscript.com/autoit3/docs/functions/ClipGet.htm After you send ^c you can set the clipboard data to a variable with ClipGet. 

Send("^c")

Func f()
$Data = ClipGet();gets data from clipboard
If $Data <> "" Then
    Local $text='@'&$textHighlighted&'@' ;if you highlight a word "apple", $text="@apple@"
    ClipPut($text) 
    Send($Data) ;"@apple@" instead of "apple"
Else
    MsgBox(0,"","NOTHING")
EndFunc

 

Thanks for reply, but you don't even know what my problem is.

In IDEs windows, $Data!="" FOEVER if you send("^c").

Edited by wo52616111

Share this post


Link to post
Share on other sites

I originally saw that you wanted to get selected text without sending ^c. JLogan3o13 answered your question.

Thanks for reply, but you don't even know what my problem is.

In IDEs windows, $Data!="" FOEVER if you send("^c").

Your last post you seemed to switch your request:

Now, I want to do this:

Func f()
If some text highlighted
    Local $text='@'&$textHighlighted&'@' ;if you highlight a word "apple", $text="@apple@"
    ClipPut($text) 
    Send("^v") ;"@apple@" instead of "apple"
Else
    MsgBox(0,"","NOTHING")
EndFunc

But you will found that, you never see the MsgBox if you get hightlight text by using send("^c").

So I entered a clipget(). Clearly I'm missing what you are trying to accomplish. If you can get all the text in a window then you can parse that info with things like StringCompare, StringInStr, StringLen, StringLower, StringMid, StringReplace, StringRight, StringSplit, StringTrimLeft, StringTrimRight, StringUpper, StringLeft

Can you restate what you want to do now?

 

 


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

I originally saw that you wanted to get selected text without sending ^c. JLogan3o13 answered your question.

Your last post you seemed to switch your request:

So I entered a clipget(). Clearly I'm missing what you are trying to accomplish. If you can get all the text in a window then you can parse that info with things like StringCompare, StringInStr, StringLen, StringLower, StringMid, StringReplace, StringRight, StringSplit, StringTrimLeft, StringTrimRight, StringUpper, StringLeft

Can you restate what you want to do now?

 

 

I did not switch my request.

I want to get the highlighted text in all programs. But you see, in IDEs, if we send("^c") when we highlighting nothing, we will get a line of text; But when we highlighting some text then send("^c"), we get the text highlighted. You will always copy some text, even you did not highlight any text.

So, if you runing this script in those programs(IDEs):

Send("^c")

Func f()
$Data = ClipGet();gets data from clipboard
If $Data <> "" Then
    Local $text='@'&$textHighlighted&'@' ;if you highlight a word "apple", $text="@apple@"
    ClipPut($text) 
    Send($Data) ;"@apple@" instead of "apple"
Else
    MsgBox(0,"","NOTHING")
EndFunc

You will never see the MsgBox, even nothing highlighted.

 

 

ps.May be I describe aren't good enough, English is not my native language, and my English is a shit.

Edited by wo52616111

Share this post


Link to post
Share on other sites

To restate what I think you want to do:  "Copy selected text from any program into a script.". I was unaware that if you sent ^c you would always get a line of code. Are you looking for autoit to identify if the text has been highlighted? I cannot find any data on this. Perhaps a dllcall - https://www.autoitscript.com/autoit3/docs/functions/DllCall.htm using a dll will work.


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

To restate what I think you want to do:  "Copy selected text from any program into a script.". I was unaware that if you sent ^c you would always get a line of code. Are you looking for autoit to identify if the text has been highlighted? I cannot find any data on this. Perhaps a dllcall - https://www.autoitscript.com/autoit3/docs/functions/DllCall.htm using a dll will work.

You get my point finally.

I know dllcall, I said at the beginning, but just don't know what function I should use. I am trying.

Edited by wo52616111

Share this post


Link to post
Share on other sites

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Sorry, I still don't know how to solve the problem. I know EM_GETSELTEXT 2 days ago when I google this question, but don't know how to use it. This script can SendMessage WM_COPY(0x301):

_SendMessage($c,0x301) ;WM_COPY

But I try to SendMessage EM_GETSELTEXT like this, it did't work. (I even not sure if EM_GETSELTEXT=0x43e)

_SendMessage($c,0x43e)

 

Edited by wo52616111

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

JLogan3o13 edited his post above and I tried it and it worked:

HotKeySet("{ESC}", "_Mouse_Exit_App")

Global $i_Mouse_Is_Down = False
Global $i_Mouse_Primary = 0x01 ; 0x01 = left for primary, 0x02 = right

Global $a_hMouseMod_hTimer = _Mouse_StartWatch("_Mouse_GetHighlight_ToClipboard", 0, 10001, 1000)

While 1
    Sleep(100)
WEnd

Func _Mouse_GetHighlight_ToClipboard()
    If Not $i_Mouse_Is_Down Then
        Local $a_GetAsync = DllCall("User32.dll", "int", "GetAsyncKeyState", "int", $i_Mouse_Primary)
        
        If Not @error And BitAND($a_GetAsync[0], 0x8000) = 0x8000 Then
            $i_Mouse_Is_Down = True
            
            While Not @error And BitAND($a_GetAsync[0], 0x8000) = 0x8000
                Sleep(50)
                $a_GetAsync = DllCall("User32.dll", "int", "GetAsyncKeyState", "int", $i_Mouse_Primary)
            WEnd
            
            Send("^{Insert}")
            Sleep(100)
            
            $i_Mouse_Is_Down = False
        EndIf
    EndIf
    
    Return 1
EndFunc

Func _Mouse_StartWatch($sCallback_Func, $hWnd, $iEvent_ID, $iEvent_Time)
    Local $h_Mouse_Mod = DllCallbackRegister($sCallback_Func, "int", "")
    If @error Then Return SetError(@error, 1, 0)
    
    Local $h_Timer = DllCall("User32.dll", "int", "SetTimer", _
                        "hwnd", $hWnd, _
                        "int", $iEvent_ID, _
                        "int", $iEvent_Time, _
                        "ptr", DllCallbackGetPtr($h_Mouse_Mod))
    
    If @error Then Return SetError(@error, 2, 0)
    
    Local $aRet[2] = [$h_Mouse_Mod, $h_Timer[0]]
    
    Return $aRet
EndFunc

Func _Mouse_StopWatch(ByRef $h_Mouse_Mod, ByRef $h_Timer, $hWnd = 0)
    If $h_Mouse_Mod > 0 Then
        DllCallbackFree($h_Mouse_Mod)
        $h_Mouse_Mod = 0
    EndIf
    
    If $h_Timer > 0 Then
        DllCall("User32.dll", "int", "KillTimer", "hwnd", $hWnd, "int", $h_Timer)
        If @error Then Return SetError(@error, 0, 0)
        
        $h_Timer = 0
    EndIf
    
    Return 1
EndFunc

Func _Mouse_Exit_App()
    _Mouse_StopWatch($a_hMouseMod_hTimer[0], $a_hMouseMod_hTimer[1])
    
    Exit
EndFunc

Run the script and highlight any text in any window and then open notepad and hit ctrl + v to see what it copied.

Edited by computergroove

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

JLogan3o13 edited his post above and I tried it and it worked:

HotKeySet("{ESC}", "_Mouse_Exit_App")

Global $i_Mouse_Is_Down = False
Global $i_Mouse_Primary = 0x01 ; 0x01 = left for primary, 0x02 = right

Global $a_hMouseMod_hTimer = _Mouse_StartWatch("_Mouse_GetHighlight_ToClipboard", 0, 10001, 1000)

While 1
    Sleep(100)
WEnd

Func _Mouse_GetHighlight_ToClipboard()
    If Not $i_Mouse_Is_Down Then
        Local $a_GetAsync = DllCall("User32.dll", "int", "GetAsyncKeyState", "int", $i_Mouse_Primary)
        
        If Not @error And BitAND($a_GetAsync[0], 0x8000) = 0x8000 Then
            $i_Mouse_Is_Down = True
            
            While Not @error And BitAND($a_GetAsync[0], 0x8000) = 0x8000
                Sleep(50)
                $a_GetAsync = DllCall("User32.dll", "int", "GetAsyncKeyState", "int", $i_Mouse_Primary)
            WEnd
            
            Send("^{Insert}")
            Sleep(100)
            
            $i_Mouse_Is_Down = False
        EndIf
    EndIf
    
    Return 1
EndFunc

Func _Mouse_StartWatch($sCallback_Func, $hWnd, $iEvent_ID, $iEvent_Time)
    Local $h_Mouse_Mod = DllCallbackRegister($sCallback_Func, "int", "")
    If @error Then Return SetError(@error, 1, 0)
    
    Local $h_Timer = DllCall("User32.dll", "int", "SetTimer", _
                        "hwnd", $hWnd, _
                        "int", $iEvent_ID, _
                        "int", $iEvent_Time, _
                        "ptr", DllCallbackGetPtr($h_Mouse_Mod))
    
    If @error Then Return SetError(@error, 2, 0)
    
    Local $aRet[2] = [$h_Mouse_Mod, $h_Timer[0]]
    
    Return $aRet
EndFunc

Func _Mouse_StopWatch(ByRef $h_Mouse_Mod, ByRef $h_Timer, $hWnd = 0)
    If $h_Mouse_Mod > 0 Then
        DllCallbackFree($h_Mouse_Mod)
        $h_Mouse_Mod = 0
    EndIf
    
    If $h_Timer > 0 Then
        DllCall("User32.dll", "int", "KillTimer", "hwnd", $hWnd, "int", $h_Timer)
        If @error Then Return SetError(@error, 0, 0)
        
        $h_Timer = 0
    EndIf
    
    Return 1
EndFunc

Func _Mouse_Exit_App()
    _Mouse_StopWatch($a_hMouseMod_hTimer[0], $a_hMouseMod_hTimer[1])
    
    Exit
EndFunc

Run the script and highlight any text in any window and then open notepad and hit ctrl + v to see what it copied.

I have already said three hundred times, that I don't want to get  selected text by send("^c") (or ^insert).

I'm tired...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now