Jump to content


Copy on select implementation


  • Please log in to reply
21 replies to this topic

#1 Guests

  • Guests

Posted 19 August 2009 - 06:51 AM

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


#2 Chicken Pie 4 Tea

Chicken Pie 4 Tea
  • Members
  • 377 posts

Posted 20 August 2009 - 11:21 AM

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!

#3 Guests

  • Guests

Posted 20 August 2009 - 02:08 PM

The tooltip is only there to indicate how long you can use middlebutton paste. Not really important.

#4 Guests

  • Guests

Posted 20 August 2009 - 02:09 PM

Anyone has an idea how to distinguish between text selection and dragging (or changing the volume on a slider with the mouse, etc.)?

#5 Guests

  • Guests

Posted 20 August 2009 - 08:37 PM

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.

#6 Guests

  • Guests

Posted 28 August 2009 - 06:34 AM

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


#7 Guests

  • Guests

Posted 25 September 2009 - 08:17 AM

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

#8 Gauss

Gauss
  • Members
  • 203 posts

Posted 29 September 2009 - 03:37 PM

Ok, lets post here, better for your script, I will use it for a while and post bugs here

#9 Guests

  • Guests

Posted 29 September 2009 - 03:41 PM

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.

#10 Nikos

Nikos
  • Guests

Posted 16 October 2009 - 07:59 AM

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?

#11 Guests

  • Guests

Posted 16 October 2009 - 01:27 PM

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?

#12 Prime

Prime
  • Guests

Posted 17 October 2009 - 03:18 PM

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


#13 Guests

  • Guests

Posted 18 October 2009 - 07:01 AM

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.

#14 User4528

User4528
  • Members
  • 60 posts

Posted 18 October 2009 - 07:41 AM

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.

#15 Prime

Prime
  • Guests

Posted 18 October 2009 - 12:18 PM

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