Libretro - programatically triggering key presses / button presses

Hi,
I’m using Libretro Unity wrapper from here: GitHub - Skurdt/SK.Libretro: Libretro wrapper written in C# with support for the Unity game engine.
It uses Unity’s Input Actions system, and that’s all fine - I have a game loading, and pressing Right Shift adds credits as expected.
However, I need to trigger this ‘add credits’ function / right shift programatically (as well as any others), and I have no idea how to do this.

I can’t seem to tap in to the functions through code. I’ve looked at simulating keypresses - dead end. There’s apparently no way to manually ‘trigger’ an input action either. I looked at creating a virtual keyboard device to register with input actions, dead end too.

KeyboardKey(0, retro_key.RETROK_RSHIFT) looks to be along the lines, although I have no idea if this is just getting the keyboard key (and I can’t even get this to work anywhere anyway).
This might help: https://github.com/Skurdt/SK.Libretro/blob/main/Scripts/Input/InputHandler.cs but I have no idea what it all does or how to tap into it.

Also can see retro_set_input_state_t and things around polling but think that’s just around capturing inputs. I don’t really need to mess with inputs, I just want to run the libretro functions that the inputs trigger. like right shift adds the credits for a MAME game, Enter starts the game etc.
My c# scripting experience isn’t great either!
Any help would be amazing, thank you.

Not that this works, but you can see what I’m aiming for:

using SK.Libretro.Unity;
using UnityEngine;
using System.Collections;
using SK.Libretro;
using SK.Libretro.Header;

public class LibretroDController : MonoBehaviour
{
    [SerializeField] private LibretroInstanceVariable _libretro;

    private IInputProcessor _inputProcessor; // complete guessing here 
    
    void Start()
    {
        _libretro.StartContent();
        StartCoroutine(RunAfterDelay(5f));
    }
    
    IEnumerator RunAfterDelay(float delay)
    {
        yield return new WaitForSeconds(delay);
        PressedReload();
    }

    public void PressedReload()
    {
        _inputProcessor.KeyboardKey(0, retro_key.RETROK_RSHIFT); // complete guessing, doesn't work at all
    }
}

I managed to find a solution to all this.
Turns out I was on right track with the virtual keyboard. I just needed to map the virtual keyboard over to the player input and port for libretro. I read that a virtual keyboard wouldn’t work on android due to no keyboard or something, but turns out it works just fine!

So if anyone arrives here looking for how to programatically trigger input actions in unity, you can, using a virtual device, then add those mappings in to your Input Actions.

Along lines of:
Create a virtual keyboard.
Then pair that with the user.
Enable the device.
ActivateInput.
Update InputSystem.
Then for libretro, set controller / port / device for libretro.
Might need to also ‘joinPlayer’ - not sure if this is a Unity or Libretro thing though.

Then, on the input actions, add mappings for the virtual keyboard device. It might not appear in the binding list, but you can just type it in by pressing the T next to the drop down, and entering along lines of…

<VirtualKeyboardDevice>/rightShift

Then when you ‘trigger’ a keypress in the the virtual keyboard code, it will trigger that input action :smiley:

Not sure if this is all the best way - I’d prefer to hook directly in to the Libretro functions, but it looks like libretro / retroarch handles the input in general by polling device states anyway - not sure if there’s even a way to ‘call a function’ in retroarch outside of using a ‘device’.

You can create virtual joypad, and mouse the same way too.