Answered by:
XAML Textbox control KeyDown event is fired twice when Enter key is pressed

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)
{
VerifyAnswer(e);
return;
}
else if ((e.Key == Windows.System.VirtualKey.Delete)
|| (e.Key == Windows.System.VirtualKey.Back))
{
HideVerdict();
}
}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
Thanks,
SriniThursday, July 12, 2012 9:07 PM
Answers
-
Hi Srini,
See this previous MSDN thread for the answer to your question:
- Proposed as answer by Richard A. Simpson Friday, July 13, 2012 8:36 PM
- Marked as answer by Srini Nid Tuesday, July 17, 2012 9:14 PM
Friday, July 13, 2012 7:38 PM
All replies
-
Hi Srini,
Did you try above scenario while debugging in Simulator? I was not able to replicate the issue while debugging using "Local Machine" as target.
-Sagar
Thursday, July 12, 2012 10:01 PM -
Hi Sagar,
Yes I ran this under the VS debugger (local machine) and only when the 'enter' keystroke is pressed the handler is invoked twice. In my above code, I put the breakpoint on VerifyAnswer(e) and it's getting hit twice.
Thanks,
SriniThursday, July 12, 2012 11:45 PM -
Hi Srini,
See this previous MSDN thread for the answer to your question:
- Proposed as answer by Richard A. Simpson Friday, July 13, 2012 8:36 PM
- Marked as answer by Srini Nid Tuesday, July 17, 2012 9:14 PM
Friday, July 13, 2012 7:38 PM