public int Size() { if (OnKeyDown != null) { return(OnKeyDown.GetInvocationList().Length); } else { return(0); } }
private void OnTick(object sender, EventArgs args) { // Check keys foreach (var key in Enum.GetValues(typeof(Keys)).Cast <Keys>()) { var keyDown = ModAPI.Input.GetButton(key.ToString()); if (keyDown && !KeysDown.Contains(key)) { KeysDown.Add(key); // Notify listeners for key down if (OnKeyDown != null) { foreach (var action in OnKeyDown.GetInvocationList()) { try { action.DynamicInvoke(this, new KeyEventArgs(key)); } catch (Exception e) { Logger.Exception("Exception while notifying OnKeyDown listener: " + action.GetType().Name, e); } } } } else if (!keyDown && KeysDown.Contains(key)) { KeysDown.Remove(key); // Notify listeners for key up if (OnKeyUp != null) { foreach (var action in OnKeyUp.GetInvocationList()) { try { action.DynamicInvoke(this, new KeyEventArgs(key)); } catch (Exception e) { Logger.Exception("Exception while notifying OnKeyDown listener: " + action.GetType().Name, e); } } } } } }
/// <summary> /// Unsubscribes all observers or the given observer from tracking events of this handler. /// </summary> /// <param name="target">The unsubscribing object.</param> private void UnsubscribeAllOrTarget(object target = null) { if (OnKeyPressed != null) { foreach (Action action in OnKeyPressed.GetInvocationList()) { if (target == null || action.Target == target) { OnKeyPressed -= action; } } } if (OnKeyDown != null) { foreach (Action action in OnKeyDown.GetInvocationList()) { if (target == null || action.Target == target) { OnKeyDown -= action; } } } }