简体   繁体   English

使用 Visual Studio 2010 在 C# 中编程热键

[英]Programming a Hotkey in C# with Visual Studio 2010

I have a small form with 9 Check Boxes in it.我有一个带有 9 个复选框的小表格。 I am trying to make hotkeys for those boxes that correspond with the Numpad, but I'm having the darnedest time.我正在尝试为与 Numpad 对应的那些框制作热键,但我有最糟糕的时间。 I have two main problems:我有两个主要问题:

1. 1.

 private void checkBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.NumPad7)
        {
            MessageBox.Show("It's working");

        }
    }

That is my code.那是我的代码。 It works, but doesn't do what I want.它有效,但没有做我想要的。 It makes a message appear, but ONLY if that checkbox is highlighted.它会显示一条消息,但仅当该复选框突出显示时。 I think KeyPreview might help in this context, but the MSDN database didn't help me solve my problem with trying to figure out how to make KeyPreview work.我认为 KeyPreview 在这种情况下可能会有所帮助,但 MSDN 数据库并没有帮助我解决我试图弄清楚如何使 KeyPreview 工作的问题。

Second, I want the code to check the box when I hit the hotkey.其次,我希望代码在我按下热键时选中该框。 No combination I can figure using CheckState seems to work.我想不出使用 CheckState 的组合似乎有效。 If anyone has some incite, I would greatly appreciate it.如果有人煽动,我将不胜感激。


Code from Comments:来自评论的代码:

public Form2() 
{ 
    InitializeComponent(); 
    this.KeyPreview = true; 
    this.KeyDown += new KeyEventHandler(Form2_KeyDown);
} 

private void Form2_KeyDown(object sender, KeyEventArgs e) 
{ 
    switch (e.KeyCode) 
    { 
        case Keys.NumPad7: 
            MessageBox.Show("ABC"); 
            break; 

        default: 
            break; 
    } 
} 

Your question does not state wether or not you want this to be an application specific hotkey or a system hotkey.您的问题没有说明您是否希望这是特定于应用程序的热键或系统热键。 I am operating under the assumption that it is an Application specific one.我在假设它是特定于应用程序的假设下进行操作。 You were right in your assumption that you need to set the Forms KeyPreview Property to true.您的假设是正确的,您需要将 Forms KeyPreview 属性设置为 true。 You then need to put your code for setting and clearing your checkbox's in the Forms Keydown event like this:然后,您需要将用于设置和清除复选框的代码放在 Forms Keydown 事件中,如下所示:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    switch (e.KeyCode )
    {
        case Keys.NumPad1:
            if (checkBox1.Checked)
                checkBox1.Checked = false;
            else
                checkBox1.Checked = true;

            break;
        case Keys.NumPad2:
            if (checkBox2.Checked)
                checkBox2.Checked = false;
            else
                checkBox2.Checked = true;

            break;
        case Keys.NumPad3:
            if (checkBox3.Checked)
                checkBox3.Checked = false;
            else
                checkBox3.Checked = true;

            break;
        case Keys.NumPad4:
            if (checkBox4.Checked)
                checkBox4.Checked = false;
            else
                checkBox4.Checked = true;

            break;
        case Keys.NumPad5:
            if (checkBox5.Checked)
                checkBox5.Checked = false;
            else
                checkBox5.Checked = true;

            break;
        case Keys.NumPad6:
            if (checkBox6.Checked)
                checkBox6.Checked = false;
            else
                checkBox6.Checked = true;

            break;
        case Keys.NumPad7:
            if (checkBox7.Checked)
                checkBox7.Checked = false;
            else
                checkBox7.Checked = true;

            break;
        case Keys.NumPad8:
            if (checkBox8.Checked)
                checkBox8.Checked = false;
            else
                checkBox8.Checked = true;

            break;
        case Keys.NumPad9:
            if (checkBox9.Checked)
                checkBox9.Checked = false;
            else
                checkBox9.Checked = true;

            break;
        default:
            break;
        }
    }

You can then create a common EventHandler for the CheckedChanged event and check for which Checkbox was selected to run the corresponding methods.然后,您可以为 CheckedChanged 事件创建一个通用的 EventHandler,并检查选择了哪个 Checkbox 来运行相应的方法。

void checkBox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox cb = (CheckBox)sender;

    switch(cb.Name)
    {
        case "checkBox1":
            if (cb.Checked)
                // Method to use when checkBox1 is checked
            else
                // Method to use when checkBox1 is unchecked


            break;

        case "checkBox2":
            if (cb.Checked)
                // Method to use when checkBox2 is checked
            else
                // Method to use when checkBox2 is unchecked

            break;

        case "checkBox3":
            if (cb.Checked)
                // Method to use when checkBox3 is checked
            else
                // Method to use when checkBox3 is unchecked

            break;

        default:
            break;

        //Implement your other checkBox's the same way.
        }

    }

Now it's possible to simply add an ampersand & before the letter you want to be enabled with ALT.现在可以在要使用 ALT 启用的字母之前简单地添加一个 & 符号。 For more info:欲了解更多信息:

https://docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-create-access-keys?view=netdesktop-5.0 https://docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-create-access-keys?view=netdesktop-5.0

Assign the same event handler to the events from all the check boxes:为所有复选框中的事件分配相同的事件处理程序:

_checkbox1.KeyDown += checkBox_KeyDown;
_checkbox2.KeyDown += checkBox_KeyDown;

// And so on...

Then, do the check inside it:然后,在里面做检查:

private void checkBox_KeyDown(object sender, KeyEventArgs e)
{
    var checkbox = sender as CheckBox;
    if (checkbox == null) { return; }

    if (e.KeyCode == Keys.NumPad1)
    {
        _checkbox1.IsChecked = !_checkbox1.IsChecked;
        return;
    }

    if (e.KeyCode == Keys.NumPad2)
    {
        _checkbox2.IsChecked = !_checkbox2.IsChecked;
        return;
    }

    // And so on...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM