Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2007

Tip: Looking for answers? Try searching our database.

How to suppress KeyUp event?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
robintw - 15 Jun 2007 15:46 GMT
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
ClayB - 16 Jun 2007 20:27 GMT
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.
robintw - 18 Jun 2007 10:01 GMT
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

> One way you can suppress the KeyUp event (and the KeyDown event) is to
> override the ProcessKeyMessage method and return true for the cases
[quoted text clipped - 16 lines]
> Clay Burch
> Syncfusion, Inc.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.