Optimized script for selection highlight auto-copying?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mct300
Posts: 5
Joined: 10 Dec 2017, 16:38

Optimized script for selection highlight auto-copying?

10 Dec 2017, 17:05

Hello AutoHotKey community. Linux has auto copy selected text to clipboard feature so that whatever you select is automatically on the clipboard without having to right click and select copy or pressing any hotkey. I have found this script but it seems that it's an old topic and it might not be the most optimized code. I need to automatically call a function whenever I select a text and save it somewhere. One of the problem that I have with the mentioned script is that it calls my target function even when I am holding the LButton for few second and move it (like scroll down using mouse). Can you introduce a better script or help me to code it?

I want the text to be selected by all possible ways
1) Shift + Arrow Keys (Up, Down, Left, Right)
2) Left Mouse Button
3) DoubleClick
4) Single click to select a single line
5) Ctrl+A (Select All)
6) Tripple Click (Will select the current line)

Code: Select all

#Persistent
#SingleInstance Force
#NoEnv  ;~ Recommended for performance and compatibility with future AutoHotkey releases.

SendMode Input  ;~ Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ;~ Ensures a consistent starting directory.
SetTitleMatchMode, 3
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;Auto copy clipboard
~Lshift::
TimeButtonDown = %A_TickCount%
; Wait for it to be released
Loop
{
   Sleep 10
   GetKeyState, LshiftState, Lshift, P
   if LshiftState = U  ; Button has been released.
      break
   elapsed = %A_TickCount%
   elapsed -= %TimeButtonDown%
   if elapsed > 200  ; Button was held down long enough
   {
      x0 = A_CaretX
      y0 = A_CaretY
      Loop
   {
     Sleep 20                    ; yield time to others
     GetKeyState keystate, Lshift
     IfEqual keystate, U, {
       x = A_CaretX
       y = A_CaretY
       break
     }
   }
   if (x-x0 > 5 or x-x0 < -5 or y-y0 > 5 or y-y0 < -5)
   {                             ; Caret has moved
	  global clip0
      clip0 := ClipBoardAll      ; save old clipboard
      ;ClipBoard =
      ;~ Send ^c                    ; selection -> clipboard
	  MakeDecision()
      ClipWait 1, 1              ; restore clipboard if no data
      IfEqual ClipBoard,, SetEnv ClipBoard, %clip0%
   }
      return
   }
}

~LButton::
MouseGetPos, xx
TimeButtonDown = %A_TickCount%
; Wait for it to be released
Loop
{
   Sleep 10
   GetKeyState, LButtonState, LButton, P
   if LButtonState = U  ; Button has been released.
   {
      If WinActive("Crimson Editor") and (xx < 25) ; Single Click in the Selection Area of CE
      {
         ;~ Send, ^c
		 MakeDecision()
         return
      }
      break
   }
   elapsed = %A_TickCount%
   elapsed -= %TimeButtonDown%
   if elapsed > 200  ; Button was held down too long, so assume it's not a double-click.
   {
      MouseGetPos x0, y0            ; save start mouse position
      Loop
   {
     Sleep 20                    ; yield time to others
     GetKeyState keystate, LButton
     IfEqual keystate, U, {
       MouseGetPos x, y          ; position when button released
       break
     }
   }
   if (x-x0 > 5 or x-x0 < -5 or y-y0 > 5 or y-y0 < -5)
   {                             ; mouse has moved
      global clip0
	  clip0 := ClipBoardAll      ; save old clipboard
      ;ClipBoard =
      ;~ Send ^c                    ; selection -> clipboard
	  MakeDecision()
      ClipWait 1, 1              ; restore clipboard if no data
      IfEqual ClipBoard,, SetEnv ClipBoard, %clip0%
   }
      return
   }
}
; Otherwise, button was released quickly enough.  Wait to see if it's a double-click:
TimeButtonUp = %A_TickCount%
Loop
{
   Sleep 10
   GetKeyState, LButtonState, LButton, P
   if LButtonState = D  ; Button has been pressed down again.
      break
   elapsed = %A_TickCount%
   elapsed -= %TimeButtonUp%
   if elapsed > 350  ; No click has occurred within the allowed time, so assume it's not a double-click.
      return
}

;Button pressed down again, it's at least a double-click
TimeButtonUp2 = %A_TickCount%
Loop
{
   Sleep 10
   GetKeyState, LButtonState2, LButton, P
   if LButtonState2 = U  ; Button has been released a 2nd time, let's see if it's a tripple-click.
      break
}
;Button released a 2nd time
TimeButtonUp3 = %A_TickCount%
Loop
{
   Sleep 10
   GetKeyState, LButtonState3, LButton, P
   if LButtonState3 = D  ; Button has been pressed down a 3rd time.
      break
   elapsed = %A_TickCount%
   elapsed -= %TimeButtonUp%
   if elapsed > 350  ; No click has occurred within the allowed time, so assume it's not a tripple-click.
   {  ;Double-click
      ;~ Send, ^c
	  MakeDecision()
      return
   }
}
;Tripple-click:
   Sleep, 100
   ;~ Send, ^c
   MakeDecision()
return

~^a::
;~ Send, ^c ;Ctl+A = Select All, then Copy
MakeDecision()
return




MakeDecision() {
SendInput, ^c
ClipWait, 5
IfEqual ClipBoard,, MsgBox, Don't copy it isn't important
MsgBox, copied to clipboard
}
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Optimized script for selection highlight auto-copying?

10 Dec 2017, 23:36

Try using KeyWait, LButton. That will wait until you release the left mouse button before proceeding with the script.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 206 guests