KeyUp event problem on a form

Hello,

I have a form which contain some controls and user controls,
I want to exit the application when press the ESC key at anytime,
So i made the previewKey property for the form true,
But it dosn't work somtimes because of the usercontrols,

So what i need is:
A function that can feel and keypress on the form regardless the controls on it, it may be an API.

Need ideas and code please,



Thanks
LVL 1
amrelgarhy81Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Implement IMessageFilter:

Public Class WindowsKeyFilter
  Implements IMessageFilter

  Public Event KeyDown As KeyEventHandler

  Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage

    Const WM_KEYDOWN = &H100

    Select Case m.Msg
      Case WM_KEYDOWN
        RaiseEvent KeyDown(Me, New KeyEventArgs(m.WParam.ToInt32))

    End Select

  End Function

End Class
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
Example usage:

  Private WithEvents m_keyFilter As New WindowsKeyFilter

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Application.AddMessageFilter(m_keyFilter)
  End Sub

  Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
    Application.RemoveMessageFilter(m_keyFilter)
  End Sub

  Private Sub m_keyFilter_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles m_keyFilter.KeyDown
    If e.KeyData = Keys.Escape Then
      Application.Exit()
    End If
  End Sub

Bob
0
amrelgarhy81Author Commented:
Realy more than perfect.

Just want a small help if you please,
I want a small hint to understand the :
1- IMessageFilter
2- Application.AddMessageFilter


To understand them fast.
0
Bob LearnedCommented:
Here is the help for IMessageFilter and Application.AddMessageFilter:

http://msdn2.microsoft.com/en-US/library/system.windows.forms.imessagefilter(VS.80).aspx

A class that implements the IMessageFilter interface can be added to the application's message pump to filter out a message or perform other operations before the message is dispatched to a form or control. To add the message filter to an application's message pump, use the AddMessageFilter method in the Application class.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.addmessagefilter(VS.80).aspx

Use a message filter to prevent specific events from being raised or to perform special operations for an event before it is passed to an event handler. Message filters are unique to a specific thread.

To prevent a message from being dispatched, the value parameter instance that you pass to this method must override the PreFilterMessage method with the code to handle the message. The method must return false.


With that said, you need to return True, if you aren't filtering out messages:

Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage

    Const WM_KEYDOWN = &H100

    Select Case m.Msg
      Case WM_KEYDOWN
        RaiseEvent KeyDown(Me, New KeyEventArgs(m.WParam.ToInt32))

    End Select

    Return True

  End Function

Bob
0
amrelgarhy81Author Commented:
Thanks alot friend.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.