How to detect CTRL + ALT key combinations in WPF

No.of Views5907
Bookmarked0 times
Downloads 
Votes0
By  Prabu   On  03 Jul 2010 03:07:48
Tag : WPF , Miscellaneous
How to detect CTRL + ALT key combination in WPF
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

The 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.

Implementation

void 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.

 
Sign Up to vote for this article
 
About Author
 
Prabu
Occupation-Software Engineer
Company-
Member Type-Fresh
Location-India
Joined date-23 Jun 2010
Home Page-
Blog Page-http://prabu-guru.blogspot.com/
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</