locked
C# textbox keydown triggered twice in metro applications RRS feed

  • Question

  • Hi

    I m working on C# and XAML for metro applications. I have a textbox and I want that once enter is pressed in that textbox a new textbox appears. But instead of just one textbox, I m getting two textboxes. I did debugging also n realized that its triggerd twice. Not able to figure out why it is triggerd twice.

    here is some code of my application

    private void TextBox_KeyDown_1(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
            {
                if (e.Key == Windows.System.VirtualKey.Enter)
                {
                  //code for producing textbox
                }
                
            }

    when i m debugging, once the above block is executed, it going to LayoutAwarePage.cs

    and the control is sent to ths code snippet

     private void CoreDispatcher_AcceleratorKeyActivated(CoreDispatcher sender,
                AcceleratorKeyEventArgs args)
            {
                var virtualKey = args.VirtualKey;
    
               if ((args.EventType == CoreAcceleratorKeyEventType.SystemKeyDown ||
                    args.EventType == CoreAcceleratorKeyEventType.KeyDown) &&
                    (virtualKey == VirtualKey.Left || virtualKey == VirtualKey.Right ||
                    (int)virtualKey == 166 || (int)virtualKey == 167))
                {
                    var coreWindow = Window.Current.CoreWindow;
                    var downState = CoreVirtualKeyStates.Down;
                    bool menuKey = (coreWindow.GetKeyState(VirtualKey.Menu) & downState) == downState;
                    bool controlKey = (coreWindow.GetKeyState(VirtualKey.Control) & downState) == downState;
                    bool shiftKey = (coreWindow.GetKeyState(VirtualKey.Shift) & downState) == downState;
                    bool noModifiers = !menuKey && !controlKey && !shiftKey;
                    bool onlyAlt = menuKey && !controlKey && !shiftKey;
    
                    if (((int)virtualKey == 166 && noModifiers) ||
                        (virtualKey == VirtualKey.Left && onlyAlt))
                    {
                        // When the previous key or Alt+Left are pressed navigate back
                        args.Handled = true;
                        this.GoBack(this, new RoutedEventArgs());
                    }
                    else if (((int)virtualKey == 167 && noModifiers) ||
                        (virtualKey == VirtualKey.Right && onlyAlt))
                    {
                        // When the next key or Alt+Right are pressed navigate forward
                        args.Handled = true;
                        this.GoForward(this, new RoutedEventArgs());
                    }
                }
            }  

    once ths code block is done, the control is sent back to the function TextBox_KeyDown_1. I m nt able to understand why on the first place control is sent to layoutawarepage.cs . This code was generated when i added SplitPage in my project.

    Friday, July 6, 2012 4:16 PM

Answers

  • Hi Karishma,

    I've been able to reproduce this bug with a simpler test application, and have added the bug to our internal bug database.  In the meantime, you can modify your app with some kind of counter, and only respond to every other KeyDown event if it happens to be the Enter key.  Something like:

    int eventCount = 0;

    private void TextBox_KeyDown_1(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
           
    {
               
    if (e.Key == Windows.System.VirtualKey.Enter)
               
    {
     
                   eventCount++;
                     if (eventCount == 2)
                     {
                         eventCount = 0;
                        
    //code for producing textbox
                     }
               
    }            
           
    }

    Hope this helps,
    Andre

    • Proposed as answer by Nair S Tuesday, July 10, 2012 8:59 PM
    • Marked as answer by Jesse Jiang Thursday, July 12, 2012 11:05 AM
    Tuesday, July 10, 2012 1:58 AM

All replies

  • It so happen to read similar trouble with Text Changed event. I presume, this answer thread will give you some hint on how to address.

    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.

    Regards,
    Nairs

    Friday, July 6, 2012 4:30 PM
  • The link you gave is nt helpful because I m nt able to figure out that what exactly i should do to solve my problem but ya i forgot to mention that even i did try to textbox from C# also. even then the event for fired twice. Can any1 tell me what should i do to stop it from firing the event twice??
    Friday, July 6, 2012 7:02 PM
  • Hi Karishma,

    I've been able to reproduce this bug with a simpler test application, and have added the bug to our internal bug database.  In the meantime, you can modify your app with some kind of counter, and only respond to every other KeyDown event if it happens to be the Enter key.  Something like:

    int eventCount = 0;

    private void TextBox_KeyDown_1(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
           
    {
               
    if (e.Key == Windows.System.VirtualKey.Enter)
               
    {
     
                   eventCount++;
                     if (eventCount == 2)
                     {
                         eventCount = 0;
                        
    //code for producing textbox
                     }
               
    }            
           
    }

    Hope this helps,
    Andre

    • Proposed as answer by Nair S Tuesday, July 10, 2012 8:59 PM
    • Marked as answer by Jesse Jiang Thursday, July 12, 2012 11:05 AM
    Tuesday, July 10, 2012 1:58 AM
  • Hi Karishma,

    As Andre has pointed out, this is caused due to a bug that we are currently investigating. You can use Andre's workaround or simply move your logic for Enter key handling to the KeyUp event instead of the KeyDown event. That should help you get around this issue for the Enter key.

    Thanks

    Harini


    Harini Kannan | Program Manager, XAML

    Friday, July 13, 2012 9:10 PM
  • Sorry for the late post, but I just thought I'd give my insight into the issue. I'm experiencing this same problem but not with a TextBox. I've created a KeyDown event for the entire page, and while this issue does seem like a bug, I've managed to resolve the issue using another method:

    private void TextBox_KeyDown_1(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
    {
        if (e.Key == Windows.System.VirtualKey.Enter)
        {
            if (e.KeyStatus.RepeatCount == 1)
            {
                //code for producing textbox
            }
        }
    }

    In my opinion, this is much simpler and uses the built-in KeyStatus property of the KeyRoutedEventArgs.
    • Edited by Alexander Deeb Sunday, August 19, 2012 11:15 PM
    • Proposed as answer by Krekkon Thursday, October 11, 2012 1:08 PM
    Sunday, August 19, 2012 11:13 PM
  • Just wanted to let you know this bug still exists in Visual Studio 2013 Preview and Windows 8.1 Preview. And the Workaround with the Counter is problematic because it breaks the application once the bug is fixed.
    Thursday, July 11, 2013 6:58 PM
  • The problem seems to happen when keydown is used. You can try Keyup instead.

    Monday, August 26, 2013 7:59 AM