Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable hotkeys #288

Closed
Sulquendi opened this issue Jun 17, 2020 · 14 comments
Closed

Disable hotkeys #288

Sulquendi opened this issue Jun 17, 2020 · 14 comments
Labels
feature request feature request

Comments

@Sulquendi
Copy link

Sulquendi commented Jun 17, 2020

Hello, i'd like to be able to disable all accelerator keys while using WebView2. I've read about an AcceleratorKeyPressed event but can't find it in C# Winforms... I tried with the existing KeyDown and KeyUp, (returning e.SuppressedKeypress = true;) and also with KeyPressed (returning e.Handled = true;) but no success...

AB#27045718

@pagoe-msft pagoe-msft added .NET feature request feature request labels Jun 17, 2020
@pagoe-msft
Copy link

@Sulquendi

Thanks for raising this issue! Its still early days for our WinForms control and as of now, we currently don't have a way to handle accelerator keys like we do in our Win32 and WPF controls. This functionality is on the roadmap!

@kirsan31
Copy link

kirsan31 commented Oct 29, 2020

Any news? Disable context menu will be good too.
Current keyboard support is near zero :( To catch keyboard (mouse too) events we need to use Javascript, but if this is known combination - all is useless. For example we need Ctrl+G shortcut - we can handle it through Javascript but WebView2 will open search bar any way :((((

@champnic
Copy link
Member

champnic commented Nov 3, 2020

The AcceleratorKeyPressed event will be exposed on the WebView2 .NET controls in the next prerelease SDK. In the meantime though, you can explicitly initialize the control and create a CoreWebView2Controller to listen to the AcceleratorKeyPressed event.

@kirsan31
Copy link

kirsan31 commented Nov 3, 2020

@champnic

In the meantime though, you can explicitly initialize the control and create a CoreWebView2Controller to listen to the AcceleratorKeyPressed event.

Thank you for your answer. Sorry, but I can't understand how to achieve this :( CoreWebView2 is uninitialized (null). I am create new CoreWebView2Environment, get WebView2Controller from it and than pass env to EnsureCoreWebView2Async. But it still creates its own CoreWebView2Controller and CoreWebView2 instances:
image

Even more. If we subscribed to AcceleratorKeyPressed through reflection - event is work, but disable hotkeys (e.Handled = true;) - not:

private void Controller_AcceleratorKeyPressed(object sender, CoreWebView2AcceleratorKeyPressedEventArgs e)
{
    if (e.VirtualKey == 71 && ModifierKeys.HasFlag(Keys.Control))
    {
        SetMsg("Ctrl+G", false);
        e.Handled = true;
    }
}

wvhk

@champnic
Copy link
Member

champnic commented Nov 3, 2020

The Handled issue is known and tracked in #549 - a fix should be available in the next SDK.

You're right - looks like we have a bug where the controller gets overwritten by the control, so unfortunately sounds like you'll have to wait for the next SDK when the AcceleratorKeyPressed event becomes available on the WebView2 controls. I'm going to open a bug on our backlog for the controller issue. Thanks for the investigation here!

@pagoe-msft
Copy link

Hi Everyone,

This was addressed in the 1.0.707-prerelease version.

@champnic
Copy link
Member

champnic commented Dec 1, 2020

Hey all - I had a mistake in a previous response. The controller is not intended to be created or interacted with for the WPF/Winforms controls. It can be used directly if you are writing your own .NET control and using the CoreWebView2* classes.

The CoreWebView2Controller.AcceleratorKeyPressed events are forwarded to the WebView2.KeyDown events, and apparently have been for a while. We're removing exposing the AcceleratorKeyPressed event and have updated our documentation to try and make this more clear.
https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.winforms.webview2?view=WebView2-dotnet-1.0.664.37#remarks

Accelerator key presses (e.g. Ctrl+P) that occur within the control will fire standard key press events such as OnKeyDown. You can suppress the control's default implementation of an accelerator key press (e.g. printing, in the case of Ctrl+P) by setting the Handled property of its EventArgs to true. Also note that the underlying browser process is blocked while these handlers execute, so:
You should avoid doing a lot of work in these handlers.
Some of the WebView2 and CoreWebView2 APIs may throw errors if invoked within these handlers due to being unable to communicate with the browser process.
If you need to do a lot of work and/or invoke WebView2 APIs in response to accelerator keys then consider kicking off a background task or queuing the work for later execution on the UI thread.

I'm going to close this issue. Please let me know if you are running into other issues or are having trouble getting the events to work properly. Thanks!

@champnic champnic closed this as completed Dec 1, 2020
@kirsan31
Copy link

kirsan31 commented Dec 2, 2020

@champnic thank you, but currently (1.0.664.37) this is not working due to KeyDown and focus bugs :(

  1. The link you have provided is wrong?
  2. WebView2 Can't be focused with mouse while in NonInit State. With tab focus change all is ok.
  3. After Init, WebView2 can be focused with mouse, but KeyDown will behave wrong (see below). With tab focus change all is ok.
    WVHK
Code
public partial class Form1 : Form
{
    private string _LastText;
    public Form1()
    {
        InitializeComponent();
        webView21.KeyDown += WebView21_KeyDown;
    }

    private void WebView21_KeyDown(object sender, KeyEventArgs e)
    {
        string info = $"Key: {e.KeyCode}; KeyData: {e.KeyData}; Control: {e.Control}.\n";
        if (_LastText != info)
        {
            _LastText = info;
            richTextBox1.Text += info;
        }

        if (e.KeyData == (Keys.Control | Keys.G))
            e.Handled = true;
    }

    private async void btnInit_Click(object sender, EventArgs e)
    {
        await webView21.EnsureCoreWebView2Async().ConfigureAwait(true);
    }
}

AcceleratorKeyPressed (through reflection) works normally with both mouse and tab focus. But naturally only in the initialized state.
Repro:
WebView2Test.zip

@champnic
Copy link
Member

  1. Sorry, that link was quite wrong. I've updated it 👍
    2/3. I saw you opened a separate issue ([WinForms] Focus and KeyDown bugs. #721) for the focus problems, and we'll continue discussion/track fix using that.

@ryanroe
Copy link

ryanroe commented Nov 24, 2022

Any news? Disable context menu will be good too. Current keyboard support is near zero :( To catch keyboard (mouse too) events we need to use Javascript, but if this is known combination - all is useless. For example we need Ctrl+G shortcut - we can handle it through Javascript but WebView2 will open search bar any way :((((

have you tried with javascript

window.onkeydown = (e) => {
  if(e.ctrlKey && e.code === 'KeyG') {
      e.preventDefault();
      return;
  }
}

i don't know if this works on wpf / winform version , but this works on tauri :)

@otykier
Copy link

otykier commented Nov 10, 2023

Why is this issue closed? As far as I can tell, even with the latest build for WinForms, it's still not possible to disable specific hotkeys (such as Ctrl+G) in order to redirect them to the parent?

@champnic
Copy link
Member

@otykier Are you listening to the KeyDown event on the WebView2 control and marking Ctrl+G as handled?

@otykier
Copy link

otykier commented Nov 12, 2023

@champnic yes, and this partly works in the sense that the WebView2 control ignores the keypress. However, I expected the KeyDown event to propagate up to the parent control/form in this case, but that doesn't happen.

@champnic
Copy link
Member

Gotcha - Yes that's currently expected for WebView2. You can handle the event yourself if you want to in the WebView2 KeyDown or continue to propagate it manually. We're also working on a feature that would enable keyboard input handling to be more expected and inline with other Winforms/WPF behaviors. You can track that work here: #951 (it's not a direct duplicate, but the work entails routing input first through the UI frameworks/controls, rather than having input go straight to the WebView2 runtime hwnd).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request feature request
Projects
None yet
Development

No branches or pull requests

6 participants