Jump to content


Auto Copy Selected Text to Clipboard


  • Please log in to reply
15 replies to this topic

#1 pwy

pwy
  • Guests

Posted 20 September 2005 - 08:53 PM

Inspired by ideas posted in 'Linux-like copy/paste with mouse' (Serenity, Laszlo) and 'Double click as hotkey?' (twwilliams, Chris), my following script will auto copy selected text to clipboard. The text can be selected by either
1)Shift + Arrow Keys (Up, Down, Left, Right),
2)Left Mouse Button, or
3)DoubleClick.

Also please check out Clipboard Recorder ( http://www.lw-works.com/ ), Very Handy!

;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, {
       x0 = A_CaretX
       y0 = A_CaretY
       break
     }
   }
   if (x-x0 > 5 or x-x0 < -5 or y-y0 > 5 or y-y0 < -5)
   {                             ; Caret has moved
      clip0 := ClipBoardAll      ; save old clipboard
      ;ClipBoard =
      Send ^c                    ; selection -> clipboard
      ClipWait 1, 1              ; restore clipboard if no data
      IfEqual ClipBoard,, SetEnv ClipBoard, %clip0%
   }
      return
   }
}

~LButton::
TimeButtonDown = %A_TickCount%
; Wait for it to be released
Loop
{
   Sleep 10
   GetKeyState, LButtonState, LButton, P
   if LButtonState = U  ; Button has been released.
      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
      clip0 := ClipBoardAll      ; save old clipboard
      ;ClipBoard =
      Send ^c                    ; selection -> clipboard
      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.
   {
      ;MouseClick, Left
      return
   }
}
; Since above didn't return, it's a double-click:
   Sleep, 100
   Send, ^c
return


#2 pwy

pwy
  • Guests

Posted 26 September 2005 - 09:12 PM

I just updated the code to add auto copy to:
1>Single click in Crimson Editor's selection area to select a single line;
2>Ctrl+A (Select All);
3>Tripple Click (Will select a sentence in Word, current line in some other apps)

;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
      clip0 := ClipBoardAll      ; save old clipboard
      ;ClipBoard =
      Send ^c                    ; selection -> clipboard
      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
         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
      clip0 := ClipBoardAll      ; save old clipboard
      ;ClipBoard =
      Send ^c                    ; selection -> clipboard
      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
      return
   }
}
;Tripple-click:
   Sleep, 100
   Send, ^c
return

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


#3 guestsp

guestsp
  • Guests

Posted 29 September 2005 - 06:02 PM

lotus notes crash with this script working. ¿any idea?

#4 pwy

pwy
  • Guests

Posted 30 September 2005 - 02:36 PM

I don't use Lotus Notes.

Try commenting out different parts of the script to find out what caused the crash.

#5 Stevie

Stevie
  • Members
  • 68 posts

Posted 17 August 2006 - 02:28 PM

hmm is there a way to replace text without feeding the clipboard with the selected text?

#6 Carlol

Carlol
  • Members
  • 163 posts

Posted 24 August 2006 - 07:14 AM

I´ve tried out this concept, and I love it.... just that I would really like to have it work without the double click selection, as it interfers with my using the Classic Media Player. Would it be possible to cut out the "double click" part of the script, and how to go about it? :-)

#7 robiandi

robiandi
  • Members
  • 49 posts

Posted 24 August 2006 - 10:22 AM

@CarloI:

I would really like to have it work without the double click selection

In both versions of pwy delete the part beginning with (up to the end)
; Otherwise, button was released quickly enough.  Wait to see if it's a double-click:
Then you have no Double Click, Tripple Click, Ctrl+A
If you are missing the Ctrl+A Hotkey, add at the end
~^a::Send, ^c


#8 Carlol

Carlol
  • Members
  • 163 posts

Posted 24 August 2006 - 10:36 AM

Robiandi "You're The Man" now it's perfect, you saved me hours of "tinkering" and thanks for thinking of how to include the "ctrl a" option again.
I´m most grateful. :-)

#9 robiandi

robiandi
  • Members
  • 49 posts

Posted 24 August 2006 - 01:37 PM

@CarloI: :D :D :D

#10 user

user
  • Members
  • 476 posts

Posted 12 June 2007 - 11:21 AM

@pwy

one of the most brilliant scripts!!!

thanks in advance for this!!!

just some questions:

1) does it use a second clipboard? not the windowsxp default? or the default clipboard? if it uses the default, so wont it replace the data I have copied to my clipboard by ctrl+c?

the best would be to store the selected text inside a second clipboard and not to interfere with the ctrl+c data

then to use the selected text, it would be best to have a seperate key, other than ctrl+v

the ctrl+v key should paste only the data that has been selected by ctrl+c

do you find my suggestion useful? or I miss something?

2) it doesnt seem to work well in opera, does anyone confirm? does it work with website text or only with text editor text ? (the first is only displayed text and the second text is editable text)

3) can I store multiple entries in the "second clipboard" and somehow choose which one to paste? something like multiple clipboard[/b]

4) can you make the "select by single clicking on the "line numbers" area to work in all editors? not just crimson editor?

5) there is also a problem, the script sometimes deletes the selected text and replaces it with c! I suppose the script sends ctrl+c after the selection, is there any other way to store the selected text?

6) another bug is that when I double click a desktop shortcut to open eg a file, it opens it and it drops it in the background (disabling the script returns to the default behaviour, where it opens it in the foreground) - it seems the script interferes in ALL double clicks and not in the SPECIFIC double clicks that occur over text, is there any way to fix this?

#11 user

user
  • Members
  • 476 posts

Posted 17 June 2007 - 12:41 PM

anyone please ????

#12 Laszlo

Laszlo
  • Fellows
  • 4713 posts

Posted 17 June 2007 - 03:55 PM

It is a two-year old script. AHK has changed a lot since it was posted. You can now write a more universal and shorter script. What are your requirements?

#13 user

user
  • Members
  • 476 posts

Posted 17 June 2007 - 04:42 PM

What are your requirements?


my requirements is what that script does plus my notes on my previous post

thanks


to sum up:

I need a script that will auto copy selected text to clipboard.
The text can be selected by either:
1)Shift + Arrow Keys (Up, Down, Left, Right),
2)Left Mouse Button, or
3)DoubleClick.
4) Single click in Crimson Editor's selection area to select a single line
5) Ctrl+A (Select All);
6) Tripple Click (Will select a sentence in Word, current line in some other apps)

plus my above post

#14 Laszlo

Laszlo
  • Fellows
  • 4713 posts

Posted 17 June 2007 - 11:11 PM

The script consists of independent parts: one handles the muse clicks, the second one handles special commands, like Ctrl-A, another part does all the Shift and Ctrl-Shift keys, which select text. Below is the first cut of the last part. When the keyboard is used to select text, a timer is re/started, which waits for the shift key to be released, when Ctrl-C is sent to the foreground application to copy the selection to the ClipBoard.
Selectors = Left.Up.Right.Down.Home.End.PgUp.PgDn ; Shifted or Ctrl-Shifted these keys select text
Loop Parse, Selectors, .
{
   HotKey  ~+%A_LoopField%, WatchShift
   HotKey ~^+%A_LoopField%, WatchShift
}
Return

WatchShift: ; Hotkey subroutine: re/start waiting for Shift-Up
   SetTimer ShiftUp, -1
Return

ShiftUp:    ; Wait until Shift is released then copy the selection to the keyboard
   KeyWait Shift
   Send ^c
Return


#15 Laszlo

Laszlo
  • Fellows
  • 4713 posts

Posted 17 June 2007 - 11:31 PM

2nd part: There is no universal way for selection, like selecting everything. Some applications use Ctrl-A, which, for example in Multi-Edit, changes the letter to the right of the cursor from lower case to capital or vice versa, but it does not select anything. Other applications select found phrases after Ctrl-F of F3, others use Shift-Ctrl-A/S/D/F/E/X for selecting characters, words, lines, etc. Therefore, you have to define hotkeys for your favorite applications, like this:
~^A::Send ^c
It does not change the original function of Ctrl-A, only follows it with sending Ctrl-C, to copy the newly selected text. You can precede it with some directives to allow- or forbid it in certain applications:
GroupAdd CtrlA, ahk_class OpusApp
GroupAdd CtrlA, ahk_class Notepad
#IfWinActive ahk_group CtrlA