A.3 ‐ Right‐clic Plugin - miel-uqac/XR-Godot GitHub Wiki

Adding right-click in Godot VR

Right-clicking in Godot is extremely useful, in particular for:

  • Copy/Pasting text
  • Deleting nodes
  • Accessing contextual menus
  • Quick saving files

However, after testing all the VR controls, none corresponds to a right-click, which is a pity.

List of detected VR controls

There are two ways to see how Godot interprets VR controls :

1. Controls edition

Dans Projet → Paramètres du projet → Contrôles, on peut créer des événements personnalisés. In Project → Project settings → Controls, we can create custom events. With the “Detect input” option, we can see what Godot detects from the VR controllers.

image

Test results :

  • Left/Right/Up/Down joysticks → Bound to Left/Right/Up/Down scroll wheel
  • A/X buttons / High triggers / Finger pinch → Bound to Left mouse button
  • B/Y → Bound to KEY_BACK (not easily found on a keyboard)
  • Low triggers → Not detected

2. Using print(event)

Creating a plugin with a _input() function allows us to see entering events :

func _input(event):
    print(event)

However, in a VR environment, moving the hands triggers at all times InputEventMouseMotion events, which overloads the console.

To filter it:

func _input(event):
    if not event is InputEventMouseMotion:
        print(event)

image

We notice that :

  • A simple click triggers:
    • InputEventMouseButton
    • InputEventScreenTouch
    • Then an InputEventScreenDrag
  • The double click works
  • It is hard to do a long click without moving

Binding KEY_BACK to a right-click

As the Back key (B/Y) does not have a specific use in the editor, we can use it to emulate a right-click.

This is a plugin example :

@tool
extends EditorPlugin

func _input(event):
    if event is InputEventKey and event.keycode == KEY_BACK:
        var mouse_pos = get_viewport().get_mouse_position()

        var right_click_event := InputEventMouseButton.new()
        right_click_event.position = mouse_pos
        right_click_event.global_position = mouse_pos
        right_click_event.button_index = MOUSE_BUTTON_RIGHT
        right_click_event.pressed = event.pressed
        right_click_event.double_click = false
        right_click_event.button_mask = MOUSE_BUTTON_MASK_RIGHT

        get_viewport().push_input(right_click_event)

Result

Right-clicking is now possible in VR using the B/Y buttons on the VR controllers :

image