amrelgarhy81
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.AddMessageFilt er
To understand them fast.
Just want a small help if you please,
I want a small hint to understand the :
1- IMessageFilter
2- Application.AddMessageFilt
To understand them fast.
Here is the help for IMessageFilter and Application.AddMessageFilt er:
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.Messa ge) As Boolean Implements System.Windows.Forms.IMess ageFilter. PreFilterM essage
Const WM_KEYDOWN = &H100
Select Case m.Msg
Case WM_KEYDOWN
RaiseEvent KeyDown(Me, New KeyEventArgs(m.WParam.ToIn t32))
End Select
Return True
End Function
Bob
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.Messa
Const WM_KEYDOWN = &H100
Select Case m.Msg
Case WM_KEYDOWN
RaiseEvent KeyDown(Me, New KeyEventArgs(m.WParam.ToIn
End Select
Return True
End Function
Bob
ASKER
Thanks alot friend.
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.AddMessageFilt
End Sub
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
Application.RemoveMessageF
End Sub
Private Sub m_keyFilter_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEv
If e.KeyData = Keys.Escape Then
Application.Exit()
End If
End Sub
Bob