locked
How can I get keyboard events if a webview has focus?

    Question

  • I'm building a Windows 8 application that is designed to make use of external hard buttons mapped to keyboard keys for some interaction features (it's a proof of concept for situations where it's necessary to disable touch).  One of the pages is a full-screen webview.  After the page loads, the webview takes focus and it becomes impossible to monitor any keyboard input because the webview doesn't seem to pass any of the events up to the page level.  This makes it impossible to navigate to any other pages in the application.

    Is there a way to get keyboard events or at least some combination of keyboard key presses when a webview has focus?  The webview is displaying external pages, so I can't use javascript events.  Here is my current code, looking for an escape keyUp that is never fired:

    public sealed partial class Map : Page
        {
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                Window.Current.CoreWindow.KeyUp += Window_KeyUp;
            }
    
            public Map()
            {
                this.InitializeComponent();
                Uri mapUri = new Uri(@"http://www.bing.com/maps/");
                mapWebView1.Navigate(mapUri);
            }
    
            void Window_KeyUp(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs e)
            {
                if (e.VirtualKey == Windows.System.VirtualKey.Escape)
                {
                    this.Frame.Navigate(typeof(MainPage));
                }
            }
    
            protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                Window.Current.CoreWindow.KeyUp -= Window_KeyUp;
            }
        }


    • Edited by Michael Dorazio Saturday, March 15, 2014 12:26 AM updated URI in code
    Friday, March 14, 2014 6:08 PM

All replies

  • If you can't get the CoreWindow events then you won't be able to get them. You might get better behavior on Windows 8.1's WebView, but I haven't tried it.

    I assume the map here is just a placeholder. If the real use is specifically for maps then you can use the map controls directly rather than in a WebView.

    --Rob


    Saturday, March 15, 2014 1:17 AM
    Owner
  • I can see that you have miss a crustal step here and I can only conclude that's the problem if your overriding base Implementation you MUST reimpliment them be chose the base class code are not called so

    in      

    protected  OnNavigatedTo(NavigationEventArgs e)

    {

    //you code here...

    //then implement the base class's

    base.OnNavigatedTo(e)  do the same with OnNavigatedFrom method 

    }

       Note the Override keyword mints don't call base class methods which have all the navigation code from .Net it's self ...
    • Proposed as answer by Phumurn.Net Saturday, March 15, 2014 1:20 AM
    Saturday, March 15, 2014 1:19 AM
  • Rob, thanks for the reply.  Yes, Bing Maps is just a placeholder while the actual web content is developed.  I'm on Windows 8.1 and just reinstalled VS2013 yesterday, so I'm pretty sure 8.1 doesn't resolve the issue.
    Saturday, March 15, 2014 1:39 AM
  • Thanks for the catch - I've added the base navigation code to both OnNavigatedTo and OnNavigatedFrom, but the problem still persists.
    Saturday, March 15, 2014 1:47 AM
  • For anyone stumbling across this thread in the future:

    I was unable to solve this issue and ended up having to recode my project as a regular Windows Desktop application using WPF and the excellent WebControl provided by Awesomium, which bubbles up all keyboard events.

    Tuesday, March 18, 2014 11:47 PM
  • This is the first time I've seen this.  Couldn't you have injected any javascript you needed into the external webpage?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, March 21, 2014 3:03 PM
    Moderator