Skip to content

Instantly share code, notes, and snippets.

@dvino
Last active May 4, 2021 18:01
Show Gist options
  • Save dvino/73772f6af97f0156ffdfa10e0c9d80e7 to your computer and use it in GitHub Desktop.
Save dvino/73772f6af97f0156ffdfa10e0c9d80e7 to your computer and use it in GitHub Desktop.
AutoHotkey sctipt for auto copy mouse selected plain text and past for middle mouse button
;For Firefox should set middlemouse.paste to true, in about:config
#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.
;Exclude applications for auto copy by mouse selected
GroupAdd, NoCopy, ahk_class CASCADIA_HOSTING_WINDOW_CLASS ;Windows Terminal
GroupAdd, NoCopy, ahk_class ConsoleWindowClass ;CMD and PowerShell
GroupAdd, NoCopy, ahk_exe Explorer.EXE ;Windows Explorer
GroupAdd, NoCopy, ahk_class TTOTAL_CMD ;Total Commander
;Exclude applications for past by mouse middle button
GroupAdd, NoPast, ahk_class MozillaWindowClass
GroupAdd, NoPast, ahk_class CASCADIA_HOSTING_WINDOW_CLASS
GroupAdd, NoPast, ahk_class TTOTAL_CMD
CopyPlain() ; Text-only copy to ClipBoard
{
Clip0 = %ClipBoardAll%
ClipBoard =
Send ^c
ClipWait 2 ; Wait for text, up to 2s
If ErrorLevel
ClipBoard = %Clip0% ; Restore original ClipBoard
Else
ClipBoard = %ClipBoard% ; Convert to text
VarSetCapacity(Clip0, 0) ; Free memory
Return
}
#IfWinNotActive ahk_group NoCopy
~LButton::
if (A_ThisHotkey = A_PriorHotkey && A_TimeSincePriorHotkey < 200) ;double and tripple click celection text copy
Sleep, 200
CopyPlain() ;;SendInput ^c
cos_mousedrag_treshold := 20 ; pixels
MouseGetPos, cos_mousedrag_x, cos_mousedrag_y
keywait lbutton
mousegetpos, cos_mousedrag_x2, cos_mousedrag_y2
if (abs(cos_mousedrag_x2 - cos_mousedrag_x) > cos_mousedrag_treshold
or abs(cos_mousedrag_y2 - cos_mousedrag_y) > cos_mousedrag_treshold)
{
WinGetClass cos_class, A
if (cos_class == "Emacs")
SendInput !w
else
CopyPlain()
}
Return
#IfWinNotActive ahk_group NoPast
~MButton::
WinGetClass cos_class, A
;; emacs does pasting on middleclick by itself
if (cos_class <> "Emacs")
SendInput ^v
Return
#IfWinActive ahk_class CASCADIA_HOSTING_WINDOW_CLASS
~MButton::
SendInput ^+v
Return
@insign
Copy link

insign commented May 4, 2021

Locked my mouse, here a working version for me: https://gist.github.com/insign/96e451503bdb927c6e38c8e8f885bc7c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment