traping all combinations of keypress in overriding ProcessCmdKey

  • Thread starter Thread starter Peted
  • Start date Start date
P

Peted

Hello,

i am lookinf for the best way to trap any alphanumeric keypress in all
multi key combminations and execute some code

For example , i have a form visible using the form.showdialog method,
then the user can press keys A, D, E, F, T, and C to peform a
function.
What i want is that if a user presses A, or a, or SHIFT A or CNTRL A
that it executes the same code, BUT NOT IF ALT A is pressed. The same
applies for the other letters.

I am overriding the processcmdkey function using the code bellow, and
it works, but not for ALL the multi key combo's i present above.

Can anyone help me on how to trap all the key combos i want listed
above

thanks for any help

Peted

#region ProcessCmdKey
protected override bool ProcessCmdKey(ref Message msg, Keys
keyData)
{
const int WM_KEYDOWN = 0x100;
const int WM_SYSKEYDOWN = 0x104;

if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
switch (keyData)
{
case Keys.Tab:
//Do something
return true;
break;
case Keys.F3:
//Do something
return true;
break;

case Keys.A:
//Do something
return true;


case Keys.D:
//Do something
return true;

case Keys.E:
//Do something
return true;

case Keys.F:
//Do something
return true;

case Keys.T:
//Do something
return true;


case Keys.C:
//Do something
return true;

}
}
return base.ProcessCmdKey(ref msg, keyData);
}
 
Why not? What happens? What did you expect to happen instead?


Pressing A, D, E, F, T, and C keys on thier own works fine

Capslock and A, D, E, F, T, and C works fine

Shift and A, D, E, F, T, and C does not work

Control and A, D, E, F, T, and C does not work

Alt and A, D, E, F, T, and C does not work
(I have now decided i do want the alt key combo to also work)

So any idea how i can get mod the code to make it pick up all those
modifier and key press combo in addition to just pressing the relevant
letter ?

I want all those combos to execute the same code as the single
keypress

Any advice appreciated

thanks

Peted
 
Great, thanks heaps dude

Peted

[...]
So any idea how i can get mod the code to make it pick up all those
modifier and key press combo in addition to just pressing the relevant
letter ?

Instead of "switch (keyData)" use "switch (keyData & Keys.KeyCode)". That
will filter out the modifier keys, leaving only the actual key pressed.

If you decide later that you want some modifier keys but not others, there
are a variety of ways you can check for that, but they all involve similar
techniques. That is, using the various mask constants in the Keys
enumeration to check specifically for modifiers.

Pete
 
Back
Top