在具有webview的WinRT应用程序(XAML和C#)中,如何处理KeyDown事件?

在具有webview的WinRT应用程序(XAML和C#)中,如何处理KeyDown事件?,c#,xaml,windows-runtime,C#,Xaml,Windows Runtime,不会触发KeyDown和KeyUP事件。以下是我正在使用的XAML: public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); Uri mapUri = new Uri(@"http://www.google.com"); oneView.Navigate(mapUri); } prot

不会触发KeyDown和KeyUP事件。以下是我正在使用的XAML:

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; }
}

我认为您可以做两件事:

  • 使用
    WebViewBrush
    而不是
    WebView
  • WebView
    中使用JavaScript处理按键操作,并通过将其传递到XAML层

  • 使用WebViewBrush似乎不是一种干净的方法。为什么我们必须使用WebViewBrush?因为
    WebView
    接管了键盘输入,它无法到达
    CoreWindow
    或XAML控件。
    WebViewBrush
    允许您只显示呈现的页面,而不允许您与内容交互,因此您的XAML元素可以保留键盘输入捕获。
    <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>