• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4964
  • Last Modified:

Capture Multiple key press in C#

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?
Thanks
0
P1ST0LPETE
Asked:
P1ST0LPETE
  • 4
  • 3
4 Solutions
 
pivarCommented:
Hi,

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.

/peter
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.

0
 
P1ST0LPETEAuthor Commented:
pivar:
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.
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
pivarCommented:
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.
0
 
P1ST0LPETEAuthor Commented:
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?
0
 
pivarCommented:
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.
0
 
P1ST0LPETEAuthor Commented:
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.
0
 
P1ST0LPETEAuthor Commented:
Thanks for all the help pivar.
Also thanks Idle Mind for the extra info.
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now