6

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.

  • Where is the textfield from which you want to copy text? Is it a normal windows forms field; can you see it's classNN using the window spy utility? – Forivin Mar 18 '16 at 11:25
  • @2501 Both attempt and problem are contained in the question. – Michelfrancis Bustillos Mar 18 '16 at 13:14
  • @Forivin I is usually within a Word or Notepad document, but sometimes is text on a webpage, in a PDF, etc. – Michelfrancis Bustillos Mar 18 '16 at 13:15
  • 4
    The question is fine. He's not asking for a code rewrite. He's simply asking how to get the text selection without using the clipboard. That is specific enough if you ask me. That being said, I don't know the solution. Reading text selections from controls that have a classNN is easy. But for custom controls there is no native AHK way. [I'm currently trying to find out how this works.](http://stackoverflow.com/questions/36083784/winapi-getting-text-selection-of-active-window-without-using-the-clipboard/36084300) Depending on how complicated it is, I might write an AHK function to do it. – Forivin Mar 18 '16 at 13:43
  • @MichelfrancisBustillos Okay it is possible using `UIAutomation`. Someone already wrote a set of classes for autohotkey that could take care of this: https://github.com/neptercn/UIAutomation/blob/master/UIA.ahk But man is this complicated. You probably have to read into [this stuff](https://msdn.microsoft.com/en-us/library/windows/desktop/ee684009.aspx) quite a bit. But from what I can tell you will need to create a `IUIAutomationElement` instance, call `GetCurrentPattern(patternId)` on that (not sure how to retrieve patternId) and then call `Text_GetSelection()` on the result of that. – Forivin Mar 18 '16 at 16:14
  • https://github.com/neptercn/UIAutomation/blob/master/sample3.ahk I think this example shows how to get the active element. Now you'd "just" need to the stuff mentioned above to that. – Forivin Mar 18 '16 at 16:16
  • This posted code doesn't seem to be functional, even with the use of the clipboard. – Stevoisiak Aug 16 '17 at 20:49

2 Answers2

3

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
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
-1

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::
     ....
phil294
  • 10,038
  • 8
  • 65
  • 98