使用AutoHotKey的中间鼠标按钮复制并粘贴”^C”到WSL终端。

huangapple go评论99阅读模式
英文:

Copy-paste with AutoHotKey using middle mouse button pastes "^C" into WSL terminal

问题

为了使我的WSL体验更像Linux,我尝试使用AutoHotKey允许中间鼠标按钮粘贴突出显示的文本。为此,我使用了这里的答案(取自这里),即创建并运行一个AutoHotKey .ahk 脚本文件,其中包含以下内容:

cos_mousedrag_treshold := 20 ; 像素
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 ; 等待复制的文本被存储在剪贴板中
      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 ; 将存储的文本复制到剪贴板
    SendInput ^v
    sleep 50 ; 我不确定这是否必要
    clipboard := previous_clipboard
  }
  return

#IfWinNotActive

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

这通常运行良好,但每次我首次左键单击运行Ubuntu的Windows终端(或Windows PowerShell终端)时,它会在命令提示符上输出"^C"。在同一个终端中的后续左键单击不会产生这个问题,但如果在之间切换了活动窗口,它会再次发生。

有人有任何想法如何通过修改上面的脚本来阻止这种情况发生吗?

注意:上面的脚本只能在AutoHotKey v1.1或更低版本中运行,而不能在v2中运行。如果有人有建议如何在v2中运行它,那也会很有用。

英文:

In order to make my WSL experience more Linux-y, I've tried using AutoHotKey to allow the middle mouse button to paste highlighted text. To do this I have used the answer here (taken from here), i.e., create and run an AutoHotKey .ahk script file containing:

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

This generally works well, but every time I first left mouse click on a Windows Terminal running Ubuntu (or, for that matter, a Windows Powershell terminal) it outputs "^C" on the command prompt. Subsequent left clicks in the same terminal do not produce this, but it happens again if you have switched the active window in between.

Does anyone have an idea how to stop this happening with an amendment to the above script?

> Note: the above script will only run with AutoHotKey v1.1 or less, and not v2. If anyone has suggestions of what to change to get it running in v2, that would also be useful.

答案1

得分: 1

在脚本执行任何复制或发送数据到剪贴板之前,建议清空剪贴板并使用ClipWait,以确保指定的数据已出现在剪贴板上:

~mbutton::
  WinGetClass cos_class, A
  if (cos_class == "Emacs")
    SendInput ^y
  else 
  {
    ClipSaved := ClipboardAll    ; 将整个剪贴板保存到变量ClipSaved中
    clipboard := ""              ; 清空剪贴板(开始时为空以允许ClipWait检测到文本已到达)
    clipboard := cos_copied_text ; 复制存储的文本到剪贴板
    ClipWait, 1                  ; 等待最多1秒钟,以确保剪贴板包含数据。 
    If (!ErrorLevel)             ; 如果没有ErrorLevel,ClipWait找到了剪贴板上的数据
      SendInput ^v               ; 粘贴存储的文本
    else
      MsgBox, 48, Error!, 无法复制存储的文本!
    Sleep, 300                   ; 在粘贴时不要更改剪贴板!
    clipboard := ClipSaved       ; 恢复原始剪贴板
    VarSetCapacity(ClipSaved, 0) ; 释放变量ClipSaved的内存	
  }
  return

在另一个热键定义中(~lButton::),请使用相同的过程。

编辑:

尝试以下方式:

#NoEnv
#SingleInstance Force

cos_mousedrag_treshold := 20 ; 像素
cos_copied_text := ""

Toggle := 0

