WPF hot key firing without modifier

大城市里の小女人 提交于 2019-12-08 05:57:11

问题


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?


回答1:


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>



回答2:


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




回答3:


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
}


来源:https://stackoverflow.com/questions/1688291/wpf-hot-key-firing-without-modifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!