Example #1
0
 private void AddHotKeyItem(HotKeyItem item, bool save = true)
 {
     try
     {
         _KeyGo.AddHotKey(item);
     }
     catch (Exception ex)
     {
         if (item.Enabled)
         {
             // 禁用后再添加
             item.Enabled = false;
             _KeyGo.AddHotKey(item);
         }
         else
         {
             MessageBox.Show("在添加新的热键时异常:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
             return;
         }
     }
     FLP_AddItem(item);
     if (save)
     {
         SaveHotKeyItems(_KeyGo);
     }
 }
Example #2
0
        private bool OnHotKeyTrigger(HotKeyItem item)
        {
            var args = new HotKeyTriggerEventArgs {
                HotKeyItem = item
            };

            HotKeyTriggerEvent?.Invoke(this, args);
            return(args.Handle);
        }
Example #3
0
        /// <summary>
        /// 删除一个热键
        /// </summary>
        /// <param name="item">The item.</param>
        public void DelHotKey(HotKeyItem item)
        {
            if (item is null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (item.HotKeyID != 0)
            {
                UnRegKey(item);
            }
            Items.Remove(item);
        }
Example #4
0
        /// <summary>
        /// 添加一个新热键
        /// </summary>
        /// <param name="item">The item.</param>
        /// <exception cref="ArgumentNullException">
        /// item
        /// or
        /// HotKey - 热键不能为空!
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// 功能键不能为空!
        /// or
        /// 快捷键不能为空!
        /// </exception>
        public void AddHotKey(HotKeyItem item)
        {
            if (item is null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (item.Enabled)
            {
                RegKey(item);
            }
            Items.Add(item);
        }
Example #5
0
        /// <summary>
        /// 注销热键 - 完成后,会清零 HotKeyID
        /// </summary>
        /// <param name="item">The item.</param>
        /// <exception cref="ArgumentNullException">item</exception>
        public void UnRegKey(HotKeyItem item)
        {
            if (item is null)
            {
                throw new ArgumentNullException(nameof(item));
            }
            if (item.HotKeyID == 0)
            {
                return;
            }

            AppHotKey.UnRegKey(FormHandle, item.HotKeyID);
            item.HotKeyID = 0;
        }
Example #6
0
        /// <summary>
        /// 修改热键
        /// </summary>
        /// <param name="item">The item.</param>
        public void ChangeHotKey(HotKeyItem item)
        {
            if (item is null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            // 重新注册
            if (item.HotKeyID != 0)
            {
                UnRegKey(item);
            }
            if (item.Enabled)
            {
                RegKey(item);
            }
        }
Example #7
0
        private void FLP_AddItem(HotKeyItem item)
        {
            var ucitem = new UCHotKeyItem
            {
                KeyGo      = _KeyGo,
                HotKeyItem = item,
            };

            // 值改变则保存
            ucitem.ValueChangedEvent += (s, e) => SaveHotKeyItems(_KeyGo);

            // 添加到最后
            FLPHotKeys.Controls.Add(ucitem);

            // 与添加按钮交换位置
            var i1 = FLPHotKeys.Controls.GetChildIndex(ucitem);
            var i2 = FLPHotKeys.Controls.GetChildIndex(BtnAdd);

            FLPHotKeys.Controls.SetChildIndex(ucitem, i2);
            FLPHotKeys.Controls.SetChildIndex(BtnAdd, i1);
        }
Example #8
0
        private void BtnAccept_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(TxtProcessName.Text))
            {
                MessageBox.Show("请设置进程名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!string.IsNullOrWhiteSpace(TxtStartupPath.Text) && !File.Exists(TxtStartupPath.Text))
            {
                MessageBox.Show("请选择有效的启动路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(TxtHotKey.Text))
            {
                MessageBox.Show("请设置快捷键", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            bool Modified = true;

            if (HotKeyItem == null)
                HotKeyItem = new HotKeyItem();
            else
                Modified = !(HotKeyItem.ProcessName == TxtProcessName.Text && HotKeyItem.StartupPath == TxtStartupPath.Text && HotKeyItem.HotKey == TxtHotKey.Text);

            if (Modified)
            {
                HotKeyItem.ProcessName = TxtProcessName.Text.Trim();
                HotKeyItem.StartupPath = TxtStartupPath.Text.Trim();
                HotKeyItem.HotKey = TxtHotKey.Text;
                DialogResult = DialogResult.OK;
            }
            else
            {
                DialogResult = DialogResult.Cancel;
            }
        }
Example #9
0
        /// <summary>
        /// 注册热键 - 成功后,会设置 HotKeyID
        /// </summary>
        /// <param name="item">The item.</param>
        /// <exception cref="ArgumentNullException">
        /// item
        /// or
        /// HotKey - 热键不能为空!
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// 功能键不能为空!
        /// or
        /// 快捷键不能为空!
        /// </exception>
        public void RegKey(HotKeyItem item)
        {
            if (item is null)
            {
                throw new ArgumentNullException(nameof(item));
            }
            if (string.IsNullOrWhiteSpace(item.HotKey))
            {
                throw new ArgumentNullException(nameof(item.HotKey), "热键不能为空!");
            }

            // 如果注册过该热键,ID不为0。卸载热键会将ID置零。
            if (item.HotKeyID != 0)
            {
                return;
            }

            int id = Interlocked.Increment(ref _RegMaxID);

            var  keys    = item.HotKey.Split('+');
            Keys keyCode = Keys.None;

            AppHotKey.KeyModifiers keyModifiers = AppHotKey.KeyModifiers.None;
            foreach (var key in keys)
            {
                switch (key.ToLower())
                {
                case "ctrl":
                    keyModifiers |= AppHotKey.KeyModifiers.Ctrl;
                    break;

                case "shift":
                    keyModifiers |= AppHotKey.KeyModifiers.Shift;
                    break;

                case "alt":
                    keyModifiers |= AppHotKey.KeyModifiers.Alt;
                    break;

                case "win":
                    keyModifiers |= AppHotKey.KeyModifiers.WindowsKey;
                    break;

                default:
                    keyCode = (Keys)Enum.Parse(typeof(Keys), key);
                    break;
                }
            }

            if (keyModifiers == AppHotKey.KeyModifiers.None)
            {
                throw new InvalidOperationException("功能键不能为空!");
            }
            if (keyCode == Keys.None)
            {
                throw new InvalidOperationException("快捷键不能为空!");
            }

            AppHotKey.RegKey(FormHandle, id, keyModifiers, keyCode);
            item.HotKeyID = id;
        }