SendEvent ^ {ins}未将内容复制到剪贴板

时间:2013-08-11 19:35:26

标签: autohotkey

!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}似乎不起作用。我不想让用户重新选择他的选择。这可能吗?

4 个答案:

答案 0 :(得分:1)

Send {Ctrl Down}{c}{Ctrl Up}

按Ctrl + C,您必须立即执行此操作,同时按下Ctrl等待按C键。

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

还发现这也发送了Ctrl + C.

Send, ^c

要发送插入密钥,请使用

{Insert}

答案 1 :(得分:1)

这种方式对我有用:

!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

答案 2 :(得分:1)

!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

答案 3 :(得分:1)

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

对于SendEvent,您可以尝试contentOfThePage之类的内容,看看这是否可以改进。另一种选择是发送明确的向下和向上键以及this answer中概述的间歇性睡眠呼叫。