简体   繁体   English

WinForms-在控制之前捕获键

[英]WinForms - Capture key up before control

I've got a form and am subscribing to the KeyUp event and handling the ALT key, this is working great but only when my form has the focus, if a control on the form has focus then this no longer works; 我有一个窗体,正在订阅KeyUp事件并处理ALT键,这很好用,但只有当我的窗体具有焦点时,如果窗体上的控件具有焦点,则该控件将不再起作用。 what can I use to capture the keys when controls have focus? 当控件具有焦点时,我可以使用什么来捕获键? I'd rather not have to subscribe to every KeyUp event on every on the form controls... 我宁愿不必订阅表单控件中每个控件上的每个KeyUp事件...

Thanks. 谢谢。

将表单的KeyPreview属性设置为true是否对您有用

Try overriding ProcessCmdKey from your form. 尝试从表单中覆盖ProcessCmdKey This will be raised regardless of which controls have focus, so long as the form is active. 只要表单处于活动状态,无论哪个控件具有焦点,都将引发此事件。 You will have to do a little more work checking the keydata. 您将需要做更多的工作来检查密钥数据。 Ex: 例如:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if ((keyData & Keys.Alt) == Keys.Alt)
        {
            Debug.WriteLine("ALT");
        }

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

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

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