By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,222 Members | 1,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,222 IT Pros & Developers. It's quick & easy.

traping all combinations of keypress in overriding ProcessCmdKey

P: n/a


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);
}
Apr 6 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Sun, 06 Apr 2008 09:40:16 -0700, <Petedwrote:
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.
Why not? What happens? What did you expect to happen instead?

Pete
Apr 6 '08 #2

P: n/a

>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.

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

Apr 7 '08 #3

P: n/a
Peted wrote:
>
Control and A, D, E, F, T, and C does not work
To capture Ctrl A separately, you would need
switch (keyData)
{
case (Keys.A | Keys.Control):

Keys combines the keys being pressed.


Apr 7 '08 #4

P: n/a
On Sun, 06 Apr 2008 18:39:37 -0700, <Petedwrote:
[...]
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
Apr 7 '08 #5

P: n/a

Great, thanks heaps dude

Peted

>On Sun, 06 Apr 2008 18:39:37 -0700, <Petedwrote:
>[...]
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
Apr 7 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.