Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Selected Text Without Using the Clipboard

Tags:

autohotkey

I am trying to create a pretty basic text wrapper in AutoHotKey for use when programming. I got it to work using the clipboard to copy the selected text, modify it, then paste it, but I am trying to refrain from using the clipboard since it does not work well in conjunction with my Clipboard Manager. Does anyone know how to do this?

!r:: ;Alt+R+%Char% = Wrap Text with Input Characters
    ClipSave := ClipboardAll
    Send ^c
    Input, Char, L1
    if ("" . Char = "{")
    {
        clipboard = {%clipboard%}
    }
    else if ("" . Char = "[")
    {
        clipboard = [%clipboard%]
    }
    else if ("" . Char = "(")
    {
        clipboard = (%clipboard%)
    }
    else
    {
        clipboard = %Char%%clipboard%%Char%
    }
    StringReplace, clipboard, clipboard,%A_SPACE%",", All
    Send ^v
    Clipboard := ClipSave
    ClipSave = 
return

Note: I have seen ControlGet, text, Selected and attempted to implement it, but it did not work (no error, just no action). If anyone has a solution to this, that would fix my issue.

like image 368
Michelfrancis Bustillos Avatar asked Mar 17 '16 17:03

Michelfrancis Bustillos


1 Answers

Credit to Solar on the AutoHotkey forums for proposing the following solution

Get text from edit controls with ControlGet

This method is a bit unreliable, as it will only work for specific control types. However, it may be the solution you are looking for, as it does not use the clipboard at all.

WinActive("A")                           ; sets last found window
ControlGetFocus, ctrl
if (RegExMatch(ctrl, "A)Edit\d+"))       ; attempt copying without clipboard
    ControlGet, text, Selected,, %ctrl%
}

Here's a proposed solution which attempts to copy text with ControlSend, but falls back to using the clipboard as a backup if needed.

WinActive("A")                           ; sets last found window
ControlGetFocus, ctrl
if (RegExMatch(ctrl, "A)Edit\d+"))       ; attempt copying without clipboard
    ControlGet, text, Selected,, %ctrl%  
else {                                   ; fallback solution
    clipboardOld := Clipboard            ; backup clipboard
    Send, ^c                             ; copy selected text to clipboard
    if (Clipboard != clipboardOld) {
        text := Clipboard                ; store selected text
        Clipboard := clipboardOld        ; restore clipboard contents
    }
}
MsgBox % text
like image 150
Stevoisiak Avatar answered Oct 10 '22 16:10

Stevoisiak