Jump to content

Copy selected text from an input control to the clipboard.


AndyS19
 Share

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


 

Link to comment
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
 Share

×
×
  • Create New...