Search code examples
c#keypresskeycodedotnetbrowser

Key combination input and special key buttons pressing


In the docs only using of 'TAB' button is mentioned.

KeyParams paramers = new KeyParams(VirtualKeyCode.TAB, ' ');

KeyParams require char representation of the button. So how can it be done right for special 'Control' button?

KeyParams paramers = new KeyParams(VirtualKeyCode.CONTROL, ' ');

Am I right that this code would produce key combination Ctrl+A?

KeyParams paramers1 = new KeyParams(VirtualKeyCode.CONTROL, ' ');
KeyParams paramers2 = new KeyParams(VirtualKeyCode.VK_A, 'A');

webView.Browser.KeyDown(paramers1);
webView.Browser.KeyDown(paramers2);

webView.Browser.KeyUp(paramers2);
webView.Browser.KeyUp(paramers1);

Solution

  • The constructor of the KeyParams class contains the third parameter - params VirtualKeyCode[] modifiers.

    To simulate pressing Ctrl + A you can create the following KeyParams:

    KeyParams p1 = new KeyParams(VirtualKeyCode.VK_A, ' ', VirtualKeyCode.CONTROL);

    And then use the KeyDown and KeyUp methods:

    browser.KeyDown(p1); browser.KeyUp(p1);