Programming the Right Mouse Click...

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

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.

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