【问题标题】:what is the WinRT equivalent of InputBindings?InputBindings 的 WinRT 等价物是什么?
【发布时间】:2012-07-19 05:54:42
【问题描述】:

WPF 允许我轻松地将窗口级别的键盘快捷键绑定到使用 InputBindings 属性的方法。 WinRT 中的这个等价物是什么?将键盘快捷键绑定到 WinRT 中的方法的正确方法是什么?

【问题讨论】:

    标签: c# wpf keyboard-shortcuts windows-runtime


    【解决方案1】:

    键盘快捷键描述为here。我想你想要access keysaccelerator keys

    访问键是应用中某个 UI 的快捷方式。访问键由 Alt 键和一个字母键组成。

    加速键是应用命令的快捷方式。你的应用可能有也可能没有与命令完全对应的 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" />
    

    Important: Setting AutomationProperties.AcceleratorKey or AutomationProperties.AccessKey doesn't enable keyboard functionality. It only reports to the UI Automation framework what keys should be used, so that such information can be passed on to users via assistive technologies. The implementation for key handling still needs to be done in code, not XAML. You will still need to attach handlers for KeyDown or KeyUp events on the relevant control in order to actually implement the keyboard shortcut behavior in your app. Also, the underline text decoration for an access key is not provided automatically. You must explicitly underline the text for the specific key in your mnemonic as inline Underline formatting if you wish to show underlined text in the UI.

    有关代码方面的实现细节,请参阅@Magiel 的回答。

    【讨论】:

    • 那么鼠标绑定呢?请参阅 StackOverflow 问题 stackoverflow.com/a/7354984
    • @Stefano.net 我不确定你的意思。鼠标绑定呢?
    • 通过 InputBinding(或等效方法)将鼠标事件附加到不直接处理的对象,例如矩形。
    • @Stefano.net 啊,你想给控件添加事件吗?那将是一个完全不同的问题,但我认为答案将是作文。只需更改已实现所需事件的控件模板即可。
    • 虽然第一个链接包含一些有用的信息 - 您的其余答案具有误导性,因为它建议指定工具提示、AutomationProperties 或加下划线的访问键可能会使键起作用。从 UX 的角度来看,将这些组合键传达给用户很重要,但这并不能使它们真正起作用。您需要在根控件或 Window.Current.CoreWindow 上处理 KeyUp/KeyDown 事件(我认为如果没有控件具有焦点,前者将无法工作)。
    【解决方案2】:

    重要!! 设置 AutomationProperties.AcceleratorKey 或 AutomationProperties.AccessKey 不会启用键盘功能。它只向 UI 自动化框架报告应该使用哪些键,以便可以通过辅助技术将这些信息传递给用户。密钥处理的实现仍然需要在代码中完成,而不是 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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-08
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 2022-11-28
      • 1970-01-01
      • 2018-07-10
      相关资源
      最近更新 更多