奇怪的KeyUp行为(主窗体处理子事件?!) [英] Weird KeyUp behaviour (main form handles events from child?!)

查看:79
本文介绍了奇怪的KeyUp行为(主窗体处理子事件?!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

  1. KeyUp由主窗口(Grid_KeyUp)中的网格处理
  2. 主窗口显示一个子窗口(例如MessageBox.Show(...))
  3. 用户按下[Return](Keys.Return)关闭MessageBox
  4. 主窗口实际上从MessageBox中的该按键中获取了KeyUp事件
  1. KeyUp is handled by the grid in main window (Grid_KeyUp)
  2. Main window shows a child window (e.g. MessageBox.Show(...))
  3. User presses [Return] (Keys.Return) to close the MessageBox
  4. Main window actually GETS the KeyUp event from that keypress in MessageBox

我有一个非常简单的隔离样本,显示了我遇到的问题.只需创建一个空的WPF项目并使用以下XAML:

I have a very simple isolated sample that shows the problem I am having. Just create an empty WPF project and use this XAML:

<Grid KeyUp="Grid_KeyUp">
    <Button Click="Button_Click"></Button>
</Grid>

以及背后的代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Grid_KeyUp(object sender, KeyEventArgs e)
    {
        Console.WriteLine("KEYUP: " + e.Key);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(this, "TEST");
    }
}

如果单击按钮并按[Return]以关闭MessageBox,则将输出输出窗口:

If you click the button and press [Return] to close the MessageBox the output window will print:

KEYUP: Return

奇怪的事实:

  1. 按[Escape](打印出KEYUP: Escape)时,我会得到相同的行为,但是当我按[Space]时,控制台不会打印出任何东西!
  2. e.Source和e.OriginalSource指向主窗口中的按钮(显然,这是错误的).
  3. 如果您在MessageBox.Show(...)之后放置一个断点,则该事件不会由out KeyUp处理程序处理(即,输出为空).
  1. I get the same behaviour when pressing [Escape] (KEYUP: Escape gets printed), but when I press [Space] the console does not print out anything!
  2. e.Source and e.OriginalSource point to the button in the main window (which is, obviously, wrong).
  3. If you put a breakpoint right after MessageBox.Show(...) the event does not get handled by out KeyUp handler (i.e. the output is empty).

谁能向我解释发生了什么事?

Could anyone explain to me what is happening?

PS目标框架是:.NET 4客户端

PS Target framework is: .NET 4 Client

推荐答案

很明显,发生的事情是,当您按Enter或转义键时,消息框会关闭,并且 会在您抬起按钮之前MainWindow获得焦点并捕捉到KeyPress .

Clearly what is happening is that when you press the enter or escape, the message box closes and before you lift up the button the MainWindow gets focus and catches the KeyPress aswell .

空格键不发生这种情况的原因是,按下空格键可以将按钮按下, 但要等到您自己释放空格键后才能释放 .这意味着空格键是在未按下"按钮之前释放的,因此MessageBox仅在释放空格键后才关闭,因此MainWindow不会捕获按钮的按下.

The reason why this is not happening with the space bar is that pressing spacebar pushes the button down, but does not release it until you release the spacebar yourself. This means that the spacebar is released before the button is "unpressed" and therefore the MessageBox only closes once the spacebar is released, so the MainWindow does not catch the button press.

您可以通过按住空格键进行测试. 您会看到该按钮一直处于按下状态,但是直到您松开它才被按下.

You can test this by holding the spacebar in. You will see that the button is held down but is not pressed until you release it.

您可以创建一个在MessageBox关闭后熄灭的标志.检查它是否是第一个KeyPress(也许检查它是否在关闭后不到10毫秒内发生),然后将其忽略.

You could prolly create a flag that goes off after the MessageBox close. Check if it's the first KeyPress (and maybe check that it happened less than 10 miliseconds after the close) and then just ignore it.

我知道,它很丑陋,但是我认为您还可以做很多事情(如果我是对的话)

I know, its ugly, but I don't think there's much else you can do (if I'm right)

这篇关于奇怪的KeyUp行为(主窗体处理子事件?!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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