2012-07-19 55 views
7

WPF允許我輕鬆地將窗口級別的鍵盤快捷鍵綁定到使用InputBindings屬性的方法。在WinRT中這與此相當甚麼?將鍵盤快捷鍵綁定到WinRT中的方法的正確方法是什麼?InputBindings的WinRT等價物是什麼?

回答

7

鍵盤快捷鍵描述爲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的回答對實施細節事物的代碼方面。

+0

那麼MouseBinding呢?請參閱StackOverflow問題http://stackoverflow.com/a/7354984。 – 2012-08-23 20:16:26

+0

@ Stefano.net我不確定你的意思。那麼鼠標綁定呢? – mydogisbox 2012-08-23 20:20:48

+0

通過InputBinding(或等價物)將鼠標事件附加到不直接像矩形處理的對象。 – 2012-08-23 20:52:06

5

重要! 設置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; 
     } 
    } 
} 
相關問題