# Programming the Right Mouse Click...

**URL:** <https://boards.straightdope.com/t/programming-the-right-mouse-click/31079>\
**Category:** Factual Questions\
**Created:** [August 28, 2000, 7:08pm UTC](https://boards.straightdope.com/t/programming-the-right-mouse-click/31079 "2000-08-28T19:08:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oblong](https://avatars.discourse-cdn.com/v4/letter/o/c0e974/32.png) [@Oblong](https://boards.straightdope.com/u/Oblong)\
**Post date:** [August 28, 2000, 7:08pm UTC](https://boards.straightdope.com/t/programming-the-right-mouse-click/31079/1 "2000-08-28T19:08:38Z")

</div>

Is there a way in Visual Basic to program a keystroke to simulate the right mouse click?

---

<div class="post-metadata">

**Author:** ![CommonMan](https://avatars.discourse-cdn.com/v4/letter/c/a698b9/32.png) [@CommonMan](https://boards.straightdope.com/u/CommonMan)\
**Post date:** [August 30, 2000, 5:24pm UTC](https://boards.straightdope.com/t/programming-the-right-mouse-click/31079/2 "2000-08-30T17:24:38Z")

</div>

Hi Oblong. Check out the keybd\_event API call. It can be used to simulate keystrokes. One of the Virtual Key Codes you can pass this function is VK\_RBUTTON (hex value=02). This is equivalent to a right mouse button click.

---

<div class="post-metadata">

**Author:** ![Starbury](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@Starbury](https://boards.straightdope.com/u/Starbury)\
**Post date:** [August 31, 2000, 4:50pm UTC](https://boards.straightdope.com/t/programming-the-right-mouse-click/31079/3 "2000-08-31T16:50:12Z")

</div>

Another way to do this is through the form (or control’s) keydown event. Its not as efficient, but at least you don’t have to make an API call.

Anyway, just set the form’s keypreview to True (assuming you want the rightclick to happen at any time). Then,  
in the Form (or control’s) keydown event, check the keycode of the button being pressed, and then raise the form’s mouse\_down event, passing it the value for the right mouse button.

Ex: (I’m using the ~ key to simulate a right button click…you’d also have to do a little extra work to figure out the x and y values of the mouse)

* * *

Private Sub Form\_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = 192 Then  
form\_mousedown(vbRightButton, false, x, y)  
End If

End Sub  
Private Sub Form\_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

If Button = vbRightButton Then  
form.popupmenu menuname  
'or whatever you wanted to happen  
End If

End Sub
