简体   繁体   中英

How do I handle KeyDown event in WinRT App (XAML and C#) which has a webview?

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

The KeyDown and KeyUP event do not get fired. Here is the XAML I am using:

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

I see two things you could do:

  1. Use WebViewBrush instead of a WebView .
  2. Handle key presses with JavaScript inside of the WebView and pass them out to the XAML layer through ScripNotify .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM