コード例 #1
0
        /// <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;
                }
            }
        }
コード例 #2
0
ファイル: MainForm.cs プロジェクト: tablesmit/OneCode
        /// <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;
                }
            }
        }