【问题标题】:How can i capture Key press events?如何捕获按键事件?
【发布时间】:2015-05-05 22:44:48
【问题描述】:

我试图在按下“Ctrl+l”时显示 2 个不同的消息框,在按下“Shift+A" 也被按下。我已经完成了一切,但是当我在程序运行时按下这些按钮时,什么也没有发生。我不确定我做错了什么。

我的代码如下:

public Color()
    {
        InitializeComponent();

       ContextMenuStrip s = new ContextMenuStrip();
       ToolStripMenuItem directions = new ToolStripMenuItem();
       directions.Text = "Directions";
       directions.Click += directions_Click;
       s.Items.Add(directions);
       this.ContextMenuStrip = s;
       this.KeyDown += new KeyEventHandler(Color_KeyDown);//Added
    }
    void directions_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Just click on a color chip to see the translation");
    }
    //Keypress
    private void Color_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.L && (e.Control))
        {
            MessageBox.Show("You can choose from four different languages by clicking on the radio buttons");
        }
        else if (e.KeyCode == Keys.A && (e.Shift))
        {
            MessageBox.Show("This is version 1.0");
        }
    }

【问题讨论】:

标签: c# winforms keypress keyboard-events


【解决方案1】:

如果您想在表单或控件中捕获命令键,您必须重写 ProcessCmdKey 方法。在您的表单中使用此代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.Shift | Keys.A))
        {

        }
        else if (keyData == (Keys.Control | Keys.I))
        {

        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

在这里你可以找到更多关于processing command keys的信息。

【讨论】:

  • @PiotrWolkowski,如果可以的话,我会吻你。我阅读了您提供的链接,但我不明白。更具体地说,为什么当事件处理程序生成代码以引发、触发和触发事件时我的代码不起作用?
【解决方案2】:

您是否为处理程序 (Color_KeyDown) 订阅了诸如 PreviewKeyDown 之类的事件?我看到您连接了方向_单击以侦听您的 ToolStripMenuItem 上称为“方向”控件的鼠标点击,但是从您的代码中,我看不到您在哪里侦听关键事件。

尝试添加类似于以下内容的行:

myWinFormsControl.PreviewKeyDown += Color_KeyDown;

其中 myWinFormsControl 是您希望在触发按键事件时调用处理程序的窗口或控件。注意:确保在测试时给控件输入焦点(例如,如果它是一个文本框,它不会触发按键事件,除非文本光标在按键之前在里面)。

如果您使用 Visual Studio,另一个有用的技巧是您可以在设计视图中选择一个控件并打开属性窗格并单击小闪电图标以查看该控件的所有可用事件。您还可以在其中一个空的卖点内双击以添加新的事件处理程序。 (详情请参阅here)

从 winforms 中的文本框获取按键事件的快速示例:

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        TextBox someTextBox = new TextBox();
        someTextBox.PreviewKeyDown += someTextBox_PreviewKeyDown;

        Form myMainWindow = new Form();
        myMainWindow.Controls.Add(someTextBox);

        Application.Run(myMainWindow);
    }

    static void someTextBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (e.KeyCode == Keys.L && (e.Control))
        {
            MessageBox.Show("You can choose from four different languages by clicking on the radio buttons");
        }
        else if (e.KeyCode == Keys.A && (e.Shift))
        {
            MessageBox.Show("This is version 1.0");
        }
    }
}

并且只在表单级别捕获(也就是只要窗口具有输入焦点):

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Form myMainWindow = new Form();
        myMainWindow.PreviewKeyDown += myForm_PreviewKeyDown;

        Application.Run(myMainWindow);
    }

    static void myForm_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (e.KeyCode == Keys.L && (e.Control))
        {
            MessageBox.Show("You can choose from four different languages by clicking on the radio buttons");
        }
        else if (e.KeyCode == Keys.A && (e.Shift))
        {
            MessageBox.Show("This is version 1.0");
        }
    }
}

【讨论】:

  • 它应该去哪里。我已经尝试过我的主程序和设计器,但没有成功
  • 我添加了 this.KeyDown += new KeyEventHandler(Color_KeyDown);到程序了,还是不行:'-(
  • 我编辑了我的回复,以便快速演示如何使其与文本框一起使用。如果您给文本框输入焦点(也就是单击它)并按 Ctrl+L 或 Shift+A,您将看到相应的消息框。要回答您“它应该去哪里”的问题,它需要向您想听关键输入的任何人发送。它可以是任何东西,从您的主窗体控件一直到该窗体的一个非常具体的子控件。
  • 我不想在文本框上使用它,我想在整个表单上使用它。表单上没有一个文本框。只是单选按钮和输出标签
  • 文本框只是我用于演示目的的示例,我不知道您的表单的子控件。我用我的示例的另一个版本编辑了我的响应,这次在表单级别捕获按键...只要您的窗口处于焦点状态,Ctrl+L 和 Shift+A 就会显示您的消息框。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-04
  • 2011-09-19
  • 1970-01-01
  • 2011-05-14
  • 1970-01-01
  • 2012-08-06
相关资源
最近更新 更多