1. 程式人生 > >unity 獲取當前鍵盤按鍵

unity 獲取當前鍵盤按鍵

using UnityEngine;
using System.Collections;

public delegate void OnTapKey(KeyCode kc);

public class InputKeyEvent : MonoBehaviour {

	public OnTapKey onTapKey;

	// Use this for initialization
	void Start () {
		onTapKey += OnGetKeyDown;
	}
	void OnGUI()
	{
		if (Input.anyKeyDown)
		{
			Event e = Event.current;
			if (e != null && e.isKey && e.keyCode != KeyCode.None)
			{
//				KeyCode currentKey = e.keyCode;
//				Debug.Log("Current Key is : " + currentKey.ToString());
				if(onTapKey != null)
					onTapKey(e.keyCode);
			}
		}
	}

	void OnGetKeyDown(KeyCode kc)
	{
		Debug.Log("Current Key is : " + kc);
	}
}

Event 目前支援在OnGUI中實現。

相關推薦

no