Solved

How to capture multiple key presses (including modifier keys) to record, e.g. user pressed Ctrl+E, in C#?

Posted on 2010-11-14
2
1,105 Views
Last Modified: 2013-12-17
Hi,
I wanted to know if there is a way to capture multiple keypress or keydown events including modifier keys.  For example:  How would I be able to tell that a user pressed multiple keys at the same time, using keypress or keydown events, for an application that would allow them to set commands/shortcuts (e.g. user pressed Ctrl+Shift+E)?
0
Comment
Question by:endrec
2 Comments
 
LVL 2

Accepted Solution

by:
getnitincr earned 500 total points
ID: 34134086
protected override void OnPreviewKeyDown(KeyEventArgs e)
    {
       if ((e.Key == Key.V || e.Key == Key.X || e.Key == Key.C)
          && Keyboard.IsKeyDown(Key.LeftCtrl))
        return;
0
 
LVL 16

Expert Comment

by:Stephan
ID: 34134090
Try this out:


[DllImport ("user32.dll")]
public static extern int GetKeyboardState( byte[] keystate );


private void Form1_KeyDown( object sender, KeyEventArgs e )
{
   byte[] keys = new byte[255];

   GetKeyboardState (keys);

   if( keys[(int)Keys.Up] == 129 && keys[(int)Keys.Right] == 129 )
   {
       Console.WriteLine ("Up Arrow key and Right Arrow key down.");
   }
}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
.Net Web Site IIS Web.Config Content-Security-Policy 1 44
Connection String 16 43
Variable Event ? 3 23
use of unassigned local variable 'id' 6 27
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now