Jump to content
AndyS19

Copy selected text from an input control to the clipboard.

Recommended Posts

AndyS19

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

    • Epic007
      By Epic007
      Hey guys I'm stuck with a program.....I need a make an automation which copies text from a chat bot app and paste it in notepad to log the replies of my chat bot and to analyse stuff....a lil help pls
       
       
       
       
       
       
       
      Thanks in advance
    • nooneclose
      By nooneclose
      I need to send a string of text to this popup and click on the ok button to save it.
      Here is the code I have so far:
      ;Start IE Sleep(7000) $oIE = _IECreate("http://www.google.com") Sleep(500) _IELoadWait($oIE) $hIE = _IEPropertyGet($oIE, "hwnd") ; Get Handle of the IE window Sleep(500) WinSetState($hIE, "", @SW_MAXIMIZE) ;Wait for a browser page load to complete Sleep(3000) _IENavigate($oIE, "https://properURL.com") Sleep(8000) _IELoadWait($oIE) ;Attach to a browser control embedded in another window $oIE = _IEAttach("https://"properURL.com", url") ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $oIE = ' & $oIE & @CRLF & '>Error code: ' & @error & ' Extended code: 0x' & Hex(@extended) & @CRLF) ;### Debug Console Sleep(2000) ;Get the title of the webpage ;Local $wTitle = _IEPropertyGet($oIE, "title") ;MsgBox($MB_SYSTEMMODAL, "Webpage title:", $wTitle) ;Clicks the new button Sleep(3000) _IEAction($nWorkOrderB, "focus") _IEAction($nWorkOrderB, "click") Sleep(5000) ;Store the Element names where the important data will be sent ;Store the long description button Local $wLongDButton = _IEGetObjById($oIE, "m65d795a4-img") ;Store the long Description field id Local $wComments = _IEGetObjById($oIE, "ma6499a9c-rte_iframe") ;Store the ok button id that is in the long description Local $wCommOk = _IEGetObjById($oIE, "m74031266-pb") ;******************************************************************************* ; Send the stored data to the proper field ;******************************************************************************* ;Click the long description button Sleep(300) _IEAction($wLongDButton, "focus") _IEAction($wLongDButton, "click") Sleep(300) ;Sends the Comments Sleep(500) _IEAction($wComments, "focus") _IEAction($wComments, "click") Sleep(500) _IEFormElementSetValue($wComments, "hello darkness my old friend") ;Click the ok button Sleep(500) _IEAction($wCommOk, "focus") _IEAction($wCommOk, "click") Sleep(500)  
      Here is the popup:

    • reida
      By reida
      Hello. Help to write the text.
      <textarea tabindex="4" class="js-input compose__labels__input" style="width: 407px; max-width: 413px;" wrap="off" autocomplete="off" data-original-name="To"></textarea>  I can not make.
      #include <IE.au3> $sLogin = 'adv' $iError = 1 $sUrl = 'https://......' $oIE = _IECreate($sUrl) ;;; _IELoadWait($oIE) $oLogin = _IEGetObjByName($oIE, "To") Sleep(7000) If IsObj($oLogin) Then _IEFormElementSetValue($oLogin, $sLogin) _IELoadWait($oIE) ; EndIf EndIf Through the selector it can be possible?
      #toolkit-154163367424641composeForm > div:nth-child(1) > div > div.compose-head__content.js-collapsed-off > div.compose-head__row-wrapper.compose-head__row-wrapper_to.js-row > div > div > div:nth-child(2) > div > div > div > textarea.js-input.compose__labels__input  
    • cramaboule
      By cramaboule
      Hi all,
       
      I was wondering if it is possible (and how) to put text on video on-the-fly (like VLC displays Title movie at the begining)
      and of course doing that in AutoIt
      Thanks in advance...
       
      C.
×