Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Copy on select implementation


  • Please log in to reply
21 replies to this topic
  • Guests
  • Last active:
  • Joined: --
Here's an implementation for copy on select. You can simply select some text and it is copied immediately without having to press ctrl+c.

Conveinent middle button paste is also provided, but with a twist. Middlebutton is useful in browsers, so there is a short time (configurable) while you can paste with the middle button after copy, before original middleclick functionality is restored.

Also, if you click with the right button it cancels middlebutton paste.

Since after selection you usually click with the left button to the place where you want to paste, so that you put the focus there, it is done automatically when using middlebutton pasting.

clipx integration is also added to Ctrl+middlebutton.

I've been using the script for a few days and it's quite convenient. Sometimes the copy tooltip appears unwantedly when something is dragged, but it doesn't cause any problems.

Note: improved code is posted later in this thread.
mousedrag_treshold := 20 ; pixels
middleclick_available := 15 ; seconds

Hotkey mbutton, paste_selection
Hotkey mbutton, off
Hotkey rbutton, cancel_paste
Hotkey rbutton, off
    
    
#IfWinNotActive ahk_class ConsoleWindowClass
~lButton::
  MouseGetPos, mousedrag_x, mousedrag_y
  keywait lbutton
  mousegetpos, mousedrag_x2, mousedrag_y2
  if (abs(mousedrag_x2 - mousedrag_x) > mousedrag_treshold
    or abs(mousedrag_y2 - mousedrag_y) > mousedrag_treshold)
  {
    wingetclass class, A
    if (class == "Emacs")
      sendinput !w
    else
      sendinput ^c
    settimer follow_mouse, 100
    settimer cleanup, % middleclick_available * 1000
    hotkey mbutton, on
    hotkey rbutton, on
  }
  return
#IfWinNotActive
  
  
follow_mouse:
  tooltip copy
  return
  
paste_selection:
  sendinput {lbutton}
  WinGetClass class, A
  if (class == "Emacs")
    SendInput ^y
  else
    SendInput ^v
  gosub cleanup
  return
  
cancel_paste:
  sendinput {rbutton}
  gosub cleanup
  return  
  
cleanup:
  Hotkey mbutton, off
  Hotkey rbutton, off
  SetTimer cleanup, off
  settimer follow_mouse, off
  tooltip
  Return
  
  
;; clipx
^mbutton::
  sendinput ^+{insert}
  return


Chicken Pie 4 Tea
  • Members
  • 379 posts
  • Last active: Oct 02 2014 11:32 AM
  • Joined: 18 Aug 2009
Hi I like your script but one thing really bugs me!
the "copy" tooltip appears a lot not just when i am dragging , how can i get rid of it, as it really isnt needed at all.
Oh I see the bit where it says "follow mouse tootip copy" I'll just delete the tooltip bit.
done so , now working as i like!

  • Guests
  • Last active:
  • Joined: --
The tooltip is only there to indicate how long you can use middlebutton paste. Not really important.

  • Guests
  • Last active:
  • Joined: --
Anyone has an idea how to distinguish between text selection and dragging (or changing the volume on a slider with the mouse, etc.)?

  • Guests
  • Last active:
  • Joined: --
Here's a new, dramatically simplified version (no timers, etc.) which can do the same as the previous version.

Turns out there is no need to handle middlebutton pasting smartly in order not to interfere with the useful "open in background" feature of browsers. The solution is to use ~ in the hotkey, so that the original function of a middlebutton click is not overridden and paste the selection anyway.

If you click with the middlebutton on a link then pasting will have no effect, so it won't interfere with anything!

Firefox users should set middlemouse.paste to true, in about:config, so that they can paste stuff into textfields.

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


Note that all variables are uniquely prefixed, so you can integrate it easily into your main hotkey script.

  • Guests
  • Last active:
  • Joined: --
Here's an other version for Firefox users who set middlemouse.paste to true. This script doesn't paste wjen the middle button is pressed, it lets firefox do the work:

#IfWinNotActive ahk_class ConsoleWindowClass

~lButton::
  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
      sendinput ^c
  }
  return
  
; firefox does pasting on middleclick by itself
#IfWinNotActive ahk_class MozillaUIWindowClass

~mbutton::
  WinGetClass cos_class, A
  ;; emacs does pasting on middleclick by itself
  if (cos_class <> "Emacs")
    SendInput ^v
  return
  
#IfWinNotActive


;; clipx
^mbutton::
  sendinput ^+{insert}
  return


  • Guests
  • Last active:
  • Joined: --
I suggest trying this script if you haven't done it already. The concept is very useful. After using the script for a while I can't image going back to the old Ctrl+C copying method, it's so efficient.

