【问题标题】:AutoHotKey script paste text with new line without pressing EnterAutoHotKey 脚本用新行粘贴文本而不按 Enter
【发布时间】:2021-08-18 13:13:39
【问题描述】:

我需要做一件非常简单的事情:创建一个快捷方式,在里面粘贴带有新行的文本,像这样

first line
second line

我知道我可以使用 send、sendinput 或 sendinput {text} 来做到这一点

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
#SingleInstance force
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.

^!q::
  Send first line `nsecond line`n
return

^!w::
  SendInput first line `nsecond line`n
return

^!e::
  SendInput {text}first line `nsecond line`n
return

我看到它可能有风险,因为在所有这些方面,新行都可以充当 Enter 命令,我想避免这种行为。

所以我尝试使用剪贴板:

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
#SingleInstance force
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.

^!r::
  string = first line `nsecond line`n
  prevClipboard := ClipboardAll
  Clipboard := string
  ClipWait, 1, 1 ; Wait for the clipboard to contain the copied text.
  Send ^v
  Clipboard := prevClipboard ; keep previous cliboard content
return

在我的个人计算机上它可以工作,但在另一种情况下它不起作用,我在一个非常慢的虚拟机中使用便携式 AHK,第一次它工作,第二次只粘贴实际剪贴板内容

有没有办法做到这一点?

我进行了测试以验证我的快捷方式是否按下了回车键: 在文件资源管理器中选择一个文件,按 Shift + Canc,这将显示一个弹出窗口“您确定要永久删除吗?” 然后按下我的 ahk 快捷方式,它就像一个 Enter 键并删除了文件

【问题讨论】:

标签: autohotkey


【解决方案1】:

您可能将旧剪贴板重新设置得太快。
要制作这样一个可靠的脚本,需要在设置旧剪贴板之前稍等片刻。您向其发送粘贴操作的应用也可以选择以自己的方式处理剪贴板,此时此问题变得尤为明显。

另外,您的第一个 ClipWait 没有做任何事情。在使用ClipWait 之前你必须清除剪贴板,否则它没有用,因为剪贴板已经包含了一些东西。另外,我不能 100% 确定这里需要 ClipWait,但至少不会受到伤害。

这是一个可能已修复的脚本:

^!r::
  string := "first line `nsecond line`n"
  
  /*
  ;continuation sections are cool
  string := "
  (LTrim
  first line
  second line
  
  )"
  */
  
  prevClipboard := ClipboardAll
  Clipboard := "" ;empty the clipboard first
  Clipboard := string
  ClipWait, 1, 1 ; Wait for the clipboard to contain the copied text.
  SendInput, ^v
  Sleep, 500 ;500ms is hopefully more than enough
  Clipboard := prevClipboard ; keep previous cliboard content
return

(Documentation for continuation)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    相关资源
    最近更新 更多