Get Selected Text Without Using the Clipboard

后端 未结 2 922
感动是毒
感动是毒 2021-01-02 23:52

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 pas

相关标签:
2条回答
  • 2021-01-03 00:23

    Your "clipboard manager" will most probably work with ctrl+c. Add the $ option, so it won't get triggered by your alt+r-hotkey, thus not intervened.

    $^c::
         ....
    
    0 讨论(0)
  • 2021-01-03 00:24

    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
    
    0 讨论(0)
提交回复
热议问题