即使keypreview = true,按钮也可以防止KeyDown事件触发 [英] Button prevents KeyDown event from triggering even with keypreview = true

查看:310
本文介绍了即使keypreview = true,按钮也可以防止KeyDown事件触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VS Express 12中重现的步骤:

Steps to reproduce in VS Express 12:

  • 创建一个新的Windows Forms Application项目
  • 添加按钮
  • 将表单KeyPreview设置为true
  • 将keyDown事件添加到表单
  • 只要按钮存在于表单上,事件就不会触发

我有一个项目想要同时捕获keydown和keyup事件,但是,我似乎只能使keyup事件正常工作.

I have a project where I want to catch both keydown and keyup events, however, I can only seem to get the keyup event to work.

我有一个带有单个面板,按钮和标签的表单.在形式中,将keyPreview属性设置为true,并将其链接到KeyDown和KeyUp事件. 但是,当我运行该程序时,只会触发KeyUp事件.

I have a form with a single panel, button and label on it. In the form the keyPreview property is set to true, and is linked to both the KeyDown and KeyUp events. However, when I run the program, only the KeyUp event triggers.

我尝试通过添加手动添加事件处理程序

I tried adding the event handlers manually by adding

this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);

但是它仍然不起作用.

有什么建议吗?

KeyUp事件:

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        TriggerKey(e.KeyCode, false);
    }

    private void TriggerKey(Keys e, Boolean pKeyDown)
    {
        switch (e)
        {
            case Keys.Left:
                mLeft = pKeyDown;
                break;

            case Keys.Right:
                mRight = pKeyDown;
                break;

            case Keys.Down:
                mDown = pKeyDown;
                break;

            case Keys.Up:
                mUp = pKeyDown;
                break;
        }
    }

我的Form1_KeyDown事件如下:

My Form1_KeyDown event looks like this:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    TriggerKey(e.KeyCode, true);
}

Edit2: 我尝试从表单中删除按钮,然后两个事件都正确触发.如果我重新添加它,keyDown事件将再次停止工作.为什么设置keypreview属性时按钮会产生干扰?

I've tried removing the button from my form, and then both events trigger correctly. If I add it back in, the keyDown event stops working again. Why is the button interfering when the keypreview property is set?

推荐答案

KeyPreview是VB6兼容性功能,它不是本地" Winforms.它有一个与您的问题完全匹配的问题.在查看KeyPreview的代码有机会运行并触发KeyDown事件之前,还有其他Form方法首先会在按键时产生嗅觉.他们首先吃了导航击键.就像您要查看的光标键以及Tab键一样.这符合VB6的行为,也看不到光标键.

KeyPreview is a VB6 compatibility feature, it isn't "native" Winforms. And it has a problem that exactly matches your issue. There are other Form methods that get a sniff at the keystroke first, before the code that looks at KeyPreview gets a chance to run and fire the KeyDown event. And they eat the navigation keystrokes first. Like the cursor keys that you are trying to see as well as the Tab key. This matches the VB6 behavior, it also couldn't see the cursor keys.

要保持该代码的领先地位,您需要重写表单的ProcessDialogKey()方法.像这样:

To stay ahead of that code, you'll need to override the ProcessDialogKey() method of the form. Like this:

    protected override bool ProcessDialogKey(Keys keyData) {
        switch (keyData) {
            case Keys.Left:
                //...
                return true;
        }
        return base.ProcessDialogKey(keyData);
    }

这篇关于即使keypreview = true,按钮也可以防止KeyDown事件触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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