none
How to capture mouse events and prevent HTML elements from handling these events?

    Question

  • In my IE extension under certain conditions I would like to capture mouse events at the document level but prevent HTML elements from receiving these captured events. By default an HTML element receives a mouse event first and then the event bubbles up to the document level. So cancelling the event at the document level does not have any effect.

    Is there any other way to capture an event and prevent HTML elements from receiving it. I still need to know what element was supposed to receive the event. I would like to find a solution that works either through injected JavaScript or in extension.

    Thank you

    Vladimir

    Monday, October 24, 2011 8:08 PM

All replies

  • I think you can use IEventTarget::addEventListener API
    NEU_ShieldEdge
    Thursday, October 27, 2011 8:57 AM
  •  HI Vladimir,

     

    As far as I know, you can inject javascript to catch all the mouse event. In this javascript, you can block the onClick, onmousedown and other event, and then you can use IHTMLWindow2::execScript Method to inject this javascript in the SetSite function.

     

    I hope these information can help you to solve this problem.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, November 01, 2011 9:42 AM
    Moderator
  • No, you can't capture at the document level and prevent elements from receiving the event.  You'll have to handle the event for each element.
    • Marked as answer by VladLvov Friday, December 16, 2011 9:28 PM
    • Unmarked as answer by VladLvov Friday, December 16, 2011 9:40 PM
    Tuesday, November 01, 2011 5:34 PM
  • I understand that I need to handle the event at the element level. How can I prevent any other event listeners to fire after my own handling?

    Thank you.

    Friday, December 16, 2011 9:43 PM
  • >  How can I prevent any other event listeners to fire after my own handling?
     
     
    you can cancel bubbling of the event with a cancelBubble property
    below is an example.
      

    using System.Windows.Forms;
    
    namespace WindowsFormsApplication5
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                var html = @"
                    <html>
                        <head>
                            <script type='text/javascript'>
                                function test(block) {
                                    if(!block) return;
                                    event.returnValue = false;
                                    event.cancelBubble = true;
                                }
                            </script>
                        </head>
                        <body onclick='alert(event.srcElement.tagName)'>
                            <div style='border: 1px solid red; height: 50px;  width: 50px;' 
                                onclick='test(true)'>test1</div>
                            <div style='border: 1px solid navy; height: 50px;  width: 50px;' 
                                onclick='test(false)'>test2</div>
                        </body>
                    </html>
                ";
                new WebBrowser { Parent = this, Dock = DockStyle.Fill, DocumentText = html };
            }
        }
    }
    
       

     

    • Edited by Malobukv Saturday, December 17, 2011 7:58 AM
    Saturday, December 17, 2011 7:58 AM