Jump to content
AndyS19

Copy selected text from an input control to the clipboard.

Recommended Posts

I want to copy just the selected text in an input control and put it to the clipboard, but GuiCtrlRead() always reads all of the text in the control.

In this test code, when I highlight a  portion of the left control's text and press ^C, I want to copy just that text to the clipboard; but when I highlight a  portion of the right control's text and press ^C, I want to copy all of its text to the clipboard.

#include <Debug.au3>
_DebugSetup(@ScriptName & "_debug.txt", False, 2, "")
_DebugOut("=============== " & @MON & "/" & @MDAY & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & " ====================")

#include <GUIConstants.au3>
#include <WinAPI.au3>
#include <Array.au3>

Opt("GUICloseOnESC", 1) ; ESC closes GUI? (0 = no, 1 = yes)
Opt("GUIOnEventMode", 1) ; Set OnEvent mode
Opt('MustDeclareVars', 1)
OnAutoItExitRegister("Event_GUIClose")
Opt("GUIEventOptions", 1) ;0=default, 1=just notification, 2=GuiCtrlRead tab index
Opt("WinTitleMatchMode", -2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

Global $hWnd_MainWin, $iID_Left, $iID_Right

_Main()

Func _Main()

    $hWnd_MainWin = GUICreate("test7", 200, 300)

    $iID_Left = GUICtrlCreateInput("AAA BBB CCC (get Selected text)", 10, 30, 200, 25)
    GUICtrlSetOnEvent($iID_Left, "handle_left_btn")

    $iID_Right = GUICtrlCreateInput("DDD EEE FFF (get All text)", 10, 60, 200, 25)
    GUICtrlSetOnEvent($iID_Right, "handle_right_btn")

    Local $aAccelKeys[1][2] = [["^c", $hWnd_MainWin]]
    GUISetAccelerators($aAccelKeys)

    GUISetOnEvent($GUI_EVENT_CLOSE, 'Event_GUIClose')

    GUISetState() ; Make everything visible

    While 1
        Sleep(127)
    WEnd
EndFunc   ;==>_Main

Func handle_CTRL_C_key()
    ConsoleWrite("+++: handle_CTRL_C_key() entered" & @CRLF)
    Local $hCtrl, $ctrlID, $control_classname, $text

    $control_classname = ControlGetFocus($hWnd_MainWin)
    $hCtrl = ControlGetHandle($hWnd_MainWin, "", $control_classname)
    $ctrlID = _WinAPI_GetDlgCtrlID($hCtrl)

    If ($ctrlID = $iID_Left) Then
        $text = GUICtrlRead($ctrlID)
        ClipPut($text)
        ConsoleWrite("+++: Copied selected text:" & @CRLF & "==>" & $text & "<==" & @CRLF)
    ElseIf ($ctrlID = $iID_Right) Then
        $text = GUICtrlRead($ctrlID)
        ClipPut($text)
        ConsoleWrite("+++: Copied all text:" & @CRLF & "==>" & $text & "<==" & @CRLF)
    EndIf

EndFunc   ;==>handle_CTRL_C_key

Func Event_GUIClose()
    Exit (99)
EndFunc   ;==>Event_GUIClose

Func handle_left_btn()
    ConsoleWrite("+++: handle_left_btn() entered" & @CRLF)
EndFunc   ;==>handle_left_btn

Func handle_right_btn()
    ConsoleWrite("+++: handle_right_btn() entered" & @CRLF)
EndFunc   ;==>handle_right_btn


 

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

  • Similar Content

    • By Kiko745
      Hello!
      I am new to autoit so please if someone could help me. I am trying to combine multiple .txt files but I can't get it right. In every file are the exact amout of lines ( for example here are 2 ) ,            I don't want to make it for just 2 lines for each txt files but for all the lines the txt files contains. The thing I want to do is :
      Text File 1
      Hello my name
      I am a big
      ...
      Text File 2
      is Fred,
      Potato head for
      ...
      Text File 3
      and I like to eat pizza.
      Not getting this right.
      ...
      ------------------- Combine them all into one txt file like here ------------------------------
      Final Text file
      Hello my name is Fred, and I like to eat pizza.
      I am a big Potato head for Not getting this right.
      ...
       
      Thanks for any advice!
      P.S. Sorry for my English, not my first language.
       
    • By Kruxe
      Hello Everyone,
      I am very green when it comes to AutoIT, I have recently started using it to automate a job that i perform using MS Excel and that is working great. Recently I have been faced with a challenge I which i need to copy a specific set of lines from one .txt file and past them, overwriting a specific set of lines in a different .txt file. I honestly have no clue how I can copy the specified lines and transfer them. What functions / commands could I use and how could I lay them out I AutoIT? Any and all help will be appreciated. 
      Thanks!
      Kruxe
    • By _leo_
      Hey there!
      I am having a problem with clicking a button in IE. As you can see in the code, there would be a class name to click it, but since this button exists several times with exactly the same code, I can only use the "Noch nicht veröffentlicht" to separate it from the others. But this part is just pure text without any classification. 
      "<div class="checkmark" data-v-1e5b1f70=""><svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 16 16" data-v-1e5b1f70=""><circle cx="8" cy="8" r="8" data-v-1e5b1f70="" /></svg></div>Noch nicht veröffentlicht"
      I was trying to click it with:
      #include <IE.au3> local $oNNVs = _IEFormGetCollection($oIE2) For $oNNV in $oNNVs If String($oNNV.innertext) = "Noch nicht veröffentlicht" Then _IEAction($oNNV, "click") EndIf Next and:
      $inputs = $oIE2.document.getElementsByTagName("div") For $input In $inputs If $input.innertext == "Noch nicht veröffentlicht" then $input.click() EndIf Next But nothing seems to work. Thanks for the help!
    • By rudi
      Hi,
      from other postings here as a start I'm quite Close to my Goal of putting formatted text to the clipboard:
      #include-once #include <GUIConstantsEx.au3> #include <GuiRichEdit.au3> #include <WindowsConstants.au3> Global $g_idLblMsg, $g_hRichEdit Example("The first text will be formatted ""as is""" & @CRLF & _ "(Default Font and font size)", "This text is formatted differently") MsgBox(0, "Rich Text copied", "Formatted Text put to clipboard") Func Example($TextDefault, $TextCourier) Local $hGui, $iMsg, $idBtnNext, $iStep = 0 $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName, StringLen(".exe")) & ")", 320, 350, -1, -1) $g_hRichEdit = _GUICtrlRichEdit_Create($hGui, $TextDefault, 10, 10, 300, 220, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL)) $g_idLblMsg = GUICtrlCreateLabel("", 10, 235, 300, 60) GUISetState(@SW_SHOW) _GUICtrlRichEdit_SetSel($g_hRichEdit, -1, -1) ; set selection behind last CHAR in RTF_EDIT _GUICtrlRichEdit_SetFont($g_hRichEdit, 8, "Courier New") ; change font to 8pt, Courier New _GUICtrlRichEdit_AppendText($g_hRichEdit, @CRLF & $TextCourier) _GUICtrlRichEdit_SetSel($g_hRichEdit, 0, -1) ; select all the content of the RTF_EDIT _GUICtrlRichEdit_Copy($g_hRichEdit) ; put the selection to the clipboard EndFunc ;==>Example  
      Finally the clipboard content has to hold pre-formatted data to be pasted into an email body. for the 2nd part I'd like to add a frame around the text, like I did manually before taking this screenshot.



      Any suggestions, or maybe some absolutely different Approach?
       
      CU, Rudi.
    • By GillesMaisonneuve
      Good morning,
       
      I am trying to align a two-line text in a button so that it would be centered in the button but can't succeed to do it for the vertical part.
      Can someone tell me what I do wrong ?
      Here is my code:
      $btn3 = GUICtrlCreateButton("Execute " & @LF & " file 1", 100, 65, 60, 50, _ BitOr($BS_CENTER,$BS_BOTTOM,$BS_MULTILINE)) and here is the resulting layout I get in my window.
      I tried to follow the help, made a lot of different parameter combinations (here it's `$BS_BOTTOM`, but I tried `$BS_VCENTER` and `$BS_TOP`), still get the same result : vertical alignement is a no go for me.
      Can someone help ?
      TIA
      Gilles

×
×
  • Create New...