예제 #1
0
        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);
        }
예제 #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);
        }
예제 #3
0
        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;
        }