public bool Register()
        {
            // Check that we have not registered
            if (this.IsRegistered)
            {
                throw new NotSupportedException("This hotkey is already registered");
            }

            if (this.KeyCode == Keys.None)
            {
                throw new NotSupportedException("Cannot register an empty hotkey");
            }

            // Remove all modifiers from the 'main' hotkey
            uint key = (uint)this.KeyCode & (~(uint)Keys.Alt) & (~(uint)Keys.Control) & (~(uint)Keys.Shift);

            // Get unmanaged version of the modifiers code
            uint modifiers = (this.KeyCode.HasFlag(Keys.Alt) ? HotkeyHandlerNativeMethods.MOD_ALT : 0)
                             | (this.KeyCode.HasFlag(Keys.Control) ? HotkeyHandlerNativeMethods.MOD_CONTROL : 0)
                             | (this.KeyCode.HasFlag(Keys.Shift) ? HotkeyHandlerNativeMethods.MOD_SHIFT : 0);

            // Register the hotkey
            if (!HotkeyHandlerNativeMethods.RegisterHotKey(this._hotkeyTarget, this._hotkeyId, modifiers, key))
            {
                return(false);
            }

            Application.AddMessageFilter(this);

            this.IsRegistered = true;

            // We successfully registered
            return(true);
        }
Beispiel #2
0
        public void Unregister()
        {
            // Check that we have registered
            if (!this.IsRegistered)
            {
                return;
            }

            this.IsRegistered = false;

            Application.RemoveMessageFilter(this);

            // Clean up after ourselves
            HotkeyHandlerNativeMethods.UnregisterHotKey(this._hotkeyTarget, this._hotkeyId);
        }
        public void Unregister()
        {
            // Check that we have registered
            if (!this.IsRegistered)
            {
                throw new NotSupportedException("This hotkey was not registered");
            }

            Application.RemoveMessageFilter(this);

            // Clean up after ourselves
            if (!HotkeyHandlerNativeMethods.UnregisterHotKey(this._hotkeyTarget, this._hotkeyId))
            {
                throw new Win32Exception();
            }

            this.IsRegistered = false;
        }