Пример #1
0
 /// <summary>
 /// Provides data for the <see cref="HotkeyListener.HotkeyUpdated"/> event.
 /// </summary>
 /// <param name="updatedHotkey">
 /// The hotkey that has been updated.
 /// </param>
 /// <param name="newHotkey">
 /// The hotkey's newly updated value.
 /// </param>
 public HotkeyUpdatedEventArgs(Hotkey updatedHotkey, Hotkey newHotkey)
 {
     _updatedHotkey = updatedHotkey;
     _newHotkey     = newHotkey;
 }
Пример #2
0
        /// <summary>
        /// [Helper] Converts keys or key combinations to their string types.
        /// </summary>
        /// <param name="hotkey">The hotkey to convert.</param>
        public static string AsString(Hotkey hotkey)
        {
            try
            {
                var _hotkey    = hotkey.KeyCode;
                var _modifiers = hotkey.Modifiers;

                string parsedHotkey = string.Empty;

                // No modifier or shift only, and a hotkey that needs another modifier.
                if ((_modifiers == Keys.Shift || _modifiers == Keys.None))
                {
                    if (NeedNonShiftModifier != null && NeedNonShiftModifier.Contains((int)_hotkey))
                    {
                        if (_modifiers == Keys.None)
                        {
                            // Set Ctrl+Alt as the modifier unless Ctrl+Alt+<key> won't work.
                            if (NeedNonAltGrModifier.Contains((int)_hotkey) == false)
                            {
                                _modifiers = Keys.Alt | Keys.Control;
                            }
                            else
                            {
                                // ...In that case, use Shift+Alt instead.
                                _modifiers = Keys.Alt | Keys.Shift;
                            }
                        }
                        else
                        {
                            // User pressed Shift and an invalid key (e.g. a letter or a number),
                            // that needs another set of modifier keys.
                            _hotkey = Keys.None;
                        }
                    }
                }

                // Without this code, pressing only Ctrl will show up as "Control + ControlKey", etc.
                if (_hotkey == Keys.Menu || /* Alt */
                    _hotkey == Keys.ShiftKey ||
                    _hotkey == Keys.ControlKey)
                {
                    _hotkey = Keys.None;
                }

                if (_modifiers == Keys.None)
                {
                    // LWin/RWin don't work as hotkeys... (neither do they work as modifier keys in
                    // .NET 2.0).
                    if (_hotkey == Keys.None || _hotkey == Keys.LWin || _hotkey == Keys.RWin)
                    {
                        parsedHotkey = string.Empty;
                    }
                    else
                    {
                        parsedHotkey = _hotkey.ToString();
                    }
                }
                else
                {
                    parsedHotkey = _modifiers.ToString() + " + " + _hotkey.ToString();
                }

                return(parsedHotkey);
            }
            catch (Exception)
            {
                return(string.Empty);
            }
        }
Пример #3
0
 /// <summary>
 /// [Special] Converts keys or key combinations to their string types.
 /// </summary>
 /// <param name="hotkey">The hotkey to convert.</param>
 public static string Convert(Hotkey hotkey)
 {
     return(_selector.Convert(hotkey));
 }
Пример #4
0
 /// <summary>
 /// Removes any specific hotkey
 /// from the global Key watcher.
 /// </summary>
 /// <param name="hotkey">The hotkey to remove.</param>
 public void Remove(Hotkey hotkey)
 {
     _handle.RemoveKey(Convert(hotkey));
 }
Пример #5
0
        /// <summary>
        /// Updates an existing hotkey
        /// in the global Key watcher.
        /// </summary>
        /// <param name="currentHotkey">The hotkey to modify.</param>
        /// <param name="newHotkey">The new hotkey to be set.</param>
        public void Update(Hotkey currentHotkey, Hotkey newHotkey)
        {
            Update(currentHotkey.ToString(), newHotkey.ToString());

            HotkeyUpdated?.Invoke(this, new HotkeyUpdatedEventArgs(currentHotkey, newHotkey));
        }
Пример #6
0
        /// <summary>
        /// [Helper] Converts keys or key combinations to their string types.
        /// </summary>
        /// <param name="hotkey">The hotkey to convert.</param>
        public string Convert(Hotkey hotkey)
        {
            try
            {
                _hotkey    = hotkey.KeyCode;
                _modifiers = hotkey.Modifiers;

                string parsedHotkey = string.Empty;

                // No hotkey set.
                if (_hotkey == Keys.None)
                {
                    parsedHotkey = string.Empty;
                }

                // LWin/RWin don't work as hotkeys...
                // (neither do they work as modifier keys in .NET 2.0).
                if (_hotkey == Keys.LWin || _hotkey == Keys.RWin)
                {
                    parsedHotkey = string.Empty;
                }

                // No modifier or shift only, and a hotkey that needs another modifier.
                if ((_modifiers == Keys.Shift || _modifiers == Keys.None) &&
                    _needNonShiftModifier.Contains((int)this._hotkey))
                {
                    if (this._modifiers == Keys.None)
                    {
                        // Set Ctrl+Alt as the modifier unless Ctrl+Alt+<key> won't work.
                        if (_needNonAltGrModifier.Contains((int)this._hotkey) == false)
                        {
                            this._modifiers = Keys.Alt | Keys.Control;
                        }
                        else
                        {
                            // ...In that case, use Shift+Alt instead.
                            this._modifiers = Keys.Alt | Keys.Shift;
                        }
                    }
                    else
                    {
                        // User pressed Shift and an invalid key (e.g. a letter or a number),
                        // that needs another set of modifier keys.
                        this._hotkey = Keys.None;
                        parsedHotkey = this._modifiers.ToString() + $" + {InvalidHotkeyText}";
                    }
                }

                // [Disabled] Check all Ctrl+Alt keys.
                // ---------------------------------------------------------------------------
                // if ((this._modifiers == (Keys.Alt | Keys.Control)) &&
                //     this._needNonAltGrModifier.Contains((int)this._hotkey))
                // {
                //     // Ctrl+Alt+4 etc won't work; reset hotkey and tell the user.
                //     this._hotkey = Keys.None;
                //     parsedHotkey = this._modifiers.ToString() + $" + {InvalidHotkeyText}";
                // }
                // ---------------------------------------------------------------------------

                if (this._modifiers == Keys.None)
                {
                    if (this._hotkey == Keys.None)
                    {
                        parsedHotkey = EmptyHotkeyText;
                    }
                    else
                    {
                        // We get here if we've got a hotkey that is valid without a modifier,
                        // like F1-F12, Media-keys etc.
                        parsedHotkey = this._hotkey.ToString();
                    }
                }

                // Without this code, pressing only Ctrl
                // will show up as "Control + ControlKey", etc.
                if (this._hotkey == Keys.Menu || /* Alt */
                    this._hotkey == Keys.ShiftKey ||
                    this._hotkey == Keys.ControlKey)
                {
                    this._hotkey = Keys.None;
                }

                parsedHotkey = this._modifiers.ToString() + " + " + this._hotkey.ToString();

                return(parsedHotkey);
            }
            catch (Exception)
            {
                return(string.Empty);
            }
        }
Пример #7
0
 /// <summary>
 /// Removes any specific hotkey from the global Key watcher.
 /// </summary>
 /// <param name="hotkey">The hotkey to remove.</param>
 public void Remove(Hotkey hotkey)
 {
     _handle.RemoveKey(HotkeyRepresentation.AsString(hotkey));
 }