KeyboardEventManager

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (keys public -> private)
(Cleanup formatting.)
 
Line 1: Line 1:
[[Category:C#]]
+
[[Category:Code Examples]]
 
[[Category:Input]]
 
[[Category:Input]]
 
Author: Berenger
 
  
 
== Description ==
 
== Description ==
Line 8: Line 6:
 
An event based keyboard input mager. You can associate one or several functions with a KeyCode. That way, you can detect several keys at a time.
 
An event based keyboard input mager. You can associate one or several functions with a KeyCode. That way, you can detect several keys at a time.
  
== Usage ==
+
== KeyboardEventManager.cs ==
 
+
KeyboardEventManager inherit from MonoBehaviour so you need to put it on a game object. It uses a singleton pattern so you can access it's instance from anywhere.
+
To add an event, use the functions RegisterKeyDown or RegisterKeyUp. To remove an event, use the functions UnregisterKeyDown or UnregisterKeyUp. To remove a key completely, use RemoveKey.
+
Remark : Only the keys with a least one event on up or on down are checked with Input.GetKey...
+
 
+
== Example ==
+
 
+
<syntaxhighlight lang="csharp">public class KeyEventMgrTest : MonoBehaviour
+
{
+
public bool ctrl, shift, a;
+
+
void Start ()
+
{
+
KeyboardEventManager.instance.RegisterKeyDown( KeyCode.LeftControl, new KeyEvent( ComboDown ) );
+
KeyboardEventManager.instance.RegisterKeyDown( KeyCode.LeftShift, new KeyEvent( ComboDown ) );
+
KeyboardEventManager.instance.RegisterKeyDown( KeyCode.A, new KeyEvent( ComboDown ) );
+
+
KeyboardEventManager.instance.RegisterKeyUp( KeyCode.LeftControl, new KeyEvent( ComboUp ) );
+
KeyboardEventManager.instance.RegisterKeyUp( KeyCode.LeftShift, new KeyEvent( ComboUp ) );
+
KeyboardEventManager.instance.RegisterKeyUp( KeyCode.A, new KeyEvent( ComboUp ) );
+
}
+
+
void ComboDown( KeyCode K)
+
{
+
print( "Key : " + K );
+
if( K == KeyCode.LeftControl ) ctrl = true;
+
if( K == KeyCode.LeftShift ) shift = true;
+
if( K == KeyCode.A ) a = true;
+
+
if( ctrl && shift && a ) MEGACOMBO();
+
}
+
void ComboUp( KeyCode K)
+
{
+
print( "Key : " + K );
+
if( K == KeyCode.LeftControl ) ctrl = false;
+
if( K == KeyCode.LeftShift ) shift = false;
+
if( K == KeyCode.A ) a = false;
+
}
+
+
void MEGACOMBO()
+
{
+
print( "MEGACOMBO" );
+
}
+
}
+
</syntaxhighlight>
+
 
+
 
+
== C# - KeyboardEventManager .cs ==
+
  
 
<syntaxhighlight lang="csharp">using UnityEngine;
 
<syntaxhighlight lang="csharp">using UnityEngine;
Line 179: Line 129:
 
}
 
}
 
#endregion
 
#endregion
 +
}
 +
</syntaxhighlight>
 +
 +
== Usage ==
 +
 +
KeyboardEventManager inherit from MonoBehaviour so you need to put it on a game object. It uses a singleton pattern so you can access it's instance from anywhere.
 +
To add an event, use the functions RegisterKeyDown or RegisterKeyUp. To remove an event, use the functions UnregisterKeyDown or UnregisterKeyUp. To remove a key completely, use RemoveKey.
 +
Remark : Only the keys with a least one event on up or on down are checked with Input.GetKey...
 +
 +
== Example ==
 +
 +
