AutoHotkey 在赋值给变量之前显示 MsgBox

AutoHotkey displays MsgBox before assignment to variable

这是我的第一个 AHK 脚本,但我无法让它执行我想要的操作。在记事本中使用组合键Ctrl+Alt+A,想跳转到一行文本的开头,select第一个字符,复制到剪贴板,查看是什么字母。现在,我只是尝试使用 MsgBox 检查剪贴板的内容,但是有一个问题:MsgBox 在 复制命令之前显示它的内容 !这是脚本:

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

#IfWinActive, ahk_class Notepad

^!a::
    Send, {Home}
    Send, {LShift Down}{Right}{LShift Up}
    Send, ^c
    MsgBox, %clipboard%
return
多次

运行 命令,您会看到它复制 selected 文本 显示 MsgBox 之后。它显示来自上一个命令的文本。关于我可能做错了什么的任何想法?

更新: 似乎我忽略包含在问题中的一小段脚本可能是罪魁祸首:SendMode Input ; Recommended for new scripts due to its superior speed and reliability. 我已经更新了脚本以上也。当我删除该行时,它会按预期工作。谁能告诉我为什么?

仅仅是因为您没有在代码末尾添加 return, 行吗?

^!a::
    Send, {Home}
    Send, {LShift Down}{Right}{LShift Up}
    Send, ^c
    MsgBox, %clipboard%
return,

或者你可以添加一个延迟,这个命令是 sleep,1000 = 1 秒。

如果失败,试试这个:

^!a::
    Send, {Home}
    Send, {LShift Down}{Right}{LShift Up}
    Send, ^c
    sleep, 200
    MsgBox, %clipboard%
return,

删除 SendMode Input ; Recommended for new scripts due to its superior speed and reliability. 解决了问题。