public static void UnregisterFormHotKey(Keys keys, Function <bool, Keys> callback) { if (hotkeyRegistrar != null) { Function <bool, Keys> theDelegate = hotkeyRegistrar[keys]; theDelegate -= callback; hotkeyRegistrar[keys] = theDelegate; IComponent targetAsComponent = callback.Target as IComponent; if (targetAsComponent != null) { targetAsComponent.Disposed -= TargetAsComponent_Disposed; } IHotKeyTarget targetAsHotKeyTarget = callback.Target as IHotKeyTarget; if (targetAsHotKeyTarget != null) { targetAsHotKeyTarget.Disposed -= TargetAsHotKeyTarget_Disposed; } if (theDelegate.GetInvocationList().Length == 0) { hotkeyRegistrar.Remove(keys); } if (hotkeyRegistrar.Count == 0) { hotkeyRegistrar = null; } } }
/// <summary> /// Registers a form-wide hot key, and a callback for when the key is pressed. /// The callback must be an instance method on a Control. Whatever Form the Control /// is on will process the hotkey, as long as the Form is derived from PdnBaseForm. /// </summary> public static void RegisterFormHotKey(Keys keys, Function <bool, Keys> callback) { IComponent targetAsComponent = callback.Target as IComponent; IHotKeyTarget targetAsHotKeyTarget = callback.Target as IHotKeyTarget; if (targetAsComponent == null && targetAsHotKeyTarget == null) { throw new ArgumentException("target instance must implement IComponent or IHotKeyTarget", "callback"); } if (hotkeyRegistrar == null) { hotkeyRegistrar = new Dictionary <Keys, Function <bool, Keys> >(); } Function <bool, Keys> theDelegate = null; if (hotkeyRegistrar.ContainsKey(keys)) { theDelegate = hotkeyRegistrar[keys]; theDelegate += callback; hotkeyRegistrar[keys] = theDelegate; } else { theDelegate = new Function <bool, Keys>(callback); hotkeyRegistrar.Add(keys, theDelegate); } if (targetAsComponent != null) { targetAsComponent.Disposed += TargetAsComponent_Disposed; } else { targetAsHotKeyTarget.Disposed += TargetAsHotKeyTarget_Disposed; } }