Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7813819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:59:25+00:00 2026-06-02T04:59:25+00:00

The problem: KeyUp is handled by the grid in main window ( Grid_KeyUp )

  • 0

The problem:

  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

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>

And the code-behind:

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");
    }
}

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

KEYUP: Return

Weird facts:

  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 Target framework is: .NET 4 Client

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:59:28+00:00Added an answer on June 2, 2026 at 4:59 am

    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 .

    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.

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Issue Weird problem. We've got two forms: the main application window and a settings
Simple problem, normally a program will produce a MessageBeep if the user presses Alt+Whatever
Problem: to show that a Not-SO-page has been discussed in SO when you at
The code given below works fine. The only problem is mouse wheel/keyup cannot be
I have commended out the problem lines so show the working code: <%@ Page
I've got a small problem with the following code: $('#body').keyup(function(){ var i = $('#body').val();
I have this function: $('.PhoneNumbers').on('keyup focusout', $('input:text[name^=Customers[0].PhoneNumbers]'), function (e) { phoneRadioBtns(e); }); The problem
I have problems with my keyup binding when cloning an element. Here's the scenario:
Problem: I am having trouble implementing a recursive image lazy load in all relevant
problem I'm using LilyPond to typeset sheet music for a church choir to perform.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.