Disable a right click (press and hold) in WPF application.
Asked Answered
C

2

12

I am working on the touch screen application which is running on Windows XP Standard. With current hardware to invoke a right click user has to click and hold for couple of seconds, but this might interfere with other actions like holding a repeat button in the scrollviewer, so I have decide to disable a right click.

I would ideally wan't to disable a right click on the application level, but if it is not possible, disable right click on windows level would also work for me.

Counterpoison answered 11/5, 2011 at 9:34 Comment(0)
A
10

You can override the OnPreviewMouseRightButtonDown on the Window and set Handled to true. You also need to handle OnPreviewMouseRightButtonUp (thanks to Vitalij for pointing this out)

That should do the trick.

Anzovin answered 11/5, 2011 at 10:45 Comment(3)
You mean to add OnPreviewMouseRightButtonDownto the UI elements?Counterpoison
If you handle OnPreviewMouseRightButtonDown on the Window, you will be handling it before anyone else... you don't need to stay handling it for every controlAnzovin
Right! Forgot that window derives from UIElement, hence has all of the mouse events. The only thing is that you have to mark both OnPreviewMouseRightButtonDown and OnPreviewMouseRightButtonUp as handled.Counterpoison
A
27

The OnPreviewMouseRightButtonDown/Up approach did not work for me.

  1. There is a property Stylus.IsPressAndHoldEnabled on UIElement however. Set that to false to get rid of the press and hold right click behavior. I tested this on Windows 7 with .NET 4.0, but the property is available from .NET 3.0.

    <RepeatButton Stylus.IsPressAndHoldEnabled="false" ... />

  2. There is also a blogpost here that provides a code sample for a similar disabling of press and hold at window level. But with this in place, the PreviewTouchDown and TouchDown events will not be raised as soon as the finger touches the screen (which would be necessary for a RepeatButton I guess), only later. Read the 'Remarks' on this msdn page.

Accuse answered 2/8, 2011 at 12:6 Comment(3)
I confirm that the Stylus.IsPressAndHoldEnabled solution works also in Windows 8 :-) thanks !Pitiable
I know this is older, but I also confirm that the Stylus.IsPressAndHoldEnabled solution works for Windows 10.Vocalism
Thanks. I needed to use this to be able to remove the "press and hold effect rectangle". Being new to using a touch screen with WPF, this was tricky to find the answer.Ferne
A
10

You can override the OnPreviewMouseRightButtonDown on the Window and set Handled to true. You also need to handle OnPreviewMouseRightButtonUp (thanks to Vitalij for pointing this out)

That should do the trick.

Anzovin answered 11/5, 2011 at 10:45 Comment(3)
You mean to add OnPreviewMouseRightButtonDownto the UI elements?Counterpoison
If you handle OnPreviewMouseRightButtonDown on the Window, you will be handling it before anyone else... you don't need to stay handling it for every controlAnzovin
Right! Forgot that window derives from UIElement, hence has all of the mouse events. The only thing is that you have to mark both OnPreviewMouseRightButtonDown and OnPreviewMouseRightButtonUp as handled.Counterpoison

© 2022 - 2024 — McMap. All rights reserved.