user15964
user15964

Reputation: 2639

can somebody help to write a autohotkey script for select all the pasted content after paste?

the autohotkey script should do follows: after I tap the hotkey, it will paste the content from the clipboard, then immediately select all the pasted content?

I write a script as follows:

^+p::
clipvar:=Clipboard
num:=strlen(clipvar)
send ^v
send +{left %num%}
return

This script works. But the selecting process is too slow!!! Can somebody write a better script?

Upvotes: 0

Views: 263

Answers (1)

MCL
MCL

Reputation: 4065

SendMode, Input optionally combined with SetBatchLines, -1 and variations of SetKeyDelay can accelerate key sequences.
However, the selection of large texts will still take some time, and slow machines may slow it down even further.

Here's another approach which - in terms of sending keystrokes - is more efficient:

^+p::
    oldCaretX := A_CaretX
    oldCaretY := A_CaretY
    Send, ^v
    WaitForCaretChange()
    MouseGetPos, mX, mY
    MouseClickDrag, Left, %A_CaretX%, %A_CaretY%, %oldCaretX%, %oldCaretY%
    MouseMove, %mX%, %mY%
return

WaitForCaretChange() {
    oldCaretX := A_CaretX
    oldCaretY := A_CaretY
    while(A_CaretX = oldCaretX && A_CaretY = oldCaretY) {
        Sleep, 15
    }
}

This code relies on the window to expose the caret position, which unfortunately, not every window does. It remembers the caret position before the paste and selects text up to the old position after pasting new text; this should be equal to selecting all the newly inserted text. If you're only working with editors that expose their caret position, I recommend you go with this one since it's faster. Otherwise, you can still think about using both your method and this one, depending on the window and/or the text length.

Upvotes: 1

Related Questions