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

Posted on 2010-11-14
Medium Priority
Last Modified: 2013-12-17
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)?
Question by:endrec

Accepted Solution

getnitincr earned 2000 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))
LVL 16

Expert Comment

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


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
An ASP.NET Web Form User Control is not newly introduced in ASP.NET. In fact, it was an old technology yet still playing a role to generate web content, especially when we want to use it to have a better and easy way to control part of the web conte…
Watch the video of Kernel Migrator for SharePoint, which demonstrate the process easily of migration from SharePoint to SharePoint, OneDrive for Business & Google Drive servers, Public Folder to SharePoint, File Server to SharePoint. The tool has va…
If you are looking for an automated tool which can generate reports for Outlook emails and other items from PST file, then you can go for Kernel PST Reporter tool. The reports which are created by this tool are helpful to analyze and understand PST …

587 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