Karthik
Karthik

Reputation: 45

Button click and virtual keyboard enter key Windows Phone 8

I am developing an app for windows phone. I have a login screen where the user must enter his username and click the LOGIN button in the UI or the enter key in the virtual keyboard of the phone. I capture both the events separately. LOGIN button has a 'Click' event which logs the user in and there is a 'KeyDown' event for the enter key in the virtual keyboard which has the same code as that of the Click event. The events work fine. It logs the user in once the login button or the enter key is pressed. But only when the login button or the enter key is pressed twice. The event gets captured in the first click (I saw the page being refreshed) but only the second click takes the user into the application. Any possible ideas to come out of this issue? Earlier I did not have the LOGIN button, only used the enter key in virtual keyboard and things were working fine in the first click

Regards Karthik

Upvotes: 1

Views: 1754

Answers (2)

Karthik
Karthik

Reputation: 45

I am not sure whether my approach was wrong. Theoretically if the logic of the HandleAll() method is present in both the Button_click and key_down event, it should give the same result (comparitevely poor performance though). Now I found that the problem is not due to using the same logic in both the events (without the HandleAll()) but because of the creation of new AppSettings in the windows phone. The following solves the issue for me:

     private void Login_button_click(object sender, RoutedEventArgs e)
    {
       //Code
        AppSettings settings = new AppSettings();
        settings.IsLoggedOutSetting = false;
        //Code
    }
 private void textbox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
           //code
            AppSettings settings = new AppSettings();
            settings.IsLoggedOutSetting = false;
          //code
        }
    }      

With the above code , the issue is solved. So from my observation, the problem was because of not creating a new setting for the App when the user is trying to log in the windows phone app.

But the answer provided above by Vladimir would NOT have led me into this issue at the first place as well as the answer by Vladimir was efficient(space and time).

Thanks to you Vladimir. I was so curious why my earlier logic didn't work and found out the windows app needs a new setting to be setup when a user logs in through either the button_click or key_down events

Upvotes: 0

Vladimir Mezentsev
Vladimir Mezentsev

Reputation: 918

Just created simple example that worked fine, try to reproduce it. Xaml:

<StackPanel Orientation="Horizontal">
    <TextBox KeyDown="TextBox_KeyDown"/>
    <Button Click="Button_Click" VerticalAlignment="Top"/>
</StackPanel>

Code-behind:

private void TextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == Windows.System.VirtualKey.Enter || e.Key == Windows.System.VirtualKey.Accept)
        HandleAll();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    HandleAll();
}

private void HandleAll()
{
    //Hit breakpoint here
}

"HandleAll" method invoked every-time when Button or Keyboard Enter clicked.

Upvotes: 5

Related Questions