Autohotkey window appear event

2020-07-06 04:28发布

问题:

I'm using WorkRave rest reminder and want to turn off my screen when the rest window appears. I know how to turn it off.

How create an event when specified window (#IfWinActive ahk_class ...) appears?

Also, can i bind % symbol? {%} doesn't work, instead of other ones.

回答1:

To have an instant notification of a window appearing, use a Shell Hook. This is sometimes so fast that autohotkey can react before you even see the window yourself.

A shell hook is demonstrated on the AutoHotkey Forum.

An example with your usage (almost copied verbatim from the forum post):

#Persistent
SetBatchLines, -1
Process, Priority,, High

Gui +LastFound
hWnd := WinExist()

DllCall( "RegisterShellHookWindow", UInt,hWnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
Return

ShellMessage( wParam,lParam )
{
    If ( wParam = 1 ) ;  HSHELL_WINDOWCREATED := 1
    {
        WinGetTitle, Title, ahk_id %lParam%
        If  ( Title = "WorkRest" )
            WinClose, ahk_id %lParam% ; close it immideately
    }
}

If you want to use a literal % symbol in a command, escape it with AutoHotkey's escape character, the backtick ` (on the same key as ~ on a US keyboard) like so:

MsgBox You are 200`% awesome!


回答2:

Romale,

You can try this, but since I don't use WorkRave, I can't test it.

; This next line needs to be added at the top of the AHK file, so it will be started as soon as AHK starts.
; Every 120000 ms, it will launch the "WorkRave:" script to check if a window with WorkRave exists.
SetTimer, WorkRave,120000 ; Run WorkRaveTester every 2 minutes = 120000


; Somewhere else in the AHK file.....
WorkRave: ; This is the label for the WorkRave script
SetTitleMatchMode, 2 ; 2 = Matches the string WorkRave anywhere in the window title of IfWinExist
IfWinExist, WorkRave ; When WorkRave window exists
{
  TrayTip, WorkRave, Started ,1 ; Or whatever you want to do here....
}
Return


标签: autohotkey