How to send keyboard keys to a Web page in UWP WebView?

Petchiammal Rajumayandi 41 Reputation points


I am developing a UWP (Windows App) app with WebView. i have loaded a URL in webview & that web page has some input field boxes. To make the app to be inline in all design, i have designed a Keyboard layout in UWP app itself.

I want to fill the Web page's input boxes with the keys pressed in this Designed Keyboard. But i am not sure how to send the pressed Key's text to webpage.

NOTE: web page can be any and is not designed by us. so i cannot get any input box from webpage with any document element id.

Kindly guide me to achieve this.

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Yan Gu - MSFT 2,671 Reputation points


    Welcome to Microsoft Q&A.

    I don’t know how your Keyboard layout implement in your UWP app. I simulate the keyboard input operation by using three buttons. In the following code, I use InputInjector API to simulate keyboard input in the Click events of buttons. The key point is to let the focus remain the input area within web page when pressing buttons which are used as keyboard. We do this by setting the value of FrameworkElement.AllowFocusOnInteraction property as false in buttons.

    Please check the following code as an example:

        <StackPanel Orientation="Horizontal">  
            <Button Content="a" Click="Button_Click" Margin="10" AllowFocusOnInteraction="False"/>  
            <Button Content="b" Click="Button_Click" Margin="10" AllowFocusOnInteraction="False"/>  
            <Button Content="c" Click="Button_Click" Margin="10" AllowFocusOnInteraction="False"/>  
        <WebView Width="1000" Height="800" x:Name="WebViewControl"  


    private void Button_Click(object sender, RoutedEventArgs e)  
        InputInjector inputInjector = InputInjector.TryCreate();  
        var key = new InjectedInputKeyboardInfo();  
        var ch = (sender as Button).Content.ToString();  
        if (ch=="a")  
            key.VirtualKey = (ushort)(VirtualKey.A);  
        else if(ch=="b")  
            key.VirtualKey = (ushort)(VirtualKey.B);  
        else if(ch=="c")  
            key.VirtualKey = (ushort)(VirtualKey.C);  
        key.KeyOptions = InjectedInputKeyOptions.None;  
        inputInjector.InjectKeyboardInput(new[] { key });  
        key.KeyOptions = InjectedInputKeyOptions.KeyUp;  
        inputInjector.InjectKeyboardInput(new[] { key });  

    Let the search box in web page get the focus, click or touch the buttons to input character.
    Please feel free to contact me if you have any concern.

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Petchiammal Rajumayandi 41 Reputation points

    Hi YanGu,

    Many thanks for your this reply :)

    It really helped me lot to do what i wanted with exceptional cases.

    The issue is i want to send special characters also. but i could not find any Virtual Key related to it.
    example: #

    Can please guide me to achieve this too ?

    0 comments No comments

  2. Petchiammal Rajumayandi 41 Reputation points


    i found solution for it by setting as below

    var key = new InjectedInputKeyboardInfo();
    key.ScanCode= (ushort)'#';
    key.KeyOptions = InjectedInputKeyOptions.Unicode;

    0 comments No comments