Monday, June 10, 2013

Windows Store Apps - Textbox KeyDown Triggered Twice when Enter Key is pressed

When developing Windows Store Apps apps, when Enter Key is pressed on a textbox, textbox_KeyDown event is triggering twice. I am sure this is some kind of a bug.

Let's say you have a text box and when you press enter on that particular text box you need to do something. So normally what you would do is, in the text box key down event, you will check whether you have pressed the enter key and you will write the code.

But in Windows Store Apps, you will see that the text box key down is triggered twice only when you press Enter on the text box. If you press any other key it will trigger only once. I don’t know why it is happening, but it is happening.  So here is a simple work around.
private void txtBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
   if (e.Key == VirtualKey.Enter)
   {
       if (e.KeyStatus.RepeatCount == 1)
       {
           //your code
       }
   }
}
Happy Coding.

Regards,
Jaliya