窗口变为活动状态时,是否显示纸托提示? [英] traytip when window becomes active?

查看:77
本文介绍了窗口变为活动状态时,是否显示纸托提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在窗口变为活动状态时显示工具提示.

I want to simply display a tooltip when a window becomes active.

为什么这行不通?脚本加载后立即启动工具提示.

Why doesn't this work? It launches the tooltip as soon as the script is loaded.

#IfWinActive, Untitled - Notepad
{
TrayTip, Notepad Has Focus, test
Tab::
    MsgBox Window Found
    return
}

选项卡检测按预期方式工作,只有在该窗口处于活动状态时,它才会显示消息框.

Tab detection works as expected, it shows the Message Box only if the window is active.

推荐答案

按照#如果是文档,则#IfWinActive 创建上下文相关的热键和热字符串.更加精确地说,这是使用#IfWin...时发生的情况:
每当您按下热键或键入热字符串时,AHK都会查找相应的#IfWin...定义(如果可用)并进行评估(例如记事本是否处于活动状态?" ).如果为true,则将执行hotkey/hotstring标签,否则将发送本机密钥.
查看此过程,您将认识到在#IfWin...语句下执行任意代码是行不通的.当指定的窗口变为活动状态/存在时,AHK不会触发事件,而是在触发相应的热键/热字符串时检查条件.
如此,您将必须编写等待记事本,显示通知并可能重复此过程的代码:

As per the #If... docs, #IfWinActive creates context-sensitive hotkeys and hotstrings. To be a bit more precise, this is what happens when you use #IfWin...:
Whenever you press a hotkey or type a hotstring, AHK looks up the corresponding #IfWin... definition (if available) and evaluates it (e.g. "Is notepad active?"). If it is true, the hotkey/hotstring label will be executed, otherwise the native key will be sent.
Looking at this procedure, you will recognize that executing arbitrary code below a #IfWin... statement won't work; AHK doesn't fire an event when a specified window becomes active/existent etc, it rather checks the conditions when a corresponding hotkey/hotstring is fired.
Ergo, you will have to write code that waits for notepad, shows a notification and possibly repeats this procedure:

#Persistent

SetTimer, WaitForNotepad, -1

Exit

WaitForNotepad:
    WinWaitActive, ahk_class Notepad
    TrayTip, Warning, Notepad is active!
    WinWaitNotActive
    SetTimer, WaitForNotepad, -1
return

请注意,如果没有SetTimer循环,这也将起作用.但是,每当您等待大量时间时,使用计时器是合理的,因为它们实际上允许其他线程在两者之间运行.
您还注意到,我使用了 window class (ahk_class)而不是窗口标题,因为它通常更可靠.

Please note that this would also work without SetTimer in some kind of loop. But whenever you're waiting a potentially large amount of time, it is reasonable to use timers, since they virtually allow other threads to run in between.
You also noticed that I used the window class (ahk_class) instead of the window title, since it's usually more reliable.

这篇关于窗口变为活动状态时,是否显示纸托提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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