Is there a way to copy-on-select without sending ^c ?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
cnd
Posts: 2
Joined: 31 Mar 2024, 06:32

Is there a way to copy-on-select without sending ^c ?

31 Mar 2024, 06:41

I use two different text-based programs (a terminal emulator, and VS-Code with VIM editor). Both of them do not "copy" when you press Ctrl-C (ctrl-C does something else in those sessions). Right-click -> Copy still works of course (and whatever underlying windows API calls triggers a copy too still works obviously).

Is there a way to write an AHK script that automatically copies whatever text I select with my mouse, from either of these 2 programs, into my clipboard?

I want to be able to mark and copy with the left button only, and to paste with the middle button - same as linux does...

I found this script from superuser whioch doesn't work in AHK v2 - which if I can get around the "sendinput ^c" problem, looks like most of the work I need to do here

Code: Select all

cos_mousedrag_treshold := 20 ; pixels
cos_copied_text := ""

#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 {
      previous_clipboard := clipboard
      sendinput ^c
      sleep 50 ; wait for copied text to be stored in clipboard
      cos_copied_text := clipboard
      clipboard := previous_clipboard
    }
  }
  return

~mbutton::
  WinGetClass cos_class, A
  if (cos_class == "Emacs")
    SendInput ^y
  else {
    previous_clipboard := clipboard
    clipboard := cos_copied_text ; copy stored text to clipboard
    SendInput ^v
    sleep 50 ; I'm not sure if this is necessary
    clipboard := previous_clipboard
  }
  return

#IfWinNotActive

;; clipx
^mbutton::
  sendinput ^+{insert}
  return
 
User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: Is there a way to copy-on-select without sending ^c ?

31 Mar 2024, 07:50

Welcome to this AutoHotkey forum!

Code: Select all

; This script copies selected text
#Requires AutoHotkey v2.0

MButton::Send '^v'

~LButton:: {
 Static minDist := 10
 MouseGetPos &x1, &y1
 KeyWait 'LButton'
 MouseGetPos &x2, &y2
 If Abs(x2 - x1) >= minDist || Abs(y2 - y1) >= minDist
  Send '^c'
}
lexikos
Posts: 9665
Joined: 30 Sep 2013, 04:07
Contact:

Re: Is there a way to copy-on-select without sending ^c ?

31 Mar 2024, 20:10

Console windows have a menu which can be invoked via MenuSelect. There's an example in the documentation for pasting, which should be simple to modify for copying. This is only for ahk_class ConsoleWindowClass, not custom console windows or Windows Terminal.

@mikeyww "without sending ^c"; "Both of them do not copy when you press Ctrl-C"
User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: Is there a way to copy-on-select without sending ^c ?

31 Mar 2024, 20:21

I see; sorry I misunderstood this one.
cnd
Posts: 2
Joined: 31 Mar 2024, 06:32

Re: Is there a way to copy-on-select without sending ^c ?

01 Apr 2024, 16:34

@lexikos - thanks for those hints; it's gotten me a bit closer to my goal :-)

Apparently, we can use https://www.autohotkey.com/docs/v2/lib/PostMessage.htm to trigger the copy without the ^C key hack... except not all applications process that (Notepad etc - yes - but VS-Code's vim editor, sadly, no...)

my test, if anyone's interested. Use Spy++ to get the HWND ( c:/Program Files/Microsoft Visual Studio/2022/Community/Common7/Tools/spyxx.exe )

Code: Select all

#!/usr/bin/env python
  
__version__ = '1.0.0' # Major.Minor.Patch

# This is free and unencumbered software released into the public domain
# under "The Unlicense" terms: https://choosealicense.com/licenses/unlicense/

"""
=head1 arrange.py - rectangle-based part distribution algorithm


=cut
"""


import win32con, win32gui, sys,os



def main():
    if len(sys.argv) < 2:
        print("Usage: {} hex_handle".format(sys.argv[0]))
        sys.exit(1)

    #hwndControl = sys.argv[1] # The handle to the control with selected text
    hwndControl = int(sys.argv[1], 16)  # The handle to the control with selected text
    win32gui.SendMessage(hwndControl, win32con.WM_COPY, 0, 0)

    print("Send WM_COPY to {}".format(hwndControl))
    sys.exit(0)


if __name__ == '__main__':
    main()
Descolada
Posts: 1183
Joined: 23 Dec 2021, 02:30

Re: Is there a way to copy-on-select without sending ^c ?

01 Apr 2024, 23:26

@cnd could also try UIA-v2, which works in a lot of programs:

Code: Select all

#Requires AutoHotkey v2
#include UIA.ahk

F2::try ToolTip UIA.GetFocusedElement().GetSelection()[1].GetText()
It doesn't work in VSCode out-of-the-box: you need to press Ctrl+Shift+P, open the user settings file, and set "editor.accessibilitySupport": "on"

Known limitation: doesn't work in Chrome address bar.
reupdereut
Posts: 2
Joined: 06 May 2024, 02:58

Re: Is there a way to copy-on-select without sending ^c ?

06 May 2024, 07:41

mikeyww wrote:
31 Mar 2024, 07:50
Welcome to this AutoHotkey forum!

Code: Select all

; This script copies selected text
#Requires AutoHotkey v2.0
..............................................
}
Hello, I have reused your script as part of another one that I posted here :
viewtopic.php?p=570700#p570700
as it should, I gave you credit for it, just wanted to let you know

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: docterry, Rohwedder and 19 guests