【问题标题】:AutoHotKey strange issue with Copy (Ctrl-C) every other execution每隔一次执行复制(Ctrl-C)的AutoHotKey奇怪问题
【发布时间】:2012-04-24 21:59:14
【问题描述】:

我是编写自己的 AutoHotKey 脚本的新手,所以这只是我在这里想念的一些愚蠢的东西。

脚本的目的是让用户选择一些文本并按下热键 (Win-W)。菜单弹出,他们单击菜单项。然后应将所选文本复制到剪贴板。这就是我现在想做的。

问题是它第一次工作,然后失败,然后工作,然后失败,等等。它基本上只是每隔一次工作。

我正在使用最新的AutoHotKey_l(unicode 32bit)运行 Win7 x64。

我在ClipWait 上有一个超时,它基本上只是等待,从不接收复制的文本,并发出 ErrorLevel 1。

代码如下:

#SingleInstance force
; EXAMPLE #2: This is a working script that creates a popup menu that is displayed when the user presses the Win-w hotkey.

; Create the popup menu by adding some items to it.
Menu, MyMenu, Add, Demo, Demo

return  ; End of script's auto-execute section.

Demo:
clipboard =  ; Start off empty to allow ClipWait to detect when the text has arrived.
Send ^c
ClipWait, 2  ; Wait for the clipboard to contain text.
if ErrorLevel = 1
{
    MsgBox Copy failed
}
else
{
    MsgBox Copy worked
}
return

#w::Menu, MyMenu, Show  ; i.e. press the Win-w hotkey to show the menu.

任何帮助将不胜感激。

【问题讨论】:

  • 我无法重现您所看到的内容。在 Win7 x64 上完全按原样使用您的脚本,我每次都看到“复制有效”。 AHK 版本 1.0.48.05。
  • 你在不同的程序中试过这个吗?我想知道您是否在某个地方使用它时失去了对文本区域或其他东西的关注。
  • 好吧,我知道这很愚蠢。 :) 你是对的。我一直在 Notepad++ 中进行测试,这就是问题所在。也许我有一个插件或导致问题的东西——因为我在其他一些文本编辑器(UltraEdit、写字板等)中尝试过它——它在所有这些编辑器中都完美无缺。感谢您的帮助!
  • 太棒了!很高兴听到您正在取得进展。

标签: autohotkey


【解决方案1】:

当您的脚本在其他程序中表现不定期和/或不同时,
首先要尝试的是模拟按键持续时间和/或按键之间的延迟时间。
这是因为有些程序不是为处理 AutoHotkey 发送的速度而设计的
人工击键。

这是最基本的例子:

f1::
Send, {ctrl down}
Sleep, 40
Send, {c down}
Sleep, 40
Send, {c up}
Sleep, 40
Send, {ctrl up}
Return

我们有几种方法可以使它更简洁。
最简单的(但并不总是可取的,因为它在延迟期间阻塞,不像睡眠)
SetKeyDelay 命令,仅适用于SendEvent 和SendPlay 模式。

f2::
SetKeyDelay, 40 ; could be set at the top of the script instead.
Send, {ctrl down}{c down}{c up}{ctrl up}
Return 

使用 AHK_L 的用户可以使用 for 循环和数组:

f3::
For i, element in array := ["{ctrl down}","{c down}","{c up}","{ctrl up}"] {
   Sendinput, %element%
   Sleep, 40
} Return

而那些使用AHK basic(或AHK_L)的可以使用Loop, Parse

f4::
list := "{ctrl down},{c down},{c up},{ctrl up}"
Loop Parse, list, `,
{
    Sendinput, %A_LoopField%
    Sleep, 40
} Return 

了解这三个Sendmodes 很有用。
更多信息可以在Send command's page底部找到。

【讨论】:

    猜你喜欢
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多