?
Solved

KeyUp event problem on a form

Posted on 2006-04-10
5
Medium Priority
?
494 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:amrelgarhy81
  • 3
  • 2
5 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 16417798
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16417801
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
 
LVL 1

Author Comment

by:amrelgarhy81
ID: 16418302
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16418337
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
 
LVL 1

Author Comment

by:amrelgarhy81
ID: 16418889
Thanks alot friend.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question