Samuele Martinelli
Samuele Martinelli

Reputation: 23

UWP problems in detecting the "Enter" key pressed

I'm trying to run some functions in my app when the Enter key is pressed on the keyboard, but I'm having problems in doing that.

KeyboardControl is in the KeyDown of my textbox.

Key.Enter is not recognized as a function, and I don't know what to do.

    // When a key is pressed on the keyboard
    private void KeyboardControl(object sender, KeyEventArgs e)
    {
        if (e.KeyStatus == Key.Enter)
        {
            PercentCalc();

            PercentageValue.Text = Convert.ToString(result, new CultureInfo("en-US")) + "%";
        }
    }

Upvotes: 1

Views: 3395

Answers (1)

Muhammad Touseef
Muhammad Touseef

Reputation: 4455

Attach KeyDown event to your TexBox like this :

<TextBox KeyDown="Box_KeyDown" />

at the backend keydown event check if the pressed key is Enter and then execute your code in that if condition.

private async void Box_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
    if (e.Key == Windows.System.VirtualKey.Enter)
    {//execute code here
        PercentCalc();

        PercentageValue.Text = Convert.ToString(result, new CultureInfo("en-US")) + "%";

    }
}

you were trying to check the KeyStatus which is not required in your usecase, instead you should be checking which key is pressed.

Upvotes: 6

Related Questions