Paolo Iommarini
Paolo Iommarini

Reputation: 259

WPF WebBrowser fires button shortcut as ALT+Key is pressed

I have a really strange problem with WPF.

A simple window with a button with a shortcut ("_Ok") and a webbrowser control. The problem is: when the cursor is in a textarea inside the webbrowser when I press "o" wpf fires the button click as I'm pressing Alt+O

Here's the XAML for Windows1.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>

And the code behind (just sets the browser content):

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");
    }
  }
}

EDIT: Fixed using this code:

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;
      }
    }
  }
}

Upvotes: 1

Views: 1111

Answers (1)

jdavies
jdavies

Reputation: 12894

According to the following link: AccessKey Pressed Event Raised when pressing access key without ALT mnemonics work in WPF without the need to press the Alt key.

There is talk in the thread about this being changed in .net 4.0 however that is what I tested it with so It seems it has not been.

The thread describes a method that could work for you though.

Upvotes: 1

Related Questions