Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2006

Tip: Looking for answers? Try searching our database.

How to detect complex keypress (STRG+N, P)?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Roland Müller - 27 Jul 2006 13:57 GMT
Hello,

i know how to get a simple keyboard combination, but how to detect a
combination of keyboard combination, pressing STRG+N and P?
This does not work:

private void MdiParentForm_KeyDown(object sender, KeyEventArgs e) {
    Debug.WriteLine(e.KeyData);

         switch(e.KeyData) {
             case Keys.Control|Keys.N|Keys.P:
               MessageBox.Show("test");
               break;
    }
}

Any help?
Thanks in advance, Roland!
Mini-Tools Timm - 27 Jul 2006 14:30 GMT
Hi Roland,

You cannot check for multiple keypresses with a single operation.  In the
Keys enumeration, letter keys do not have "Flag" values that can be OR'd
together, such as Keys.N | Keys.P.  In this case, Keys.N = 78 and Keys.P =
80.  Only the modifier keys such as Keys.Shift can be OR'd with letter keys,
such as Keys.Shift | Keys.N.

Instead, to detect a complex key sequence, you need to save the state of the
keypress then check that state on the next keypress.  For example:

private Keys m_PreviousKey;
private void MdiParentForm_KeyDown(object sender, KeyEventArgs e)
{
    switch(e.KeyData)
    {
        case Keys.Control|Keys.N:
            Console.WriteLine( "First key" );
            break;
        case Keys.P:
            if (this.m_PreviousKey == Keys.Control|Keys.N)
                Console.WriteLine( "GOT IT!" );
            break;
    }
    this.m_PreviousKey = e.KeyData;
}

> Hello,
>
[quoted text clipped - 11 lines]
>     }
> }

Signature

Timm Martin
Mini-Tools
.NET Components and Windows Software
http://www.mini-tools.com

Roland Müller - 27 Jul 2006 14:56 GMT
Thanks, that is easy and logical.

Mini-Tools Timm schrieb:
> Hi Roland,
>
[quoted text clipped - 38 lines]
>>     }
>> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.