什么是WinRT相当于InputBindings?

Bra*_*non 7 c# wpf keyboard-shortcuts windows-runtime

WPF允许我轻松地将窗口级别的键盘快捷键绑定到使用InputBindings属性的方法.在WinRT中相当于什么?将键盘快捷键绑定到WinRT中的方法的正确方法是什么?

N_A*_*N_A 7

此处描述键盘快捷键.我想你想要访问键加速键.

访问键是应用程序中一段UI的快捷方式.访问键由Alt键和字母键组成.

加速键是app命令的快捷方式.您的应用可能有也可能没有与命令完全对应的UI.加速键由Ctrl键和字母键组成.

以下示例演示了媒体播放,暂停和停止按钮的快捷键的可访问实现:

<MediaElement x:Name="Movie" Source="sample.wmv"
  AutoPlay="False" Width="320" Height="240"/>

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">

  <Button x:Name="Play" Margin="1,2"
    ToolTipService.ToolTip="shortcut key: Ctrl+P"
    AutomationProperties.AccessKey="Control P">
    <TextBlock><Underline>P</Underline>lay</TextBlock>
  </Button>

  <Button x:Name="Pause" Margin="1,2"
    ToolTipService.ToolTip="shortcut key: Ctrl+A"
    AutomationProperties.AccessKey="Control A">
    <TextBlock>P<Underline>a</Underline>use</TextBlock>
  </Button>

  <Button x:Name="Stop" Margin="1,2"
    ToolTipService.ToolTip="shortcut key: Ctrl+S"
    AutomationProperties.AccessKey="Control S">
    <TextBlock><Underline>S</Underline>top</TextBlock>
  </Button>

</StackPanel>

<object AutomationProperties.AcceleratorKey="ALT+F" />
Run Code Online (Sandbox Code Playgroud)

要点:设置AutomationProperties.AcceleratorKey或AutomationProperties.AccessKey不会启用键盘功能.它仅向UI Automation框架报告应使用哪些密钥,以便可以通过辅助技术将这些信息传递给用户.密钥处理的实现仍然需要在代码中完成,而不是XAML.您仍需要在相关控件上附加KeyDown或KeyUp事件的处理程序,以便在您的应用程序中实际实现键盘快捷键行为.此外,不会自动提供访问密钥的下划线文本修饰.如果要在UI中显示带下划线的文本,则必须将助记符中特定键的文本明确加下划线为内联下划线格式.

请参阅@Magiel的答案,了解代码方面的实现细节.


Mag*_*iel 5

重要!!设置AutomationProperties.AcceleratorKey或AutomationProperties.AccessKey不启用键盘功能.它仅向UI Automation框架报告应使用哪些密钥,以便可以通过辅助技术将这些信息传递给用户.密钥处理的实现仍然需要在代码中完成,而不是XAML.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    // Set the input focus to ensure that keyboard events are raised.
    this.Loaded += delegate { this.Focus(FocusState.Programmatic); };
}

private void Grid_KeyUp(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == VirtualKey.Control) isCtrlKeyPressed = false;
}

private void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == VirtualKey.Control) isCtrlKeyPressed = true;
    else if (isCtrlKeyPressed)
    {
        switch (e.Key)
        {
            case VirtualKey.P: DemoMovie.Play(); break;
            case VirtualKey.A: DemoMovie.Pause(); break;
            case VirtualKey.S: DemoMovie.Stop(); break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)