Autocopy text to clipboard

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
fmen
Posts: 7
Joined: 26 Nov 2021, 08:44

Autocopy text to clipboard

Post by fmen » 28 Dec 2021, 16:04

Needless to say, I am new to AHK, any help would be appreciated.
This is a terrific snippet of code that allows copying of highlighted text into clipboard.
How can I prevent copying text when CTRL is held down? Tried using GetKeyState, state, Ctrl and {LCtrl Up} without success.

Code: Select all

;=====================================AutoCopy Text========================
~LButton::
 	  MouseGetPos x0, y0            ; save start mouse position
 	  KeyWait LButton
 	  ;GetKeyState, state, Ctrl
  	  MouseGetPos x, y              ; position when button released
  	  If (abs(x-x0) > 5 or abs(y-y0) > 5) and {LCtrl Up} {
   	  	 Send ^c                    ; selection -> clipboard
     	  ClipWait 2, 1              ; restore clipboard if no data
     	  If ErrorLevel
        	 ClipBoard := clip0
	  ToolTip, Copied to Clipboard
	  	Sleep 1000
	  Tooltip
   }
Return

fmen
Posts: 7
Joined: 26 Nov 2021, 08:44

Re: Autocopy text to clipboard

Post by fmen » 28 Dec 2021, 16:11

Got it on my own!
Kind of proud....

Code: Select all

;=====================================AutoCopy Text========================
~LButton::
   MouseGetPos x0, y0            ; save start mouse position
   KeyWait LButton
   GetKeyState, state, LCtrl
   MouseGetPos x, y              ; position when button released
   If (abs(x-x0) > 5 or abs(y-y0) > 5) and (state = "U") {
      Send ^c                    ; selection -> clipboard
      ClipWait 2, 1              ; restore clipboard if no data
      If ErrorLevel
         ClipBoard := clip0
	  ToolTip, Copied to Clipboard
	  Sleep 1000
	  Tooltip
   }
Return

User avatar
flyingDman
Posts: 2776
Joined: 29 Sep 2013, 19:01

Re: Autocopy text to clipboard

Post by flyingDman » 28 Dec 2021, 16:25

You should be! The joys of programming... :bravo:
14.3 & 1.3.7

rivolity
Posts: 1
Joined: 28 Mar 2023, 18:43

Re: Autocopy text to clipboard

Post by rivolity » 28 Mar 2023, 18:47

@fmen your script is awesome, have you tried to convert it to autohotkey V2 ? :salute:

User avatar
boiler
Posts: 16709
Joined: 21 Dec 2014, 02:44

Re: Autocopy text to clipboard

Post by boiler » 28 Mar 2023, 20:36

rivolity wrote: @fmen your script is awesome, have you tried to convert it to autohotkey V2 ? :salute:
It’s not that long of a script. If you have a need for it, I suggest you take on the task of updating it for v2 and post your results (or questions if you get stuck) here.

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Autocopy text to clipboard

Post by mikeyww » 28 Mar 2023, 20:59

Hello,

GetKeyState is a deprecated command, so I would use the GetKeyState function instead.

Since ClipWait waits for the clipboard to be populated, the command has no effect if the clipboard was already populated before your clipping. As a result, you may get a false-negative ErrorLevel, because even clipping nothing could lead ErrorLevel to be zero. You can actually confirm this yourself in a simple test where you clip nothing. When the clipboard is already populated, ErrorLevel will be zero.

Code: Select all

#Requires AutoHotkey v1.1.33
Clipboard := 1
SoundBeep 1500

F3::
; Clipboard := ""
Send ^c
ClipWait 0
MsgBox % ErrorLevel
Return
Here is an example of how to use ClipWait properly. https://www.autohotkey.com/docs/v1/lib/ClipWait.htm#ExBasic

My script does the same thing, but I commented the clipboard reset to demonstrate the bug.

Jasonosaj
Posts: 46
Joined: 02 Feb 2022, 15:02
Location: California

Re: Autocopy text to clipboard

Post by Jasonosaj » 30 Mar 2023, 11:32

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
	
	

Post Reply

Return to “Ask for Help (v1)”