# Function Reference

# TrayItemSetOnEvent

Defines a user-defined function to be called when a tray item is clicked.

TrayItemSetOnEvent ( itemID, "function" )

## Parameters

itemID | The item identifier (itemID) as returned by a TrayCreateItem function.
function | The name of the user function to call.

## Return Value

Success: | 1.
Failure: | 0.
@error: | 1 = the "function" is not defined.

## Remarks

OnEvent functions are only called when the option Opt("TrayOnEventMode", 1) - when in this mode TrayGetMsg() is NOT used at all.
Within the called user function the item identifier can be retrieved with @TRAY_ID.
If the function is an empty string "" the previous user-defined is disabled.

## Related

TrayCreateItem, TrayGetMsg, TrayOnEventMode (Option), TraySetOnEvent

## Example

    #NoTrayIcon

    #include <MsgBoxConstants.au3>
    #include <StringConstants.au3>
    #include <TrayConstants.au3>

    Opt("TrayMenuMode", 3) ; The default tray menu items will not be shown and items are not checked when selected. These are options 1 and 2 for TrayMenuMode.
    Opt("TrayOnEventMode", 1) ; Enable TrayOnEventMode.

    Example()

    Func Example()
        TrayCreateItem("About")
        TrayItemSetOnEvent(-1, "About")

        TrayCreateItem("") ; Create a separator line.

        TrayCreateItem("Exit")
        TrayItemSetOnEvent(-1, "ExitScript")

        TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, "About") ; Display the About MsgBox when the tray icon is double clicked on with the primary mouse button.

        TraySetState($TRAY_ICONSTATE_SHOW) ; Show the tray menu.

        While 1
            Sleep(100) ; An idle loop.
        WEnd
    EndFunc   ;==>Example

    Func About()
        ; Display a message box about the AutoIt version and installation path of the AutoIt executable.
        MsgBox($MB_SYSTEMMODAL, "", "AutoIt tray menu example." & @CRLF & @CRLF & _
                "Version: " & @AutoItVersion & @CRLF & _
                "Install Path: " & StringLeft(@AutoItExe, StringInStr(@AutoItExe, "\", $STR_NOCASESENSEBASIC, -1) - 1)) ; Find the folder of a full path.
    EndFunc   ;==>About

    Func ExitScript()
        Exit
    EndFunc   ;==>ExitScript