未检测到 UWP Enter 按键

UWP Enter key press not detected

我正在尝试检测我的 c# uwp 应用程序中的按键操作,但未触发回车键事件。我已将 KeyUp 事件添加到主 window 并且所有键盘键都会触发该事件,但 enter 和 space 栏键除外。关于如何检测回车键的任何想法?我添加的用于检测标签 XAML 中的按键的行是 KeyUp="MainPg_KeyUp"

我会使用键盘加速器。例如,我想在用户按下 Enter 键时调用按钮单击。代码将是:

<Button Content="Invoke Me!">
    <Button.KeyboardAccelerators>
        <KeyboardAccelerator Key="Enter" />
    </Button.KeyboardAccelerators>
</Button>

您还可以使用 alt、ctrl 或 shift 等修饰符以及键,如下所示:

<Button Content="Save">
    <Button.KeyboardAccelerators>
        <KeyboardAccelerator Key="S" Modifiers="Control" />
    </Button.KeyboardAccelerators>
</Button>

您也可以将它们放置在页面级别,但您必须在代码后面放置一个事件处理程序。 Here 是关于键盘加速的更多信息。

KeyDown 事件不会触发系统处理的键。您将不得不使用 PreviewKeyDown 事件。

https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.previewkeydown

<TextBox PreviewKeyDown="eventhandler"/>

 private void eventhandler(object sender, KeyRoutedEventArgs e)
 {
     if(e.Key == VirtualKey.Enter)
     {
     }
 }