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

Keyboard Event Clarification #1793

Closed
leaanthony opened this issue Oct 4, 2021 · 3 comments
Closed

Keyboard Event Clarification #1793

leaanthony opened this issue Oct 4, 2021 · 3 comments
Assignees

Comments

@leaanthony
Copy link

My application is currently using add_acceleratorkeypressed to intercept keypresses in the host. The aim was to prevent or remap keys like ctrl-p and prevent default dialogs. The problem is, it seems that those keypresses aren't getting raised as JS events, EG: window.addEventListener('keydown', console.log) will print nothing. If I return false for handled, then the print dialog will be opened, but if i return true, the event isn't raised in JS.

What's the correct approach to this problem? Thanks.

@champnic
Copy link
Member

champnic commented Oct 8, 2021

@leaanthony Hey Lea! If I understand correctly, you want to use AcceleratorKeyPressed to stop things like Print, but also want them to show up as regular key presses so you can do something else with ctrl+P? If you are ok with handling the logic in the host app you could have the logic for detecting ctrl+P, cancel the behavior, and then send ExecuteScript to call the different functionality you want.

Or have you tried doing this completely within javascript (use e.preventDefault() for example)?

Another option is to completely disable the default browser behavior using CoreWebView2Settings.AreBrowserAcceleratorKeysEnabled (I'm not sure if this would also stop 'keydown' from happening):
https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2settings.arebrowseracceleratorkeysenabled?view=webview2-dotnet-1.0.992.28

@champnic champnic self-assigned this Oct 8, 2021
@leaanthony
Copy link
Author

Hey @champnic - thanks. I was unaware of the Settings3 methods and they work perfectly for my use case. Amazing job!

@champnic
Copy link
Member

champnic commented Oct 8, 2021

Glad it's working!

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

No branches or pull requests

2 participants