Only the definition of pressing a single key case Key.LeftCtrl works:

<Window KeyDown="KeyDownMy" > 
 void KeyDownMy(object sender, KeyEventArgs e) { switch (e.Key) { //case Key.LeftCtrl & Key.S: case Key.LeftCtrl: e.Handled = true; NodeSave(null, null); break; } } 

    2 answers 2

    Do you want to bind Ctrl + S to the save command? This is done wrong.

    You in the window in which this command should work, bind the key combination to the command.

    If your command is available in the VM (that is, in the DataContext ), you do this:

     <Window.InputBindings> <KeyBinding Key="S" Modifiers="Ctrl" Command="{Binding SaveCommand}" /> </Window.InputBindings> 

    If your team is defined somewhere else, you will need more complex code. In this case, it makes sense to attach to ApplicationCommands.Save :

     <Window.InputBindings> <KeyBinding Key="S" Modifiers="Ctrl" Command="Save" /> </Window.InputBindings> 

    Now you need to bind your save implementation to ApplicationCommands.Save . You define two methods:

     void SaveCanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; // ΠΈΠ»ΠΈ False, Π² зависимости ΠΎΡ‚ Π»ΠΎΠ³ΠΈΠΊΠΈ } void SaveExecute(object sender, ExecutedRoutedEventArgs e) { NodeSave(null, null); } 

    and so attached:

     var saveBinding = new CommandBinding(ApplicationCommands.Save, SaveExecute, SaveCanExecute); CommandManager.RegisterClassCommandBinding(typeof(System.Windows.Window), saveBinding); 

    Do not forget that saving is not a matter of a window or other View-components, bring it to the VM or even to the model. (You are using MVVM, right?)


    Do not forget that you can attach both a menu item and a button to the same command.

        bool leftctrl= false; bool s = false; private void MainForm_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.LeftCtrl ) { leftctrl= true; } else if (e.KeyCode == Keys.S) { s= true; } if (leftctrl && s) { MessaeBox.Show("ΠΎΠ΄Π½ΠΎΠ²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠ΅ Π½Π°ΠΆΠ°Ρ‚ΠΈΠ΅"); } } private void MainForm_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.LeftCtrl ) { leftctrl = false; } else if (e.KeyCode == Keys.S) { s = false; } }