<syntaxhighlight lang="csharp">public class KeyEventMgrTest : MonoBehaviour
 +
{
 +
public bool ctrl, shift, a;
 +
 +
void Start ()
 +
{
 +
KeyboardEventManager.instance.RegisterKeyDown( KeyCode.LeftControl, new KeyEvent( ComboDown ) );
 +
KeyboardEventManager.instance.RegisterKeyDown( KeyCode.LeftShift, new KeyEvent( ComboDown ) );
 +
KeyboardEventManager.instance.RegisterKeyDown( KeyCode.A, new KeyEvent( ComboDown ) );
 +
 +
KeyboardEventManager.instance.RegisterKeyUp( KeyCode.LeftControl, new KeyEvent( ComboUp ) );
 +
KeyboardEventManager.instance.RegisterKeyUp( KeyCode.LeftShift, new KeyEvent( ComboUp ) );
 +
KeyboardEventManager.instance.RegisterKeyUp( KeyCode.A, new KeyEvent( ComboUp ) );
 +
}
 +
 +
void ComboDown( KeyCode K)
 +
{
 +
print( "Key : " + K );
 +
if( K == KeyCode.LeftControl ) ctrl = true;
 +
if( K == KeyCode.LeftShift ) shift = true;
 +
if( K == KeyCode.A ) a = true;
 +
 +
if( ctrl && shift && a ) MEGACOMBO();
 +
}
 +
void ComboUp( KeyCode K)
 +
{
 +
print( "Key : " + K );
 +
if( K == KeyCode.LeftControl ) ctrl = false;
 +
if( K == KeyCode.LeftShift ) shift = false;
 +
if( K == KeyCode.A ) a = false;
 +
}
 +
 +
void MEGACOMBO()
 +
{
 +
print( "MEGACOMBO" );
 +
}
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 23:54, 18 October 2020


Contents

[edit] Description

An event based keyboard input mager. You can associate one or several functions with a KeyCode. That way, you can detect several keys at a time.

[edit] KeyboardEventManager.cs

using UnityEngine;
using System.Collections.Generic;
 
public delegate void KeyEvent( KeyCode K );
 
public class KeyboardEventManager : MonoBehaviour 
{
	#region Singleton
	private static KeyboardEventManager m_Instance;
	public static KeyboardEventManager instance
	{
		get
		{
			if( m_Instance == null )
			{
				m_Instance = GameObject.FindObjectOfType( typeof( KeyboardEventManager ) ) as KeyboardEventManager;
				if( m_Instance == null )
					m_Instance = new GameObject( "KeyboardEventManager Temporary Instance", typeof( KeyboardEventManager ) ).GetComponent< KeyboardEventManager >();
				m_Instance.Init();
			}
			return m_Instance;
		}
	}
 
	void Awake() 
	{
		if( m_Instance == null )
		{
			m_Instance = this;
			m_Instance.Init();
		}
	}
	#endregion
 
	private List< KeyCode > keys;
	private Dictionary< KeyCode, KeyEvent > keyDownEvents, keyUpEvents;
 
	private void Init()
	{
		keyDownEvents = new Dictionary<KeyCode, KeyEvent>();
		keyUpEvents = new Dictionary<KeyCode, KeyEvent>();
		keys = new List<KeyCode>();
	}
 
	#region Registration
	public void RegisterKeyDown( KeyCode K, KeyEvent kEvent )
	{
		if( keyDownEvents.ContainsKey(K) ) 
			keyDownEvents[K] += kEvent;
		else{
			if( !keys.Contains(K) ) keys.Add( K );
			keyDownEvents.Add( K, kEvent );
		}
	}
 
	public void RegisterKeyUp( KeyCode K, KeyEvent kEvent )
	{
		if( keyUpEvents.ContainsKey(K) ) 
			keyUpEvents[K] += kEvent;
		else{
			if( !keys.Contains(K) ) keys.Add( K );
			keyUpEvents.Add( K, kEvent );
		}
	}
 
	public void UnregisterKeyDown( KeyCode K, KeyEvent kEvent, bool removeKey )
	{
		if( keyDownEvents.ContainsKey(K) ){
			keyDownEvents[K] -= kEvent;
			if( keyDownEvents[K] == null )
				keyDownEvents.Remove(K);
		}
		if( removeKey ) RemoveKey( K );
	}
 
	public void UnregisterKeyUp( KeyCode K, KeyEvent kEvent, bool removeKey )
	{
		if( keyUpEvents.ContainsKey(K) ){
			keyUpEvents[K] -= kEvent;
			if( keyUpEvents[K] == null )
				keyUpEvents.Remove(K);
		}
		if( removeKey ) RemoveKey( K );
	}
 
	public void RemoveKey( KeyCode K )
	{
		if( keyDownEvents.ContainsKey( K ) ) keyDownEvents.Remove( K );
		if( keyUpEvents.ContainsKey( K ) ) keyUpEvents.Remove( K );
		if( keys.Contains( K ) ) keys.Remove( K );
	}
	#endregion
 
	#region Key detection
	private void Update () 
	{
        foreach (KeyCode key in keys) 
		{
            if( Input.GetKeyDown(key) )
				OnKeyDown( key );
 
            if( Input.GetKeyUp(key) )
				OnKeyUp( key );
        }
	}
 
	private void OnKeyDown( KeyCode K )
	{
		KeyEvent E = null;
		if( keyDownEvents.TryGetValue(K, out E) ) 
			if( E != null )
				E(K);
	}
	private void OnKeyUp( KeyCode K )
	{
		KeyEvent E = null;
		if( keyUpEvents.TryGetValue(K, out E) )
			if( E != null )
				E(K);
	}
	#endregion
}

[edit] Usage

KeyboardEventManager inherit from MonoBehaviour so you need to put it on a game object. It uses a singleton pattern so you can access it's instance from anywhere. To add an event, use the functions RegisterKeyDown or RegisterKeyUp. To remove an event, use the functions UnregisterKeyDown or UnregisterKeyUp. To remove a key completely, use RemoveKey. Remark : Only the keys with a least one event on up or on down are checked with Input.GetKey...

[edit] Example

public class KeyEventMgrTest : MonoBehaviour 
{
	public bool ctrl, shift, a;
 
	void Start () 
	{
		KeyboardEventManager.instance.RegisterKeyDown( KeyCode.LeftControl, new KeyEvent( ComboDown ) );
		KeyboardEventManager.instance.RegisterKeyDown( KeyCode.LeftShift, new KeyEvent( ComboDown ) );
		KeyboardEventManager.instance.RegisterKeyDown( KeyCode.A, new KeyEvent( ComboDown ) );
 
		KeyboardEventManager.instance.RegisterKeyUp( KeyCode.LeftControl, new KeyEvent( ComboUp ) );
		KeyboardEventManager.instance.RegisterKeyUp( KeyCode.LeftShift, new KeyEvent( ComboUp ) );
		KeyboardEventManager.instance.RegisterKeyUp( KeyCode.A, new KeyEvent( ComboUp ) );
	}
 
	void ComboDown( KeyCode K)
	{
		print( "Key : " + K );
		if( K == KeyCode.LeftControl ) ctrl = true;
		if( K == KeyCode.LeftShift ) shift = true;
		if( K == KeyCode.A ) a = true;
 
		if( ctrl && shift && a ) MEGACOMBO();
	}
	void ComboUp( KeyCode K)
	{
		print( "Key : " + K );
		if( K == KeyCode.LeftControl ) ctrl = false;
		if( K == KeyCode.LeftShift ) shift = false;
		if( K == KeyCode.A ) a = false;
	}
 
	void MEGACOMBO()
	{
		print( "MEGACOMBO" );
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox