Win Forms UserControl未检测到按键 [英] Win Forms UserControl not detecting key presses

查看:65
本文介绍了Win Forms UserControl未检测到按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对此有很多疑问(两个四个

There are a lot of questions about this (one, two, three, four, five), but I tried the fixes in all of them and they either don't work or don't suit my purposes. Here is my basic structure:

User Control
|-Panel
  |-Picture Box (several of them, created at runtime, do not exist at design time)

由于我认为这是相关的,因此面板将其停靠栏设置为填充",将调整大小设置为增大和缩小",因此它始终覆盖整个用户控件.PictureBox始终覆盖面板的一部分,但通常不是全部(尽管有可能).

Because I think it is relevant, the Panel has its dock set to "fill" and resize set to "grow and shrink", so it always covers the entire user control. The PictureBoxes always cover a portion of the panel, but usually not all of it (although it is possible).

我特别在听 Ctrl + C ,并且我需要一种可以响应的方法,而不管哪个孩子有焦点.我想要一种可以监听任意按键的方法,以便以后进行扩展.

I am specifically listening for Ctrl + C, and I need a method that can respond regardless of which child has focus. I'd like a method that can listen for arbitrary key presses so I can expand on it later.

链接页面上的答案之一是建议使这些按键成为全局监听器,我不想这样做,因为如果它是后台应用程序,我不希望它消失.另一个建议以顶级形式检测它并将其过滤到我的用户控件中.问题是关闭的用户控件被构建为DLL,并且我不想强迫使用它的应用程序必须实现监听 Ctrl + C 的功能,这是它应该自己处理.

One of the answers on the linked pages suggests making a global listener for those key presses, I don't want to do that since I don't want it going off if it is a background application. Another suggests detecting it in the top level form and filtering it down to my User Control. The problem is that the User Control on down is being built out as a DLL, and I don't want to force the application using it to have to implement listening for Ctrl + C, this is something that it should be handling on its own.

1)我没有在UserControl上将KeyPreview属性设置为true.关于该问题的第二个答案表明,我已经重写了ProcessCmdKey,但无论尝试如何,都永远不会调用回调.

1) I have no KeyPreview property to set to true on my UserControl. The second answer on that question suggests overriding ProcessCmdKey, which I did, but the callback is never called no matter what I try.

2)此建议还建议覆盖ProcessCmdKey.正如我所说,它永远不会被调用.

2) This one also suggests overriding ProcessCmdKey. As I said, it is never called.

3)我没有接受按钮可以设置为true.

3) There is no accept button for me to set to true.

4) KeyDown PreviewKeyDown 回调都已实现,从未调用过.

4) The KeyDown and PreviewKeyDown callbacks have both been implemented, neither is ever called.

5)还建议使用ProcessCmdKey.

5) Also suggests ProcessCmdKey.

无论焦点如何,如何在用户控制级别检测关键事件?另外,如果我尝试了上述方法,但我错过了哪些设置却无法正常工作?

推荐答案

OP:我专门在听Ctrl + C,我需要一个可以不管哪个孩子有重点.

OP: I am specifically listening for Ctrl + C, and I need a method that can respond regardless of which child has focus.

如果您想从控件中处理诸如 Ctrl + C 之类的组合键,即使它没有焦点或无法选择,也可以添加一个不可见的将 MenuStrip 添加到您的用户控件,然后向其中添加一个项目并为其分配快捷方式.然后处理项目的点击事件并做您需要的事情.

If you want to handle a key combination like Ctrl+C from your control even if it doesn't have focus or it's not selectable, you can add an invisible MenuStrip to your user control and add an item to it and assign the shortcut to it. Then handle click event of item and do what you need.

即使您的控件不包含焦点,每次用户按下 Ctrl + C 时,都会引发click事件.

The click event will be raised each time the user press Ctrl+C even if your control doesn't contain focus.

您也可以使用代码进行操作:

You can also do it using code:

public UserControl1()
{
    InitializeComponent();
    var menu = new MenuStrip();
    var item = new ToolStripMenuItem();
    item.ShortcutKeys = System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.C;
    item.Click += item_Click;
    menu.Items.Add(item);
    menu.Visible = false;
    this.Controls.Add(menu);
}
void item_Click(object sender, EventArgs e)
{
    MessageBox.Show("Ctrl + C");
}

注意

您不能在没有焦点的情况下处理按键事件,但是使用 MenuStrip 可以捕获使用上述方法想要的快捷键.

You can't handle key events without having focus, but using a MenuStrip you can trap shortcut keys which you want using above method.

使其工作的原因是, Form ContainerControl ,并且 ContainerControl 调用 ToolStripManager.ProcessCmdKey 方法在 ProcessCmdKey 中导致导致 ToolStripManager 的所有非上下文菜单条的处理快捷方式.
有关更多信息,请参见
源代码代码用于 ContainerControl.ProcessCmdKey .

The reason which makes it working is, the Form is ContainerControl and ContainerControl calls ToolStripManager.ProcessCmdKey method in ProcessCmdKey which cause processing shortcuts of all non-contextmenu strips of the ToolStripManager.
For more information take a look at source code for ContainerControl.ProcessCmdKey.

这篇关于Win Forms UserControl未检测到按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