Skip shortcut for buttons

M

Matthew

C# Component Shortcut Key Event Focus

We have created our own checkbox component. By capturing the KeyDown and
KeyUp events the checkbox steps through a string containing a list of the
valid checkbox values, for instance: ABC etc. Also a blank space can be a
valid value.

Problem:
If there is a button on the form with the same shortcut key ('X') as one of
the valid values in our checkbox, the buttons OnClick event is captured
although our checkbox component has focus and also captures the keyboard
events. We have tried to set e.Handled = True in the KeyDown event, but this
does not correct the error.

Qestion:
How can we prevent other components on our form capturing keyboard events
when our own checkbox component has focus?

Thank you in advance... // Matthew
 
M

Matthew

Fergus Cooney said:
Hi Matthew,

There are two low-level hooks which you can grab onto: ProcessCmdKey()
and ProcessKeyPreview().

These are overridable methods of Forms and Controls which occur before
the KeyDown, Up and Press events.

Don't let the blurb fool you. It talks about the Message parameter but
for both functions you are definitely dealing with a key message not any old
message. Also, it says that ProcessCmdKey() is for handling command keys
(aka accelerators, aka shortcuts - ahah, I hear bells ringing). It's true,
ProcessCmdKey() does do that, but your override of ProcessCmdKey() can
prevent that!

I suggest that your Checkbox override its ProcessCmdKey() and if the
message (key) is to its taste, don't let anyone else get a sniff. Like so:

class WonderCheckbox
{
: : :
// All keys call this first on KeyDown (or SysKeyDown).
// Returning true will prevent any further KeyDown processing.
// KeyUp will proceed as normal at ProcessKeyPreview()
protected override bool ProcessCmdKey (ref Message oKeyMsg, Keys
oKeyData)
{
// Let's have a look at this little teaser.
DebugLine ("ProcessCmdKey (" + oKeyMsg.Msg.ToString("X") + ", " +
oKeyData.ToString() + ")");

if (oKeyMsg/oKeyData is for me)
{
Do mah thang;
return true; // This key is a gonna. It jes' ain't no mo'. No
siree. Uh uh.
}

// Let some other poor slob deal with it. :)
return base.ProcessCmdKey (ref oKeyMsg, oKeyData);
}
}

Hope this does the trick.

Regards,
Fergus

Thanks Fergus! Your tips really helpt us and works fine in our soloution!

// Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top