Capture keypress in Unity when Unity does not have input focus

I need Unity to capture ALL keystrokes, even if Unity has no focus.

I tried to use:

Input.KeyPress()

But this seems to work only if Unity has user input focus. I need it to work when it has no focus, for example, when I watch / work with another Windows program.

PS: I already turned on the option "Run in the background" in the player settings.

+4
source share
2 answers

That's quite possible! Although, there is no way to do this using only the tools built into Unity3D. For this you will have to use your own libraries.

WH_KEYBOARD, . SetWindowsHookEx [] [1].

, (WH_KEYBOARD) [] [2]

using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;

public class KBHooks : MonoBehaviour
{
    [DllImport("user32")]
    protected static extern IntPtr SetWindowsHookEx(
        HookType code, HookProc func, IntPtr hInstance, int threadID);

    [DllImport("user32")]
    protected static extern int UnhookWindowsHookEx(
        IntPtr hhook);

    [DllImport("user32")]
    protected static extern int CallNextHookEx(
        IntPtr hhook, int code, IntPtr wParam, IntPtr lParam);

    // Hook types. To hook the keyboard we only need WH_KEYBOARD
    protected enum HookType : int
    {
        WH_JOURNALRECORD = 0,
        WH_JOURNALPLAYBACK = 1,
        WH_KEYBOARD = 2,
        WH_GETMESSAGE = 3,
        WH_CALLWNDPROC = 4,
        WH_CBT = 5,
        WH_SYSMSGFILTER = 6,
        WH_MOUSE = 7,
        WH_HARDWARE = 8,
        WH_DEBUG = 9,
        WH_SHELL = 10,
        WH_FOREGROUNDIDLE = 11,
        WH_CALLWNDPROCRET = 12,
        WH_KEYBOARD_LL = 13,
        WH_MOUSE_LL = 14
    }

    protected IntPtr m_hhook = IntPtr.Zero;
    protected HookType m_hookType = HookType.WH_KEYBOARD;

    protected delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);

    //We install the hook and hold on to the hook handle.
    //The handle will be need to unhook. 
    protected bool Install(HookProc cbFunc)
    {
        if (m_hhook == IntPtr.Zero)
            m_hhook = SetWindowsHookEx(
                m_hookType, 
                cbFunc, 
                IntPtr.Zero, 
                (int)AppDomain.GetCurrentThreadId());

        if (m_hhook == IntPtr.Zero)
            return false;

        return true;
    }

    protected void Uninstall()
    {
        if (m_hhook != IntPtr.Zero)
        {
            UnhookWindowsHookEx(m_hhook);
            m_hhook = IntPtr.Zero;
        }
    }

    protected int CoreHookProc(int code, IntPtr wParam, IntPtr lParam)
    {
        if (code < 0)
            return CallNextHookEx(m_hhook, code, wParam, lParam);

        Debug.Log(
            "hook code =" + code.ToString() + 
            " lparam=" + lParam.ToString() + 
            " wparam=" + wParam.ToString());

        // Yield to the next hook in the chain
        return CallNextHookEx(m_hhook, code, wParam, lParam);
    }

    // Use this for initialization
    void Start()
    {
        Debug.Log("install hook");
        Install(CoreHookProc);
    }

    void OnDisable()
    {
        Debug.Log("Uninstall hook");
        Uninstall();
    }

}

[ ] [3].

Windows. OS X Linux, - .

1 , SO. , .

 [1]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms644990(v=vs.85).aspx
 [2]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms644984(v=vs.85).aspx
 [3]: http://phardera.blogspot.com.es/2010/12/windows-hooks-in-unity3d.html
+4

Unity @boris-makogonyuk . GitHub ( MIT): https://github.com/Elringus/UnityRawInput

:

.

using UnityRawInput;

, .

RawKeyInput.Start();

, , , ( ).

var workInBackground = true;
RawKeyInput.Start(workInBackground);

.

RawKeyInput.OnKeyUp += HandleKeyUp;
RawKeyInput.OnKeyDown += HandleKeyDown;

private void HandleKeyUp (RawKey key) { ... }
private void HandleKeyDown (RawKey key) { ... }

, .

if (RawKeyInput.IsKeyDown(key)) { ... }

.

RawKeyInput.Stop();

, .

private void OnDisable ()
{
    RawKeyInput.OnKeyUp -= HandleKeyUp;
    RawKeyInput.OnKeyDown -= HandleKeyDown;
}
0

All Articles