Search code examples
autohotkey

Autohotkey window appear event


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.


Solution

  • 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!