?
Solved

IMessageFilter KeyUp Interruption

Posted on 2011-10-20
3
Medium Priority
?
737 Views
Last Modified: 2012-05-12
I'm using the class located below to recognize when a user presses Ctrl + B to open a Report a Bug form. The issue with this is that now the KeyUp events on the other forms and controls are being ignored. Is it possible to keep the IMessageFilter while still allowing the forms to retain their KeyUp events?

Thanks

public class MessageFilter : IMessageFilter
    {
        const int WM_KEYUP = 0x101;

        public bool PreFilterMessage(ref Message m)
        {
            bool ret = true;
            if (m.Msg == WM_KEYUP)
            {
                Keys key = (Keys)(int)m.WParam & Keys.KeyCode;
                if (key == Keys.B)
                {
                    if ((int)Control.ModifierKeys == ((int)Keys.Control))
                    {
                        AppSupport frmAppSupport = new AppSupport();
                        frmAppSupport.IsBugOnly = true;
                        frmAppSupport.ShowDialog();
                    }
                    else
                    {
                        ret = false;
                    }
                }
                return ret;
            }
            else
            {
                return false;
            }
        }
    }

Open in new window

0
Comment
Question by:dvargas531
  • 3
3 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37000706
See: http://msdn.microsoft.com/en-us/library/system.windows.forms.imessagefilter.prefiltermessage.aspx

    "true to filter the message and stop it from being dispatched"

So you have it backwards.  You want to return true when Ctrl-B is pressed and you open up the report.  Otherwise you should return false so that the event gets propagated correctly thru your application:
public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg == WM_KEYUP)
            {
                Keys key = (Keys)(int)m.WParam & Keys.KeyCode;
                if (key == Keys.B)
                {
                    if ((int)Control.ModifierKeys == ((int)Keys.Control))
                    {
                        AppSupport frmAppSupport = new AppSupport();
                        frmAppSupport.IsBugOnly = true;
                        frmAppSupport.ShowDialog();
                        return true; // suppress the keystroke because we opened the form
                    }
                }
            }
            return false; // allow normal process of the keystroke
        }

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37000726
...and it's probably not a good idea to use ShowDialog() inside PreFilterMessage.   =\

Instead, make your MessageFilter() class raise a custom event that your main form subscribes to.  Then, when that event is received, open up the report from there.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 37000885
That could look something like:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        private MessageFilter mf;

        public Form1()
        {
            InitializeComponent();

            mf = new MessageFilter();
            mf.OpenBugReport += new MessageFilter.CtrlB(mf_OpenBugReport);
            Application.AddMessageFilter(mf);
        }

        private void mf_OpenBugReport()
        {
            AppSupport frmAppSupport = new AppSupport();
            frmAppSupport.IsBugOnly = true;
            frmAppSupport.ShowDialog();
        }

    }

    public class MessageFilter : IMessageFilter
    {

        public delegate void CtrlB();
        public event CtrlB OpenBugReport;

        const int WM_KEYUP = 0x101;

        public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg == WM_KEYUP)
            {
                Keys key = (Keys)(int)m.WParam & Keys.KeyCode;
                if (key == Keys.B)
                {
                    if ((int)Control.ModifierKeys == ((int)Keys.Control))
                    {
                        if (OpenBugReport != null)
                        {
                            OpenBugReport();
                        }
                        return true; // suppress the keystroke because we opened the form
                    }
                }
            }
            return false; // allow normal process of the keystroke
        }
    }

}

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

840 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