---
tags:
    - "Gain"
    - "Focus"
    - "Suspend"
    - "Minimize"
---

# On Application Focus

[Application](index.md) / **On Application Focus**

## Description

Executes when application gains or loses the focus

## Code

```cs
using System;
using UnityEngine;
using VisualScript.Icons;

namespace VisualScript.Runtime
{
    [Title("On Application Focus")]
    [Image(typeof(IconApplication), ColorTheme.Type.TextLight)]
    
    [Description("Executes when application gains or loses the focus")]
    [Category("Application/On Application Focus")]
    
    [Keywords("Gain", "Focus", "Suspend", "Minimize")]

    [Serializable]
    public class TriggerCoreOnApplicationFocus : Trigger
    {
        [SerializeField]
        private TValueOrAny<GetBool> m_HasFocus = new TValueOrAny<GetBool>();

        protected override string GetTitle(GameObject source) => $"Application Focus = {this.m_HasFocus}";

        public override void OnEnable()
        {
            base.OnEnable();
            Application.focusChanged += this.OnChangeFocus;
        }

        public override void OnDisable()
        {
            base.OnDisable();
            Application.focusChanged -= this.OnChangeFocus;
        }

        private void OnChangeFocus(bool hasFocus)
        {
            using Args args = Args.Get(this.Component, this.Component);
            if (this.m_HasFocus.IsAny || this.m_HasFocus.Value.Get(args) == hasFocus) this.Run(args);
        }
    }
}
```

