MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
449 stars 55 forks source link

[Problem/Bug]: OnKeyDown event not received when typing keys "p", "o", "i" on PDFViewer #4721

Open thanhbao1996 opened 2 months ago

thanhbao1996 commented 2 months ago

What happened?

When I open a pdf file on webview 2, I want to block users from using Ctrl + P to print. The OnKeyDown event doesn't recognize the P key so I can't intercept it

Importance

Blocking. My app's basic functions are not working due to this issue.

Runtime Channel

Stable release (WebView2 Runtime), Prerelease (Edge Canary/Dev/Beta)

Runtime Version

126.0.25.92.102

SDK Version

No response

Framework

Winforms

Operating System

Windows 10

OS Version

Enterprise LTSC 1809

Repro steps

  1. Attach the OnKeyDown event to webview2
  2. Open a PDF file
  3. Press P, O, I....

Repros in Edge Browser

No, issue does not reproduce in the corresponding Edge version

Regression

No, this never worked

Last working version (if regression)

No response

AB#53007917

johna-ms commented 2 months ago

@thanhbao1996 are you referring to javascript onkeydown event or winforms keydown event? For javascript I am seeing p, o, and i onkeydown events even when preceded by 'ctrl.'

For .NET WebView2 controls, accelerator key combinations should trigger KeyDown event, so you should listen to that and suppress from there. I'm seeing that I can suppress Ctrl+O combination by doing this. However, I'm seeing Ctrl+P is not working for some reason.

For that I will file a bug on our side and track this issue,

champnic commented 2 months ago

✅ Successfully linked to Azure Boards work item(s):

thanhbao1996 commented 2 months ago

@johna-ms Just as you mentioned, I am using the .NET WebView2 controls version and I am using the KeyDown event of the control. I also tried again, and when I press the 'o' key and set a breakpoint at the KeyDown event, it still does not respond. Make sure you are doing it on the content of the PDF file.

johna-ms commented 2 months ago

@thanhbao1996 for .NET WebView2 it is expected that most keyboard events will not trigger the control's KeyDown/KeyUp handlers. Those will only respond to certain accelerator key presses. The way to get normal key events to work with the .NET control is to add a javascript onkeydown listener and use WebMessage to communicate that to the control