Find out if any Button on any Gamepad has been pressed and which one


The title pretty much says it all. I want my players to be able to assign custom gamepad buttons for the controls. So imagine you click on “jump” and a message says “press the key for jump”, after which you press that key, the game remembers which one it was and you can use the new setting from then on.

To do that for a keyboard is easy, since I only have to call

public void OnGUI ()
		if (Event.current.type == EventType.KeyDown) {
			KeyCode code = Event.current.keyCode;

to find out what was pressed and it is very easy to work with that KeyCode.

For gamepads however there does not seem to be such an event. The only option I have found so far is to poll

if (Input.GetKeyDown ("joystick 1 button 0"))

for each button on each gamepad during each update(), which seems very bloated and certainly not right.

Does anyone know an easier way to catch any button that has been pressed?

for (int i = 0;i < 20; i++) {
if(Input.GetKeyDown("joystick 1 button "+i)){
print("joystick 1 button "+i);


I may help someone. I am using Logitech Wireless GamePad F710.

Here detection pressed button:

void Update ()    {
        if (Input.GetKeyDown(KeyCode.JoystickButton0)) {

Here description of buttons of this Gamepad:

JoystickButton0 - X
JoystickButton1 - A
JoystickButton2 - B
JoystickButton3 - Y
JoystickButton4  - LB
JoystickButton5  - RB
JoystickButton6  - LT
JoystickButton7  - RT
JoystickButton8 - back
JoystickButton9 - start
JoystickButton10 - left stick[not direction, button]
JoystickButton11 - right stick[not direction, button]

Actually there is an “anyKey” option, but you still have to iterate for every keyCode:

if (Input.anyKey)
		foreach(KeyCode kcode in Enum.GetValues(typeof(KeyCode)))
                             //do something with kcode

No there isnt an any button option, but if you need to find which one, you have to monitor them all anyway…

writing IF is actually the most efficient way of writing an multiplex for scanning IO values, it seems abit long but it’s very efficient, IF statements are less taxing than even a single + arithmetic to the processor. so it’s like every frame you are adding 20 plus arithmetics it’s pretty cool.

So after Extensive thinking, I believe I have found the answer to this long standing problem.
here’s the code— You need a string variable Called CONFIGBUTTONS2 for this instance to work.

var Numbers1 = new Array (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
for (var go1 : float in Numbers1) {
if(Input.GetKeyDown("joystick 1 button "+go1)){CONFIGBUTTONS2 = "joystick 1 button "+go1;}
if(Input.GetKeyDown("joystick 2 button "+go1)){CONFIGBUTTONS2 = "joystick 2 button "+go1;}
if(Input.GetKeyDown("joystick 3 button "+go1)){CONFIGBUTTONS2 = "joystick 3 button "+go1;}
if(Input.GetKeyDown("joystick 4 button "+go1)){CONFIGBUTTONS2 = "joystick 4 button "+go1;}
if(Input.GetKeyDown("joystick 5 button "+go1)){CONFIGBUTTONS2 = "joystick 5 button "+go1;}
if(Input.GetKeyDown("joystick 6 button "+go1)){CONFIGBUTTONS2 = "joystick 6 button "+go1;}
if(Input.GetKeyDown("joystick 7 button "+go1)){CONFIGBUTTONS2 = "joystick 7 button "+go1;}
if(Input.GetKeyDown("joystick 8 button "+go1)){CONFIGBUTTONS2 = "joystick 8 button "+go1;}
var Numbers2 = new Array ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
for (var go2 : String in Numbers2) {
if(Input.GetKeyDown(go2)){CONFIGBUTTONS2 = go2;}

So this script checks through all the buttons on 8 separate joysticks and returns it in the form of the String CONFIGBUTTONS2. So if you want to assign that to an input button, just assign the value of CONFIGBUTTONS2 to another variable like ABUTTON. then use that ABUTTON in…

//do something

I have yet to find a way to easily find all the Input Axis’s, but those aren’t nearly as complicated as the 150+ joystick buttons. Hope this Helps!

for(int butt =0; butt<10;butt++) { string code = "Joystick"+i+"Button"+butt; KeyCode butcode = (KeyCode)System.Enum.Parse(typeof(KeyCode), code); if(Input.GetKey(butcode)) { Debug.Log("Button pressed"+ "Joystick"+i+"Button"+butt); } }