【问题标题】:SendEvent ^{ins} isn't copying content to the clipboardSendEvent ^{ins} 没有将内容复制到剪贴板
【发布时间】:2016-08-26 19:17:17
【问题描述】:
!c::
    file_name = footnote.ini                             
    restore_original_clipBoard := clipboard
    clipboard =
    KeyWait, Alt                
    KeyWait, c                                           ;small c
    BlockInput, on
    SendEvent, ^{ins}                                   ;^c doesn't work
    ClipWait, 2                                     ; Wait for the clipboard to contain text.
    if ErrorLevel
    {
        MsgBox Failed to save the selection: %clipboard%
        exit
    }
    BlockInput, off
    save_selection := clipboard 

问题: 尽管进行了选择,Sendevent ^{ins} 不会将其保存到剪贴板。有时我必须重复我的热键,alt + c 几次,然后才能将选择复制到剪贴板。 KeyWait 应该确保我只处理 ^{ins} 而没有任何额外的密钥。我在这里做错了什么?


更新 我试图强制将选择复制到剪贴板的方法之一是使用 while 循环。我让它通过帖子工作:Looping clipboard and errorlevel evaluation not working as expected

问题 当我进行选择并按 alt + c 时,有时会卡在我实现的无限循环中。但正如您从该代码中看到的那样:

clipboard := ""
while( StrLen(clipboard) < 1 )
{
    Send, ^{ins}
    Sleep, 50
}
MsgBox % ClipBoard

无限循环在其自身中包含一个继续重新发送 ^{ins}。出于某种原因,我的选择未被识别为选择。虽然它处于无限循环中,但我尝试重新选择文本。然后它会立即识别它并将我的选择复制到剪贴板。可惜!选择不完整,因为它走得太快了。 这个问题并不总是这样。有时它会识别选择的第一个位置!所以有时它会将我的选择复制到我的剪贴板,有时不会。如果没有,则重新发送 ^{ins} 似乎不起作用。我不想让用户重新选择他的选择。有可能吗?

【问题讨论】:

  • Khalil,您知道您可以而且应该接受或讨论您的问题的答案吗?在过去的几天里,您提出了几个问题,但没有接受任何答案。

标签: autohotkey


【解决方案1】:
Send {Ctrl Down}{c}{Ctrl Up}

按 Ctrl+C,您必须立即执行此命令,而不是按 Ctrl 等待然后按 C。

从未见过用于复制文本的插入键。

还发现这也会发送 Ctrl+C。

Send, ^c

发送插入键使用

{Insert}

【讨论】:

  • 它不起作用。有整个线程专门用于解决 ^c 不适用于人数的问题。您的解决方法根本没有任何作用。此外,{ins} 是 {insert} 的正确缩写。
【解决方案2】:

这种方式适合我:

!vk43:: ; alt+c
   clipContent:=ClipboardAll
   Clipboard:=""
   SendEvent, ^{Ins}
   ClipWait, .75
   MsgBox, % 262 . (ErrorLevel ? 160:208)
         , % ErrorLevel ? "Period expired:":"Result:"
         , % ErrorLevel ? "Failed to save the selection.":Clipboard
         , % (ErrorLevel ? 0:2) . .5
   Clipboard:=clipContent
   KeyWait, vk43
   Return

【讨论】:

  • 我正在尝试弄清楚这是在做什么,但没有提供文档。你能为我澄清一下 MsgBox 部分吗?为什么要在 262 前面加上一个 %,然后是一个点? 160:208 是什么意思? 0:2 是什么意思?然后另一个。然后是 0.5 秒?
  • 另外,如果它失败了,我希望它重试复制直到它工作。
  • Khalil : 你能帮我解释一下 MsgBox 部分吗?我在下面的代码中给出了解释。 Khalil我希望它重试复制,直到成功为止。见下面的代码。
  • Khalil另外,你能解释一下为什么 !vk43:: 比 !c:: 效果更好吗? 因为键盘布局对这些热键。我mean 随输入语言变化的键(如果系统中有多种语言)。
  • 感谢提供虚拟扫码优势的链接!我发现即使我更改了键盘布局,我现在也可以使用热键了。
【解决方案3】:
!vk43:: ; alt+c
   clipContent:=ClipboardAll ; backup clipboard content (if needed)
   Clipboard:="" ; no comment :)
   Loop
   {
      SendEvent, ^{Ins}
      ClipWait, .75 ; means 750ms, same if write 0.75
      ; assign value of "ErrorLevel" an variable for further usage
      errLvl:=ErrorLevel
      ; monitoring current action (for debugging purpose only)
      TrayTip, % "attempt: #"A_Index
             , % """ErrorLevel"" of ""ClipWait"" command is: "errLvl
   }
   ; here you can set the condition of ending the cycle: either...
   ; ...variable errLvl has not a true value,...
   ; ...or the number of attempts is equal 5
   Until, Not errLvl Or A_Index=5
   ; value of each field of the command "MsgBox"...
   ; ...are set depending on the value of errLvl variable...
   ; ...using a ternary expression
   ; means if errLvl is a true, "options" field is 262160
   MsgBox, % 262 . (errLvl ? 160:208)
         ; means that "title" has a couple variants
         , % errLvl ? "Period expired:":"Result:"
         ; means that "text" has a couple variants
         , % errLvl ? "Failed to save the selection.":Clipboard
         ; means if errLvl is a true, "timeout" field is 0.5 (500ms)
         , % (errLvl ? 0:2) . .5
/* same that and above:
   IfEqual, errLvl, % True, MsgBox, 262160
                                  , % "Period expired:"
                                  , % "Failed to save the selection."
                                  , 0.5
   Else MsgBox, 262208, % "Result:", % Clipboard, 2.5
*/
   TrayTip ; remove "TrayTip" (for debugging purpose only)
   ; save an positive result (if needed)
   IfEqual, errLvl, 0, Sleep, -1, someVar:=Clipboard
   ; return a temporarily saved data into clipboard (if needed)
   Clipboard:=clipContent
   KeyWait, % "vk43"
   Return

【讨论】:

    【解决方案4】:

    根据我的经验,每当无法可靠识别击键时,都是由于系统或目标程序跟不上这些键的发送速度。

    对于 SendEvent,您可以尝试SetKeyDelay, 1000, 1000 之类的方法,看看这是否会有所改善。另一种选择是在this answer 中概述的间歇性睡眠呼叫中发送显式向下和向上键。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 2011-04-26
      • 2020-10-10
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多