Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key Combination event

Tags:

mvvm

wpf

As per requirement I need to capture the event of Key combination of two character and One control key(Example (ALT+S+C). How can I implement the same.

Thanks Ranish

like image 771
Ranish Avatar asked Feb 15 '23 20:02

Ranish


2 Answers

Using the KeyDown event:

if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt) // Is Alt key pressed
{
    if (Keyboard.IsKeyDown(Key.S) && Keyboard.IsKeyDown(Key.C))
    {
        // do something here
    }
}
like image 154
Sheridan Avatar answered Apr 02 '23 10:04

Sheridan


EDIT: Modified code. Using both Gesture and Key properties is not possible. The last declared property will be used as the key and the key specified in the Gesture property will be ignored.

The following code is only possible with 2 ModifierKeys, not 2 Keys:

<KeyBinding Gesture="Alt+Shift+C" Command="{Binding ACommand}"/>

To implement Key combinations with 2 Keys and a single ModifierKey the following article looks quite useful:

KeyGesture with Multiple Keys

like image 44
Richard E Avatar answered Apr 02 '23 09:04

Richard E