XAML Textbox control KeyDown event is fired twice when Enter key is pressed RRS feed

  • Question

  • Hi,

    In my test metro app I have a text box control and have added a handler for the KeyDown event. After a user enters some data into this text box, I am expecting him to press the 'enter' key after which I parse this data. However, I am noticing that only in the case when the 'Enter' key is pressed the KeyDown event is fired twice leading to the handler being invoked twice. When I use any other key, say Windows.System.VirtualKey.Space the event is fired only once as expected.

    For example, in XAML I have:

    <TextBox x:Name="Test" KeyDown="KeyboardKey_Pressed">

    and my CS event handler code looks like this:

    private void KeyboardKey_Pressed(object sender, KeyRoutedEventArgs e)
                if (e.Key == Windows.System.VirtualKey.Enter)
                else if ((e.Key == Windows.System.VirtualKey.Delete)
                      || (e.Key == Windows.System.VirtualKey.Back))

     If I put a breakpoint near the VerifyAnswer() statement, it hits twice when the keyboard 'Enter' key is pressed. I am using a physical keyboard attached to my desktop.

    Any ideas as to why this event is fired twice? Is there something that I may be missing here?

    Build info:

    Desktop PC with Windows 8 RP, AMD64, Visual Studio Express 2012 RC



    Thursday, July 12, 2012 9:07 PM


All replies