Suppress/Intercept Alt+Left/Alt+WinForms WebBrowser 控件中的右键

Suppress/Intercept Alt+Left/Alt+Right in WinForms WebBrowser Control

我们有一个 WinForms 应用程序,其中 WebBrowser control on a form (currently compiling it for the classic .NET Framework 4.7.2). We need to suppress or intercept the Alt+Left and Alt+Right keyboard combinations when this WebBrowser control has the input focus to implement our own history navigation. We tried several methods including overwriting of the Control.ProcessCmdKey() 用于控件或整个表单,但这没有帮助。如何在 C# 或 VB.NET?

的 .NET Framework 中做我们需要的事情

虽然您无法在不出现运行时错误的情况下订阅 WebBrowser KeyDown、KeyUp 或 KeyPress 事件,但您可以订阅 WebBrowser.PreviewKeyDown Event 以检测 Alt-Left ArrowAlt-RightArrow 组合键。

如果您在事件处理程序中将 PreviewKeyDownEventArgs.IsInputKey Property 设置为 true,WebBrowser 控件将不会将该组合解释为 WebBrowser。GoBack/WebBrowser.GoFoward 命令。

Private Sub WebBrowser1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles WebBrowser1.PreviewKeyDown
  If e.KeyData = (Keys.Left Or Keys.Alt) OrElse e.KeyData = (Keys.Right Or Keys.Alt) Then
    ' setting IsInputKey = true prevents the Browser for processing 
    ' Alt-Left Arrow and Alt-Right Arrow as history navigation
    e.IsInputKey = True
  End If
End Sub