Matthew Cole
Matthew Cole

Reputation: 1339

WPF hot key firing without modifier

I have a WPF window, which contains a button called Cancel. Under ordinary circumstances I want a user to be able to press Alt+C to cancel their actions and close the program. As such, the button is captioned "_Cancel."

Problem 1: During Window_Load, if I press C without modifiers, the Cancel_Clicked event fires, and the program closes.

Problem 2: After my program opens, assuming I don't interact with anything on the window, pressing C without modifiers will close the program.

Note that, due to problem 2, using some sort of boolean to track the "loaded" status won't work.

What am I doing wrong? Any ideas?

Upvotes: 1

Views: 2477

Answers (3)

Michael Swart
Michael Swart

Reputation: 126

I've found that this is the same behavior that is in WinForms. (I was in disbelief as well until I created a test project to try it out!). Here is a previous SO question that hits on the same scenario: WPF Accelerator Keys like Visual Studio

If you want to force the hotkey to ALWAYS be triggered with an Alt modifier, you could consider refactoring the action into a command.

Here is a very quick and hacky demonstration on how one might go about implementing such a command.

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        this.InputBindings.Add(new KeyBinding(new DoActionCommand(() => DoIt()), new KeyGesture(Key.C, ModifierKeys.Alt)));
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        DoIt();
    }

    private void DoIt()
    {
        MessageBox.Show("Did It!");
    }

}

public class DoActionCommand : ICommand
{
    private Action _executeAction;

    public DoActionCommand(Action executeAction)
    {
        _executeAction = executeAction;
    }
    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        if (_executeAction != null)
        {
            _executeAction.Invoke();
        }
    }

    #endregion
}

Upvotes: 1

Will Eddins
Will Eddins

Reputation: 13907

Your best solution is to refactor this into a command:

<Window.CommandBindings>
  <CommandBinding Command="Cancel" Executed="CancelExecuted" />
</Window.CommandBindings>

<Window.InputBindings>
  <KeyBinding Command="Cancel" Key="C" Modifiers="Alt"/>
</Window.InputBindings>

Upvotes: 2

Matthew Cole
Matthew Cole

Reputation: 1339

It looks like this is standard behavior. (Boo!)

WPF Accelerator Keys like Visual Studio

http://blogs.msdn.com/saraford/archive/2004/04/16/114935.aspx

Upvotes: 1

Related Questions