How to 'click' a UI Button with a keypress using new InputSystem?

I’ve adopted the new InputSystem.

It works well for things like player control, where I add a Player Input component to my player game object and I route the events to the player control script.

It also works well for the canvas, where I have a Button and the EventSystem’s Input System UI Input Module script takes care of mouse move/hover/click.

But how can I assign a keypress to a specific Button using this new InputSystem approach? And is there a way to do it without hardcoding things in a script?

In my case, I have a ‘Play’ button on the start screen. I want it to work when someone clicks it with a mouse or presses P (which I might also extend in future to a gamepad too).

Short version:

  • create a “menu” (or whatever you want to call it) action map

  • create actions: up (wKey + gamepad up), down (sKey + gamepad down), select (enterKey + southButton), playbutton (pKey + maybe gamepad start?), etc…

  • on the UI you develop the hover stuff with menu point IDs

  • create a menu script and embed the input system into it

  • disable other action maps and activate the menu action map when the menu is enabled

  • create methods for
    – SelectPreviousMenupoint
    – SelectNextMenupoint
    – SelectMenupoint (for hovering)

in these you build a logic to select a menupoint in the UI, highlight the active one and whatnot

– ActivateMenupoint

in this method you decide what action needed to take when the “active” menu point is selected

– ActivatePlayMenupoint

add this method to the playbutton action and also call it when the ActivateMenupoint runs and the active menu is the play…

you can do this for many menu points