Manage key commands from c # and WPF

1

Hello, I am trying to avoid that the user can copy, paste, cut and print from my application with WPF.

What I found in the forum all used in the System.Windows.Forms.KeyEventArgs method however I have to use System.Windows.Input.KeyEventArgs because I'm in the UIElement class then the KeyDown method asks for the input then all the code I've seen does not it works since this library does not have e.Control .

The problem is that I believe that this liberia only takes a key not both of them and this does not work either:

 (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.P)
    
asked by jooooooooota 11.05.2017 в 12:37
source

2 answers

1

Taking for example that you are using a Window object:

You declare your Commands in Resources of the window (or the object you use):

<Window.Resources>
    <RoutedUICommand x:Key="AccionAComando" Text="Se ejecuta cuando oprimes A"/>
    <RoutedUICommand x:Key="AccionBComando" Text="Se ejecuta cuando oprimes B"/>
</Window.Resources>

Then you assign them the actions (in Executed ) and the conditions when they can be executed (in CanExecute ):

<Window.CommandBindings>
    <CommandBinding Command="{StaticResource AccionAComando}" Executed="AccionAComando_Execucion" CanExecute="AccionAComando_Validar"/>
    <CommandBinding Command="{StaticResource AccionBComando}" Executed="AccionAComando_Execucion" CanExecute="AccionAComando_Validar"/>
</Window.CommandBindings>

Now we only have to assign the commands to KeyBindings :

<Window.InputBindings>
    <KeyBinding Key="A" Command="{StaticResource AccionAComando}"/>
    <KeyBinding Key="B" Command="{StaticResource AccionBComando}"/>
</Window.InputBindings>

P.D .: The function Executed is where you will add all the logic of the event.
And in CanExecuted the conditions to only execute it when it should be done. For this you just have to change e.CanExecute to true or false (as the case may be).

    
answered by 09.11.2017 в 03:16
0

To detect a key combination using the event KeyDown of a WPF window for example, the only thing to do is a bitwise and of the property Keyboard.Modifiers with the control key that we want to check. The following example captures the combination CTRL + C :

private void Window_KeyDown(object sender, KeyEventArgs e)
{
    if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
    {
        switch (e.Key)
        {
            case Key.C:
                //Ctrl+C
                break;
            case Key.V:
                //Ctrl+V
                break;
            case Key.P:
                //Ctrl+P
                break;
        }
    }
}

Anyway, I recommend that you follow the programming pattern introduced by WPF: MVVM and use commands and other advanced methods of linking keyboard events with functions of your viewmodel. At the beginning it's a little hard, but it's worth it and that kind of programming patterns seem to be the future.

    
answered by 11.05.2017 в 13:26