2016-03-31 8 views
2

Ich brauche Unity, um ALLE Tastendrücke zu erfassen, auch wenn Unity nicht den Fokus hat.Capture-Tastendruck in Unity, wenn Unity nicht den Eingabefokus hat

habe ich versucht, zu verwenden:

arbeiten
Input.KeyPress() 

Aber das scheint nur, wenn die Einheit den Fokus der die Eingabe des Benutzers hat. Ich brauche es, wenn es nicht den Fokus hat, wie wenn ich mit einem anderen Windows-Programm arbeite.

PS: Ich habe bereits die Option "Im Hintergrund laufen" in Player-Einstellungen eingeschaltet.

+0

gute frage .... – Fattie

Antwort

2

Das ist absolut möglich! Es gibt jedoch keine Möglichkeit, dies zu tun, indem nur die in Unity3D integrierten Tools verwendet werden. Sie müssen dazu native Bibliotheken verwenden.

Im folgenden Beispiel wird die Hakenkette mit einem Hakentyp WH_KEYBOARD verknüpft, der einem Tastaturhook auf Nachrichtenebene entspricht. Sie können mehr über SetWindowsHookEx und verschiedene Arten [hier] [1] lesen.

Sie können die Parameter überprüfen, die auf Einhaken eines solchen Nachrichtentyp (WH_KEYBOARD) [hier] [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(); 
    } 

} 

Dieses Beispiel stammt aus [diesem Blog] [3] empfangen werden.

Eine solche Art des Hooks wird nur arbeiten auf Windows-Systemen. Wenn Sie unter OS X oder Linux einen separaten Hook erstellen müssen, müssen Sie dies auf native Weise in diesem Betriebssystem tun.

Ich kann nicht mehr als 1 Link veröffentlichen, weil mir der Ruf auf SO fehlt. Ich hoffe, einer der Mods wird meinen Post entsprechend bearbeiten.

[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