Adding right click to Silverlight

In an earlier post, I created a small extension library that added several mouse gestures to Silverlight. However, the right-click only worked in Internet Explorer, and the solution was a bit of a hack. So to continue down the road of hacks, I found that there is a way to prevent the Silverlight Plugin from getting the right click event. By inserting a <div> on top of the plugin when the right mousebutton down event fires, you can prevent the mozilla browsers from getting the rightclick event. The mouseup event is then fired on this overlay div, and we remove the overlay again when the button is released or the mouse moves. Ugly but it works... The approach still requires the plugin to run in windowless mode.

Of course whether you even should be preventing the user from getting to the silverlight context menu in the first place is a whole different discussion.

I've updated the demo page, source and binary:

Try a demo!

Download binary (7 kb)

Download source (25 kb)

Comments (9) -

  • Hi,

    Helpful stuff. Chrome, though, has odd behavior with right click. Sometimes it does the menu correctly, sometimes just the Silverlight context, and then sometimes the chrome context?

    Other events work fine.
  • I did say it was a hack, and it only added support for Mozilla browsers, and not webkit. Personally I don't think you should be adding right-click functionality to your Silverlight app. Partly because it's not bulletproof, and partly because the context menu already serves a different purpose that we shouldn't prevent the user from getting to.
    The remaining click methods in this library are there for this exact purpose: to provide alternative gestures to get a context menu.
  • ali
          excellent artical but when i right click on the silverlight window and then click on other area then right click window does not remove ..... stay their  how to do this

  • public static class Drag

           private static void RaiseDragged(MouseButtonEventArgs e)
                Application.Current.RootVisual.MouseMove -= RootVisual_MouseMove;
                Application.Current.RootVisual.MouseLeftButtonUp -= RootVisual_MouseLeftButtonUp;
                if (currentElement == null) return;

                if (e.GetPosition(currentElement) == start.GetPosition(currentElement)) return;            //add by dazhou.

                IList<EventHandler<MouseDragEventArgs>> handlers = currentElement.GetValue(DragHandlersProperty) as IList<EventHandler<MouseDragEventArgs>>;
                if (handlers != null)
                    MouseDragEventArgs args = new MouseDragEventArgs(start, e, null);
                    foreach (EventHandler<MouseDragEventArgs> handler in handlers)
                        handler(currentElement, args);
                currentElement = null;

  • VFC
    I suppose that position fixed is better than absolute for the glass layer. Moreover if the SL context menu is open the mouse enter event is not triggered and the glass layer isn't placed on top of the SL application (right click on the "wheel" panel, SL context menu is shown, move the mouse on the "right click" panel and right click again, the SL context menu is shown again).
  • Ali: That's expected behavior. Right-click is only detected in the right-click area. The other areas use different gestures.
  • This is not work for MacOS?
  • Previously I had not noticed that it only works in Internet Explorer, but now I want to correct the error.

Pingbacks and trackbacks (1)+

Comments are closed