In Unity, I want to get mouse input in the background (just click) when the window is inactive.

It works normally if it is active, but not if it is inactive.
The build did not change.

Applicable source code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClickMouse: MonoBehaviour
    public void OnGUI ()
        Event e = Event.current;
        if (e.button == 0&&e.isMouse&&e.type == EventType.MouseDown)
            this.gameObject.GetComponent<SpriteRenderer>(). color = new Color (1, 1, 1, 0);

There is an article about using Event to receive input, but I tried it, but it does not work.

You can get keyboard input using "UnityRawInput" which is also introduced.

There is almost no other information and I'm stuck.

Supplemental information (FW/tool version etc.)


  • Answer # 1

    It was solved by arriving at the word "Global hook".
    By the way, it seems that global hooks in C # can only be low level hooks, but mouse and keyboard information can be obtained within that range.
    If you have knowledge of other languages, it seems easy to implement in C ++.

    Reference site

    Words that helped resolve
    SetWindowsHookEx function, WH_MOUSE_LL