Skip to content

Detecting Right Mouse Button Presses

In a desktop Canvas control you have two ways to handle a right mouse button press.

The most common situation is that you want to show a menu when the right mouse button is pressed. You can do this using the ConstructContextualMenu and ContextualMenuAction events.

For example, say you have a Canvas on a Window and want to display a menu when the user right-clicks in it. Add the CreateContextualMenu event to create your menu like this:

base.AddMenu( New MenuItem("Test 1"))
base.AddMenu( New MenuItem("Test 2"))
base.AddMenu( New MenuItem("Test 3"))

Then add the ContextualMenuAction event to check which menu was selected (provided by the hitItem parameter) and perform a corresponding action:

If hitItem <> Nil Then MessageBox(hitItem.Value)
Return True

Alternatively you may want to just know if the right mouse button was pressed so that it can perform another action rather than displaying a menu. For example, in the case of a Canvas maybe it changes a drawing color or marks a location. In situations like this you can also check for a right mouse button press in the MouseDown event handler by using the IsContextualClick method like this:

If IsContextualClick Then
  MessageBox("Right mouse button clicked.")
  MessageBox("Left mouse button clicked.")
End If