簡體   English   中英

如何處理具有Web視圖的WinRT App(XAML和C#)中的KeyDown事件?

[英]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; }
}

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. 使用WebViewBrush而不是WebView
  2. WebView內部使用JavaScript處理按鍵,然后通過ScripNotify傳遞到XAML層。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM