4

Similar to the question here, I found an AutohotKey script (solution) below in order to copy highlighted text and paste it by clicking on the middle button of the mouse. However there is still a lack in the script. This script only works when I highlight a text. I am wondering how I can modify it so that it also works when I double click on a word in the text and it gets highlighted.

cos_mousedrag_treshold := 20 ; pixels


#IfWinNotActive ahk_class ConsoleWindowClass

~lButton::
  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
      sendinput ^c
  }
  return

~mbutton::
  WinGetClass cos_class, A
  if (cos_class == "Emacs")
    SendInput ^y
  else
    SendInput ^v
  return

#IfWinNotActive


;; clipx
^mbutton::
  sendinput ^+{insert}
  return
H'H
  • 143

2 Answers2

7

You can modify the code like so:

~lButton::
  if (A_PriorHotKey = "~lButton" && A_TimeSincePriorHotkey < "500") {
    if (cos_class == "Emacs")
      sendinput !w
    else
      sendinput ^c
    }
  // rest of code //

Adjust 500 to higher or lower values in milliseconds if you double click faster/slower.

Nelson
  • 1,393
0

Using the excellent AHK-v2-script-converter linked here: https://github.com/mmikeww/AHK-v2-script-converter

I was able to convert this script to run in version 2.0 of Auto Hot Key. This script enables the following:

  • copy text on highlight
  • copy text on double click
  • paste text on middle click

Thanks to H'H for the original question/code example.

cos_mousedrag_treshold := 20 ; pixels

#HotIf !WinActive("ahk_class ConsoleWindowClass", )

~lButton:: { ; V1toV2: Added bracket /*

  • Detects a double click and copies text after a short delay

*/ If (A_TimeSincePriorHotkey != ''){ If (A_TimeSincePriorHotkey<400) and (A_PriorHotkey="~LButton"){ Sleep 100 SendInput("^c") return } }

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) { cos_class := WinGetClass("A") if (cos_class == "Emacs") SendInput("!w") else SendInput("^c") } return } ; V1toV2: Added Bracket before hotkey or Hotstring

~mbutton:: { ; V1toV2: Added bracket cos_class := WinGetClass("A") if (cos_class == "Emacs") SendInput("^y") else SendInput("^v") return } ; V1toV2: Added bracket in the end

#HotIf !WinActive(, )

;; clipx ^mbutton:: { ; V1toV2: Added bracket SendInput("^+{insert}") return } ; V1toV2: Added bracket in the end

I was not able to get double click copy suggested by Nelson to work with the included code but I fiddled for a bit and make something work. Could probably be improved.

Also, I'm noticing that my workflow is to often copy a block of text, highlight another block of text, and delete / replace it. With this script it automatically copies the second block of text (not the original text I wanted to paste). I keep looking for a solution to this, or adjust my workflow.

I'm also noticing the AHK sometimes throws errors when switching between applications, and in certain applications (like Excel), double click and click and drag cause things to behave differently then you would expect in Linux. So this script can certainly be improved be ignoring particular applications, or perhaps integrating its own clipboard.

Thomas
  • 1