如何处理具有webview的WinRT App(XAML和C#)中的KeyDown事件?

时间:2015-02-10 00:07:38

标签: c# xaml windows-runtime

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:

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

1 个答案:

答案 0 :(得分:0)

我看到你可以做的两件事:

  1. 使用WebViewBrush代替WebView
  2. 使用WebView内的JavaScript处理按键,并通过ScripNotify将其传递给XAML图层。