For best experience utilize a clipboard manager too. E.g. clipx

Gauss
  • Members
  • 203 posts
  • Last active: Jan 27 2012 12:49 PM
  • Joined: 10 Sep 2009
Ok, lets post here, better for your script, I will use it for a while and post bugs here

  • Guests
  • Last active:
  • Joined: --
Not my script, I just use it. :) I wrote into the other topic only, so you can try it if you don't know about it. I'm generally quite happy with this one.

Nikos
  • Guests
  • Last active:
  • Joined: --
Another idea for pasting is to click and hold the left mouse button and then click the middle button to paste.

What do you think?

  • Guests
  • Last active:
  • Joined: --

Another idea for pasting is to click and hold the left mouse button and then click the middle button to paste.

What do you think?



What's wrong with middlebutton only? Does it interfere with normal behavior somewhere?

Prime
  • Guests
  • Last active:
  • Joined: --

Another idea for pasting is to click and hold the left mouse button and then click the middle button to paste.

What do you think?



What's wrong with middlebutton only? Does it interfere with normal behavior somewhere?


for me the Middle Click option doesn't seem to work since I'm using Chrome... it would be great if we could hold down the Right Mouse Button and then click the Left Mouse Button as a hotkey to paste the copied text.

I tried modifying it (see below commented code) but after I hooked the Right Mouse Button even the Left Click copying stopped working :S

cos_mousedrag_treshold := 20 ; pixels

#IfWinNotActive ahk_class ConsoleWindowClass

~LButton::
	MouseGetPos, cos_mousedrag_x, cos_mousedrag_y
	
	KeyWait, LButton, U T0.2
	If (ErrorLevel)
	{
		KeyWait, RButton, D T5
		If (ErrorLevel)
		{
			Return
		}
		Else
		{
			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 )
			{
				SendInput ^c
				Return
			}
		}
	}
	Return

;~RButton::
;	KeyWait, RButton, U T0.2
;	If (ErrorLevel)
;	{
;		KeyWait, LButton, D T1
;		If (ErrorLevel)
;		{
;			Return
;		}
;		Else
;		{
;			SendInput ^v
;		}
;	}
;	Return


~Mbutton::
	SendInput ^v
	Return


  • Guests
  • Last active:
  • Joined: --

for me the Middle Click option doesn't seem to work since I'm using Chrome... it would be great if we could hold down the Right Mouse Button and then click the Left Mouse Button as a hotkey to paste the copied text.

I tried modifying it (see below commented code) but after I hooked the Right Mouse Button even the Left Click copying stopped working :S


It could surely be done, but it would need some trial and error to get it right.

Why don't you try something simpler instead like shift+middle click or shift+right click?


+rbutton:: 
   SendInput ^v 
   Return

It's still quite convenient and it's easier to implement.

User4528
  • Members
  • 60 posts
  • Last active: Feb 26 2013 03:59 AM
  • Joined: 30 Jul 2009
Would someone be able to make a change to the script to allow copying when dragging and holding left click then middle click? Pasting will be done with rapid double middle mouse click.

Prime
  • Guests
  • Last active:
  • Joined: --

It could surely be done, but it would need some trial and error to get it right.

Why don't you try something simpler instead like shift+middle click or shift+right click?

It's still quite convenient and it's easier to implement.


The reason I wanted a Mouse only script was since when I'm browsing, I tend to use just the mouse :) All these time I was using the Right Click and then select COPY to copy text, and then used to go to the required field and again right click and PASTE to paste it.. so finally I managed to remove those 2 extra clicks with the menu :)

New script works like this..

Copy - Drag and Select with the LEFT Mouse Button, and while holding it, click the RIGHT Mouse Button to copy the selected text/folders/files etc.
Paste - Right Click where the contents need to be pasted, and while holding the RIGHT Mouse Button, LEFT Click to paste the items in the clipboard.

Please feel free to modify as required :)

#IfWinNotActive ahk_class ConsoleWindowClass

bAllowOverride := False

~LButton::
	GetKeyState, keystate, RButton
	If (keystate = "D")
	{
		SendInput {RButton Up}
		SendInput {Escape}
		SendInput ^v
		bAllowOverride := True
	}
	Return

RButton::
	GetKeyState, keystate, LButton
	If (keystate = "D")
	{
		SendInput {LButton Up}
		SendInput ^c
		bAllowOverride := True
		Return
	}
	SendInput {RButton Down}
	Return

RButton Up::
	GetKeyState, keystate, LButton
	If (keystate = "D")
	{
		Return
	}
	If (bAllowOverride)
	{
		bAllowOverride := False
		Return
	}
	SendInput {RButton Up}
	Return