We help IT Professionals succeed at work.

Capture Multiple key press in C#

P1ST0LPETE asked
Medium Priority
Last Modified: 2013-12-17
Hi all,
I have a "secret" windows form that I want to load when multiple keys are pressed at the same time.  How can I do this is C#, starting from my main form?
Watch Question


If it's multiple character keys (not Shift, Ctrl, Alt) you could use the main forms KeyDown and KeyUp events to record the keys you're pressing.

    string keys = "";
    private void Form1_KeyDown(object sender, KeyEventArgs e) {
      if (keys.IndexOf((char)e.KeyValue) == -1) keys += (char)e.KeyValue;
      if(keys == yourkeycombination) showsecretform();


    private void Form1_KeyUp(object sender, KeyEventArgs e) {
      int p = keys.IndexOf((char)e.KeyValue);
      if (p > -1) keys = keys.Remove(p, 1);

Remeber to set main forms KeyPreview to True.


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
If you want the key combination to work when your form is NOT in focus then you need a "hotkey"...

...achieved thru either:
(1) Timer polling the keyboard state with the GetAsyncKey() API.
(2) Registering a HotKey with the system via the RegisterHotKey()  API (only ONE app can receive a unique combination notification).
(3) Intercepting ALL keystrokes with a low level keyboard hook via WH_KEYBOARD_LL and the SetWindowsHookEx() API.


Your solution looks similar to what I was trying to work with already when I got stuck.  I have another question for clarification though. In the IF statement "if(keys == yourkeycombination)", what exactly do I put in for "yourkeycombination"?  The key combination I'm going for is: SHIFT + 5 letter keys.
It's just the string of keys in uppercase, if you like to test for shift, use \x0010:
if (keys == "\x0010ZX")

Of course you must press the buttons in this exact sequence, if you demand more flexibility you'll need to change to some logic where you search for chars in every position.
Also I noticed that depending on combination, you can press a different number of keys. I think I remember something of partitioned keyboards, where keys are grouped, but I might be totally wrong here. Maybe some hardware guy could answer that.


I'm fine with pressing the keys in the correct order, however I'm not getting it to work.  The "S" key, which is one of the 5 letters is not working.  If I replace the "S" with another letter, then it works, but with the S in it, it won't fire.  Obviously the "S" on my keyboard is working or I wouldn't be able to type them out in this sentence.  I even tried putting a conditional breakpoint on the methed to try and catch the S, and it's not working either.  What's the deal with the "S" key?  Does it hold a special hex falue similar to the shift key?
What combination are you trying to catch? I noticed that with certain combination you couldn't get five characters. Try S in the beginning of the sequence.


Well I was using the name of the company I work for which ends in an S, but I fine with not using it.  I'll just play around with it until I find a combo that I like and the system accepts.  I've got a good understanding of how it woks though now, so I should be good, thanks for the help.


Thanks for all the help pivar.
Also thanks Idle Mind for the extra info.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.