每隔一次执行复制(Ctrl-C)的 AutoHotKey 奇怪问题 [英] AutoHotKey strange issue with Copy (Ctrl-C) every other execution

查看:47
本文介绍了每隔一次执行复制(Ctrl-C)的 AutoHotKey 奇怪问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始编写自己的 AutoHotKey 脚本,所以这一定是我在这里遗漏的一些愚蠢的东西.

I'm new to writing my own AutoHotKey scripts so this just has to be something stupid I'm missing here.

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

The intent of the script is for the user to select some text and press the hot-key (Win-W). The menu pops-up and they click the menu item. The selected text should then be copied to the clipboard. That's all i'm trying to do right now.

问题是它第一次工作,然后失败,然后工作,然后失败,等等.它基本上每隔一次才有效.

The problem is that it works the first time, then fails, then works, then fails, etc. It basically only works every other time.

我使用最新的 AutoHotKey_l(unicode 32 位)运行 Win7 x64.

I'm running Win7 x64 with the latest AutoHotKey_l (unicode 32bit).

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

I have a timeout on the ClipWait, and it basically just waits, never receives the copied text, and issues an 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.

任何帮助将不胜感激.

推荐答案

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

When you have a script that behaves sporadically, and/or differently in other programs,
the first thing to try is simulating a key-press duration and/or delay period between keys.
This is because some programs aren't designed to handle the speed that AutoHotkey sends
artificial keystrokes.

这是最基本的例子:

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

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

We have several ways to make it more concise.
The easiest (yet not always desirable since it blocks during delays, unlike sleep)
is the SetKeyDelay command, which only works for the SendEvent and SendPlay modes.

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 循环和数组:

Those using AHK_L can make use of a for-loop and an array:

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:

And those using AHK basic (or AHK_L) can use Loop, Parse:

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

了解三个 Sendmodes 很有用.
更多信息可以在发送命令页面的底部找到.

It's useful to read about the three Sendmodes.
More information can be found at the bottom of the Send command's page.

这篇关于每隔一次执行复制(Ctrl-C)的 AutoHotKey 奇怪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