F1::           	      ; 按F1启用选择复制和中键粘贴
	Toggle := !Toggle ; 再次按F1以禁用该功能
	CoordMode, ToolTip, Screen
	if (Toggle)
		ToolTip, 选择时复制`n中键粘贴, 0, 0
	else
		ToolTip
return

#If (Toggle and !WinActive("ahk_class ConsoleWindowClass"))

~lButton::
	MouseGetPos, cos_mousedrag_x, cos_mousedrag_y
	Keywait, LButton, T0.3 ; 最多等待0.3秒钟以释放LButton
	If (!ErrorLevel)       ; 如果在上述时间内未按下
		return             ; 什么都不做
	Keywait, LButton       ; 等待释放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
		{
			ClipSaved := ClipboardAll    ; 将整个剪贴板保存到变量ClipSaved中
			clipboard := ""              ; 清空剪贴板(开始时为空以允许ClipWait检测到文本已到达)
			SendInput ^c                 ; 复制选定的文本
			ClipWait, 1                  ; 等待最多1秒钟,以确保剪贴板包含数据。 
			If (!ErrorLevel)             ; 如果没有ErrorLevel,ClipWait找到了剪贴板上的数据
				cos_copied_text := clipboard      ; 将复制的文本存储到变量cos_copied_text中
			else
				MsgBox, 48, Error!, 无法复制选定的文本!
			clipboard := ClipSaved       ; 恢复原始剪贴板
			VarSetCapacity(ClipSaved, 0) ; 释放变量ClipSaved的内存 
		}
	}
	return

~mbutton::
	WinGetClass cos_class, A
	if (cos_class == "Emacs")
		SendInput ^y
	else 
	{
		ClipSaved := ClipboardAll    ; 将整个剪贴板保存到变量ClipSaved中
		clipboard := ""              ; 清空剪贴板(开始时为空以允许ClipWait检测到文本已到达)
		clipboard := cos_copied_text ; 复制存储的文本到剪贴板
		ClipWait, 1                  ; 等待最多1秒钟,以确保剪贴板包含数据。 
		If (!ErrorLevel)             ; 如果没有ErrorLevel,ClipWait找到了剪贴板上的数据
			SendInput ^v             ; 粘贴存储的文本
		else
			MsgBox, 48, Error!, 无法复制存储的文本!
		Sleep, 300                   ; 在粘贴时不要更改剪贴板!
		clipboard := ClipSaved       ; 恢复原始剪贴板
		VarSetCapacity(ClipSaved, 0) ; 释放变量ClipSaved的内存   
	 }
	 return

#If

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

Before copying or sending any data to clipboard per script, is recommended to empty the clipboard and use ClipWait, to make sure that the specified data appears on the clipboard:

~mbutton::
  WinGetClass cos_class, A
  if (cos_class == "Emacs")
	SendInput ^y
  else 
  {
	ClipSaved := ClipboardAll    ; save the entire clipboard to the variable ClipSaved
	clipboard := ""              ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
	clipboard := cos_copied_text ; copy stored text to clipboard
	ClipWait, 1                  ; wait max. 1 sec for the clipboard to contain data. 
	If (!ErrorLevel)             ; If NOT ErrorLevel ClipWait found data on the clipboard
		SendInput ^v			 ; paste the stored text
	else
		MsgBox, 48, Error!, The stored text could not be copied!
	Sleep, 300                   ; Don't change the clipboard while pasting!
	clipboard := ClipSaved       ; restore original clipboard
	VarSetCapacity(ClipSaved, 0) ; Free  the memory of the variable ClipSaved	
  }
  return

Do use the same procedure in the other hotkey definition (~lButton::).

EDIT:

Try it this way:

#NoEnv
#SingleInstance Force

cos_mousedrag_treshold := 20 ; pixels
cos_copied_text := ""

Toggle := 0

F1::           	      ; Press F1 to enable copy on select and paste on middle click
	Toggle := !Toggle ; Press F1 once again to disable that feature
	CoordMode, ToolTip, Screen
	if (Toggle)
		ToolTip, copy on select`npaste on middle click, 0, 0
	else
		ToolTip
return

#If (Toggle and !WinActive("ahk_class ConsoleWindowClass"))  

	~lButton::
	MouseGetPos, cos_mousedrag_x, cos_mousedrag_y
	Keywait, LButton, T0.3 ; waits 0.3 seconds maximally for LButton to be released
	If (!ErrorLevel)       ; If not pressed for above that time
		return             ; do nothing
	Keywait, LButton       ; waits for LButton to be released
	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
		{
			ClipSaved := ClipboardAll    ; save the entire clipboard to the variable ClipSaved
			clipboard := ""              ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
			SendInput ^c                 ; copy the selected text
			ClipWait, 1                  ; wait max. 1 sec for the clipboard to contain data. 
			If (!ErrorLevel)             ; If NOT ErrorLevel ClipWait found data on the clipboard
				cos_copied_text := clipboard      ; store the copied text text to the variable cos_copied_text
			else
				MsgBox, 48, Error!, The selected text could not be copied!
			clipboard := ClipSaved       ; restore original clipboard
			VarSetCapacity(ClipSaved, 0) ; Free  the memory of the variable ClipSaved 
		}
	}
	return

	~mbutton::
	WinGetClass cos_class, A
	if (cos_class == "Emacs")
		SendInput ^y
	else 
	{
		ClipSaved := ClipboardAll    ; save the entire clipboard to the variable ClipSaved
		clipboard := ""              ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
		clipboard := cos_copied_text ; copy stored text to clipboard
		ClipWait, 1                  ; wait max. 1 sec for the clipboard to contain data. 
		If (!ErrorLevel)             ; If NOT ErrorLevel ClipWait found data on the clipboard
			SendInput ^v             ; paste the stored text
		else
			MsgBox, 48, Error!, The stored text could not be copied!
		Sleep, 300                   ; Don't change the clipboard while pasting!
		clipboard := ClipSaved       ; restore original clipboard
		VarSetCapacity(ClipSaved, 0) ; Free  the memory of the variable ClipSaved   
	 }
	  return

#If


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

huangapple
  • 本文由 发表于 2023年7月17日 22:12:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定