当按下ALT+键时,WPF浏览器会触发按钮快捷方式

本文关键字:按钮 快捷方式 浏览器 WPF ALT+ 键时 | 更新日期: 2023-09-27 18:06:20

我在使用WPF时遇到了一个非常奇怪的问题。

一个简单的窗口,带有一个快捷键按钮("_Ok")和一个浏览器控件。问题是:当光标在web浏览器内的文本区,当我按下"o"wpf触发按钮点击,因为我按下Alt+ o

XAML:
<Window x:Class="testBrowser.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="*" />
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <WebBrowser Name="m_Browser" Focusable="False" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
                Margin="0"/>
    <TextBox Name="m_Text" Grid.Row="1" />
    <Button Name="m_Ok" Content="_Ok" Click="m_Ok_Click" Grid.Row="2" />
  </Grid>
</Window>

后面的代码(只是设置浏览器内容):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace testBrowser
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
      m_Browser.NavigateToString("<textarea cols='"20'" rows='"10'"/>");
    }
    private void m_Ok_Click(object sender, RoutedEventArgs e)
    {
      MessageBox.Show("Ok pressed");
    }
  }
}

编辑:修正使用这个代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace testBrowser
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
      EventManager.RegisterClassHandler(typeof(UIElement), AccessKeyManager.AccessKeyPressedEvent, 
                                        new AccessKeyPressedEventHandler(OnAccessKeyPressed));
      m_Browser.NavigateToString("<textarea cols='"20'" rows='"10'"/>");
    }
    private void m_Ok_Click(object sender, RoutedEventArgs e)
    {
      MessageBox.Show("Ok pressed");
    }
    private static void OnAccessKeyPressed(object sender, AccessKeyPressedEventArgs e)
    {    
      if ((Keyboard.Modifiers & ModifierKeys.Alt) != ModifierKeys.Alt) {
        e.Target = null;
        e.Handled = true;
      }
    }
  }
}

当按下ALT+键时,WPF浏览器会触发按钮快捷方式

根据以下链接:AccessKey Pressed事件当按下访问键而不使用ALT助记符时在WPF中工作而不需要按ALT键

在线程中有关于这在。net 4.0中被改变的讨论,然而这是我测试的,所以它似乎没有。

这个线程描述了一个可以为你工作的方法。