/// <summary>
        /// Handle the KeyDown of tbHotKey. In this event handler, check the pressed keys.
        /// The keys that must be pressed in combination with the key Ctrl, Shift or Alt,
        /// like Ctrl+Alt+T. The method HotKeyRegister.GetModifiers could check whether
        /// "T" is pressed.
        /// </summary>
        private void tbHotKey_KeyDown(object sender, KeyEventArgs e)
        {
            // The key event should not be sent to the underlying control.
            e.SuppressKeyPress = true;

            // Check whether the modifier keys are pressed.
            if (e.Modifiers != Keys.None)
            {
                Keys         key       = Keys.None;
                KeyModifiers modifiers = HotKeyRegister.GetModifiers(e.KeyData, out key);

                // If the pressed key is valid...
                if (key != Keys.None)
                {
                    this.registerKey       = key;
                    this.registerModifiers = modifiers;

                    // Display the pressed key in the textbox.
                    tbHotKey.Text = string.Format("{0}+{1}",
                                                  this.registerModifiers, this.registerKey);

                    // Enable the button.
                    btnRegister.Enabled = true;
                }
            }
        }
        /// <summary>
        /// Handle the Click event of btnRegister.
        /// </summary>
        private void btnRegister_Click(object sender, EventArgs e)
        {
            try
            {
                // Register the hotkey.
                hotKeyToRegister = new HotKeyRegister(this.Handle, 100,
                                                      this.registerModifiers, this.registerKey);

                // Register the HotKeyPressed event.
                hotKeyToRegister.HotKeyPressed += new EventHandler(HotKeyPressed);

                // Update the UI.
                btnRegister.Enabled   = false;
                tbHotKey.Enabled      = false;
                btnUnregister.Enabled = true;
            }
            catch (ArgumentException argumentException)
            {
                MessageBox.Show(argumentException.Message);
            }
            catch (ApplicationException applicationException)
            {
                MessageBox.Show(applicationException.Message);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 处理tbHotKey的KeyDown,事件处理中要检查按键。
        /// 按键必须是包含Ctrl,Shift或者Alt键的,比如Ctrl+Alt+T,HotKeyRegister.GetMo-
        ///difiers方法能够检查是否“T”按键被按了。
        /// </summary>
        private void tbHotKey_KeyDown(object sender, KeyEventArgs e)
        {
            // 按键事件不应该被发送到底层控制。
            e.SuppressKeyPress = true;

            // 检查修饰键是否被按下。
            if (e.Modifiers != Keys.None)
            {
                Keys         key       = Keys.None;
                KeyModifiers modifiers = HotKeyRegister.GetModifiers(e.KeyData, out key);

                // 如果按下的按键是有效的。
                if (key != Keys.None)
                {
                    this.registerKey       = key;
                    this.registerModifiers = modifiers;

                    // 在文本框中显示按下的按键。
                    tbHotKey.Text = string.Format("{0}+{1}",
                                                  this.registerModifiers, this.registerKey);

                    // 启动按钮。
                    btnRegister.Enabled = true;
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 处理btnRegister的Click事件。
        /// </summary>
        private void btnRegister_Click(object sender, EventArgs e)
        {
            try
            {
                // 注册热键。
                hotKeyToRegister = new HotKeyRegister(this.Handle, 100,
                    this.registerModifiers, this.registerKey);

                // 注册热键事件。
                hotKeyToRegister.HotKeyPressed += new EventHandler(HotKeyPressed);

                // 更新界面。
                btnRegister.Enabled = false;
                tbHotKey.Enabled = false;
                btnUnregister.Enabled = true;
            }
            catch (ArgumentException argumentException)
            {
                MessageBox.Show(argumentException.Message);
            }
            catch (ApplicationException applicationException)
            {
                MessageBox.Show(applicationException.Message);
            }
        }
        /// <summary>
        /// Dispose the hotKeyToRegister when the form is closed.
        /// </summary>
        protected override void OnClosed(EventArgs e)
        {
            if (hotKeyToRegister != null)
            {
                hotKeyToRegister.Dispose();
                hotKeyToRegister = null;
            }

            base.OnClosed(e);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 当窗体关闭的时候处理hotKeyToRegister。
        /// </summary>
        protected override void OnClosed(EventArgs e)
        {
            if (hotKeyToRegister != null)
            {
                hotKeyToRegister.Dispose();
                hotKeyToRegister = null;
            }

            base.OnClosed(e);
        }
        /// <summary>
        /// Handle the Click event of btnUnregister.
        /// </summary>
        private void btnUnregister_Click(object sender, EventArgs e)
        {
            // Dispose the hotKeyToRegister.
            if (hotKeyToRegister != null)
            {
                hotKeyToRegister.Dispose();
                hotKeyToRegister = null;
            }

            // Update the UI.
            tbHotKey.Enabled      = true;
            btnRegister.Enabled   = true;
            btnUnregister.Enabled = false;
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 处理btnUnregister的Click事件。
        /// </summary>
        private void btnUnregister_Click(object sender, EventArgs e)
        {
            // 处理hotKeyToRegister。
            if (hotKeyToRegister != null)
            {
                hotKeyToRegister.Dispose();
                hotKeyToRegister = null;
            }

            // 更新界面。
            tbHotKey.Enabled      = true;
            btnRegister.Enabled   = true;
            btnUnregister.Enabled = false;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 处理btnUnregister的Click事件。
        /// </summary>
        private void btnUnregister_Click(object sender, EventArgs e)
        {
            // 处理hotKeyToRegister。
            if (hotKeyToRegister != null)
            {
                hotKeyToRegister.Dispose();
                hotKeyToRegister = null;
            }

            // 更新界面。
            tbHotKey.Enabled = true;
            btnRegister.Enabled = true;
            btnUnregister.Enabled = false;
        }