IntroductionThe Key combination are a fancy pair of words to describe pressing/holding multiple keyboard buttons to perform a command. Example :- Few of ever using key combination are, - Ctrl + S to Save,
- Ctrl + C to Copy,
- Alt + F4 to close an application,
- Ctrl + Alt + Del to lock our computer, etc.
There are many such combination, and while I provided some common ones, many applications like Visual Studio provide their own key combination to help save you some time. Let's add some key combination to our little program also. Implementationvoid SpecialKeyHandler(object sender, KeyEventArgs e)
{// Ctrl + Nif ((Keyboard.Modifiers == ModifierKeys.Control) && (e.Key == Key.N))
{
MessageBox.Show("New");
}// Ctrl + Oif ((Keyboard.Modifiers == ModifierKeys.Control) && (e.Key == Key.O))
{
MessageBox.Show("Open");
}// Ctrl + Sif ((Keyboard.Modifiers == ModifierKeys.Control) && (e.Key == Key.S))
{
MessageBox.Show("Save");
}// Ctrl + Alt + Iif ((Keyboard.Modifiers == (ModifierKeys.Alt | ModifierKeys.Control)) && (e.Key == Key.I))
{
MessageBox.Show("Ctrl + Alt + I");
}
}The key approach doesn't work for another reason because your commonly used Alt, Ctrl, Shift, and Windows keys can't be accessed from the Key enum at all. Instead, those four keys can only be accessed using the ModifierKeys enum and checking whether Keyboard.Modifiers is equal to that key. That's all,now you able to handle key event in Windows Presentation application.thank you reading. |