How to suppress KeyUp event?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

In certain situations during the execution of my application I want to
disable certain keys from performing their normal functionality. I am trying
to do this by setting e->SuppressKeyPress to true in the KeyDown event, but
this appears to only suppress the KeyPress event (as it name suggests) and
not the KeyDown event.

The same seems to happen with e->Handled - ie. if I set e->Handled to true
in the KeyDown event the KeyUp event still fires.

I want to stop the KeyUp event firing (I have to use the KeyUp event as the
KeyPress event does not allow non-character keys to be captured). Does anyone
know any way to do this? Am I just being incredibly stupid?

Cheers,

Robin
 
One way you can suppress the KeyUp event (and the KeyDown event) is to
override the ProcessKeyMessage method and return true for the cases
you want to suppress. Here is code that 'eats' a's.

public class MyTextBox : TextBox
{
protected override bool ProcessKeyMessage(ref Message m)
{
Keys keyCode = (Keys)((int)m.WParam) & Keys.KeyCode;
if (keyCode == Keys.A || keyCode == (Keys.A + 32))
{
return true;
}
return base.ProcessKeyMessage(ref m);
}
}

====================
Clay Burch
Syncfusion, Inc.
 
Thanks for your response. I have tried your suggestion (I had to convert it
from C# to C++ - but I think I did that correctly) but I can't seem to quite
get it to work. I know that my overriden function is being called - as I have
a message box set to display at the beginning of the function - but no keys
seem to get through - even though I am only excluding F6 at the moment.

I notice your example was using a textbox - and I'm using it on a form. Is
that likely to cause any problems? I'm assuming it should still be possible!

The other thing I'm having a slight problem with is the last line -
base.ProcessKeyMessage. In Visual C++ .Net it does not seem to be possible to
access the base class by using the keyword 'base'. From what I've read I have
to fully specify the function I want to call. I've done this by changing the
last line to return System::Windows::Forms::Form::ProcessKeyMessage(m). Is
that likely to be correct? One possible reason for my problems could be that
that function is always returning true (because I've got the wrong function
or something) and therefore everything is being 'eaten'.

Thanks a lot for your help,

Robin
 
Back
Top