UWP 在检测按下的“Enter”键时出现问题

0 c# keyboard-shortcuts uwp

当按下键盘上的按键时,我尝试在我的应用程序中运行某些功能Enter,但在执行此操作时遇到问题。

KeyboardControlKeyDown我的文本框中。

Key.Enter不被识别为函数,我不知道该怎么办。

    // 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")) + "%";
        }
    }
Run Code Online (Sandbox Code Playgroud)

Muh*_*eef 5

将KeyDown事件附加到您的TexBox ,如下所示:

<TextBox KeyDown="Box_KeyDown" />
Run Code Online (Sandbox Code Playgroud)

在后端 keydown 事件中检查按下的键是否为Enter,然后在该 if 条件下执行代码。

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")) + "%";

    }
}
Run Code Online (Sandbox Code Playgroud)

您试图检查用例中不需要的KeyStatus ,而是应该检查按下了哪个键。

  • 请随意添加更好的解决方案作为答案,以便我可以了解更多有关如何提供更好的解决方案的信息,谢谢 (2认同)