C# Form Handeling Multi-key press

  • Thread starter Thread starter Kbalz
  • Start date Start date
K

Kbalz

I have a Form that is performing a wizard-like step system (using
TabControl), and I need a key combination (like ControlKey + A + T) to
be caught and show a secret admin TabPage.

I tried to add an event handler to the form to catch a key that I
press and change a label to the key I press with this code:


Code:
private void AdminKeyboardEvent(object sender, KeyEventArgs e)
{
lblDebug.Text = e.KeyCode.ToString();
}

However I read on some other sites that my TabControl was preventing
my Form from ever getting these key inputs.. so I had to add
"this.KeyPreview = true;" in my Form contructor..

Now my form is properly handeling a single key stroke, but how can I
get it to handel the user press 2 or 3 keys at the same time?

C#.NET 2.0 using VS2005.
 
I have a Form that is performing a wizard-like step system (using
TabControl), and I need a key combination (like ControlKey + A + T) to
be caught and show a secret admin TabPage.

I tried to add an event handler to the form to catch a key that I
press and change a label to the key I press with this code:

Code:
private void AdminKeyboardEvent(object sender, KeyEventArgs e)
{
lblDebug.Text = e.KeyCode.ToString();
}

However I read on some other sites that my TabControl was preventing
my Form from ever getting these key inputs.. so I had to add
"this.KeyPreview = true;" in my Form contructor..

Now my form is properly handeling a single key stroke, but how can I
get it to handel the user press 2 or 3 keys at the same time?

C#.NET 2.0 using VS2005.

This seems to work so far, Control + X

private void AdminKeyboardEvent(object sender, KeyEventArgs
e)
{
lblDebug.Text = e.KeyCode.ToString();
if (ModifierKeys.Equals(Keys.Control) &
e.KeyCode.Equals(Keys.X))
lblDebug.Text = "enter admin window";
}
 
Use KeyDown and KeyUp instead of KeyPress, or P/Invoke GetKeyboardState.
You may also want to check that other keys are NOT simultaneously pressed,
to keep someone from getting in by just mashing half the keyboard.
 
Use KeyDown and KeyUp instead of KeyPress, or P/Invoke GetKeyboardState.
You may also want to check that other keys are NOT simultaneously pressed,
to keep someone from getting in by just mashing half the keyboard.

Hopefully my software isn't that frustrating that users would do
that!! That is a great suggestion though, thanks!
 
Back
Top