- Home /
Send UI Button Event with key press NOT onClick()???
Hi, I've got a simple start menu with "Start" and "quit" buttons. I've already set up navigation between them with Horizontal axis using the EventSystem, but I have no idea how to get either of my buttons to call a method in my ManagerScript using Enter or Space keys. I know how to do this using the "OnClick()" on the button component, but I would like to have no mouse, only keyboard inputs. There are plenty of games that use gamepad or keyboard to send events via UI, so I feel like it's probably really simple. So how would I go about that tho?
Thanks!!!
$$anonymous$$ind posting some of your code? The code with the Button commands would be what I will need
Answer by flashframe · Mar 18, 2016 at 09:31 PM
In the Standalone Input Module component of the Event System game object in your scene, you can specify which keyboard/joystick inputs to use for Submit and Cancel on your UI. (You set these buttons up in Edit/ProjectSettings/Input)
Then on your button, add an Event Trigger for Submit
hey thanks for this. I had no idea that you could add an event type to a button. this is what i was looking for.
You sir are a coding god!
This UI challenge was bugging me for a couple years. I had left it on the backburner until this week, which I was deter$$anonymous$$ed to figure out how to get a simple Unity Grid Layout Group to respond to keyboard input. After banging my head reading so many useless threads with long nonsensical , convoluted solutions, I found this short and simple answer.
This was the answer I was looking for. Thank you!
Sharin' is carin'
@indieDoroid
So, is it possible/safe to have like A and B buttons mapped so Submit? In the Standalon Input $$anonymous$$odule I see option to only add one button, may be is it possible to add more like Button1;Button2? or in the Edit/ProjectSettings/Input Should we have two "Submit" named inputs and in the Standalon Input module Submit Button have "Submit" assinged?. That is correct way to do the things?
Answer by Dudicus · Mar 18, 2016 at 09:29 PM
Use:
void Update () {
if (Input.GetKeyDown("Enter"))
{
Application.Quit();
}
}
This would be for the quit button. For the Start button replace Application.Quit(); with whatever variable or method you are using to start the game.
i'm not sure this would work. As long as the quit button is enabled, escape would cause the game to quit, regardless of the button highlighted. I'm trying to use one button input or a "submit" button.