当Unity没有输入焦点时,在Unity中捕获按键

时间:2016-03-31 17:07:16

标签: unity3d focus keypress

我需要Unity来捕获所有按键,即使Unity没有焦点。

我试过用:

Input.KeyPress()

但是,如果Unity有用户输入的焦点,这似乎才有效。当它没有焦点时,我需要它才能工作,例如当我正在查看/使用其他Windows程序时。

PS:我已经在玩家偏好设置中打开了“在后台运行”选项。

2 个答案:

答案 0 :(得分:4)

这完全有可能!虽然,只使用Unity3D中内置的工具是无法做到的。您将不得不使用本机库来执行此操作。

以下示例使用钩子类型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上缺乏声誉。我希望其中一个mod会相应地编辑我的帖子。

 [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

答案 1 :(得分:1)

我根据@ boris-makogonyuk答案组装了一个Unity软件包,并提供了一些可用性改进。 该软件包可在GitHub(MIT许可证)上获得:https://github.com/Elringus/UnityRawInput

您可以按如下方式使用它:

包含包名称空间。

p->f()

初始化输入服务以开始处理本机输入消息。

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();