2015-02-10 19 views
0
public sealed partial class MainPage : Page 
{ 
    public MainPage() 
    { 
     this.InitializeComponent(); 
     Uri mapUri = new Uri(@"http://www.google.com"); 
     oneView.Navigate(mapUri); 
    } 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     //Focus(FocusState.Programmatic); 
     //this.AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(Window_KeyDown), true); 
     Window.Current.CoreWindow.KeyUp += Window_KeyUp; 
     Window.Current.CoreWindow.KeyDown += Window_KeyDown; 
    } 

    void Window_KeyUp(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs e) 
    { 
     if (e.VirtualKey == VirtualKey.Control) isCtrlKeyPressed = false; 
    } 

    void Window_KeyDown(object sender, KeyEventArgs e) 
    { 
     var messageDialog = new MessageDialog(""); 
     bool isCtrlKey = CoreWindow.GetForCurrentThread().GetAsyncKeyState(Windows.System.VirtualKey.Control) == CoreVirtualKeyStates.Down; 
     if (isCtrlKey) 
     { 

      switch (e.VirtualKey) 
      { 
       case VirtualKey.P: 
        //video.Play(); 
        messageDialog.Content = "P Key Pressed"; 
        messageDialog.ShowAsync(); 
        break; 
       case VirtualKey.G: 
        messageDialog.Content = "G Key Pressed"; 
        messageDialog.ShowAsync(); 
        break; 
       case VirtualKey.A: 
        messageDialog.Content = "A Key Pressed"; 
        messageDialog.ShowAsync(); 
        break; 
       case VirtualKey.T: 
        messageDialog.Content = "T Key Pressed"; 
        messageDialog.ShowAsync(); 
        break; 
      } 
     } 
     //bool isMenuKey = CoreWindow.GetForCurrentThread().GetAsyncKeyState(Windows.System.VirtualKey.Menu) == CoreVirtualKeyStates.Down; 
     //if (isMenuKey && e.Key == VirtualKey.S) 
     //{ 
     // queryTextBox.Focus(FocusState.Keyboard); 
     // queryTextBox.SelectAll(); 
     //} 
    } 


    public bool isCtrlKeyPressed { get; set; } 
} 

KeyDown和KeyUP事件不會被觸發。下面是我使用的XAML:如何在WinRT App(XAML和C#)中處理具有webview的KeyDown事件?

<Page 
    x:Class="App5.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:App5" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
     <WebView x:Name="oneView" IsHitTestVisible="False"> 

     </WebView> 
    </Grid> 
</Page> 

回答

0

我看到兩件事情你可以做:

  1. 使用WebViewBrush而不是WebView
  2. WebView內處理JavaScript中的按鍵,並通過ScripNotify將它們傳遞到XAML圖層。
+0

看起來像使用WebViewBrush不是一個乾淨的方式來做到這一點。爲什麼我們必須使用WebViewBrush? – 2015-02-12 17:23:05

+0

因爲'WebView'接管了鍵盤輸入,並沒有達到'CoreWindow'或者XAML控件。 – 2015-02-12 18:36:33

+0

'WebViewBrush'允許您僅顯示呈現的頁面而不允許您與內容交互,因此您的XAML元素可以保留鍵盤輸入捕獲。 – 2015-02-12 18:37:53