Link to home
Start Free TrialLog in
Avatar of amrelgarhy81
amrelgarhy81Flag for Egypt

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of amrelgarhy81

ASKER

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.
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
Thanks alot friend.