Super cool idea. Did some iterating, cleanup, and commenting to explain changes. I'm not SUPER good at AHK, but this should cover some issues that I experienced when I was using a similar script in the past.
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
;=====================================AutoCopy Text========================
~LButton:: ; hotkey to automatically copy highlighted text or media
SecondClipboard_Copy()
Return
^#v::
SecondClipboard_Paste(ClipBoard2) ; hotkey, currently control + win + v, to paste data copied automatically. PRESERVES clipboard in case of accidental copying
Return
SecondClipBoard_Copy()
{
Global ClipBoard2 ; people hate globals but I don't grok the static/byRef relationship and fall back on them. Remember to zero out/set globals to null after you use them
MouseGetPos x0, y0 ; posiiton when left button is pressed
KeyWait, LButton, U T3 ; wait for left mouse button to be released, terminate function if longer than three seconds
If !Errorlevel ; if NOT timeout on left button, proceed with copy; otherwise, end function
{ ;
MouseGetPos x1, y1 ; position when left button released
XDif:=abs(x1-x0) ; check for a change in mouse position between depression and release in the x plane
YDif:=abs(y1-y0) ; check for a change in mouse position between depression and release in the y plane
LControlState := GetKeyName(LControl) ; is left control depressed? if so, end function
If (XDif>5 or YDif>5 && LControlState == 0) ; check for change in x or y plan + lcontrol in up position
{
Clipbak := ClipBoardAll ; save clipboard
Clipboard := "" ; empty clipboard
Send ^c ; copy
ClipWait, 1 ; give clipboard time to store copied info
If !ErrorLevel ; if copy succeeded, proceed, else restore the saved clipboard data
{
ClipBoard2 := Clipboard ; if copy succeeded, save copied test to the Clipboard2 variable and free up clipboard to restore its original contents. Avoids issues with accidentally copying highlighted text while reading or whatever
tooltip, %Clipboard2% copied, press control alt v to paste
SetTimer, RemoveTooltip,-1000 ; go to the RemoveTooltip sub after displaying tooltip for one second
}
ClipBoard := Clipbak ; whether copy succeeded or failed, restore the original clipboard so that you are back to pre-copy status
Clipwait 1 ; wait up to one second for clipboard data to be restored
}
}
Return
}
SecondClipboard_Paste(ClipBoard2)
{
Clipbak := ClipBoardAll ; backup your clipboard data
Clipboard := ClipBoard2 ; set the auto-copied text to the clipboard for pasting. Clipboard is WAY faster than "send, % Clipboard2"
Clipwait 1 ; wait up to one second for autocopied data to be set in the clipboard
Send, ^v ; paste the autocopied data
Clipboard := Clipbak ; return clipboard to pre-autocopy/paste status
ClipBoard2 := "" ; zero out clipboard2, since we're lazy and using a global variable
Return
}
RemoveTooltip:
ToolTip ; dismiss all existing tooltips
Return