• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker [Solved + Code] How to check what button is being pressed on the gamepad?

PlayerOne

Member
Trying to work on key rebinds for both keyboard and controller. While I have the keyboard portion completed I'm a bit confused on bow to determine what button is being pressed on the controller.

Basically is there function similar to keyboard_key for gamepad input?
 

obscene

Member
On a gamepad you have to check each button specifically to see if it has been pressed or not, so for a rebind you'll have to loop through all the buttons and look for one being pressed.
 

PlayerOne

Member
On a gamepad you have to check each button specifically to see if it has been pressed or not, so for a rebind you'll have to loop through all the buttons and look for one being pressed.
I get that much and implemented it. However, I looked through the manual and all functions use a 0 to 1 input rather than a button id. Still looking around for the solution but no such luck.
 

PlayerOne

Member
You're looking for the constants listed at GamePad Input, I imagine?
I'm trying to make a controller profile using a ds_grid and having it saved in a *.json format. The player can load or save the inputs they remapped to the controller when the game boots up. Now I have everything set but the key to it is the button id's.

Keyboard inputs have id's (A=65, T=84, 7=55, etc - Not sure what the word for it is but I call them id's).

This is what I have written:

Code:
if (key_change==true && alarm[1]>-1)
{
   if (alarm[2]=-1)
   {
       for (var i=0; i<16; i++)
       {
           if (gamepad_button_check_pressed(0,i)) // <<< Press to change
           {
           global.controls[# 0, cursor_id]=               // <<< The button id needed to replace the current button in the grid. 
           key_change=false;
           alarm[1]=-1;
           loop=true;
           }
       
       }
       
   }
}
else
{
key_change=false;   
}
 

NightFrost

Member
You have to use contants Chamaeleon linked to when checking buttons, and they will return a boolean (0 or 1) depending on button state. However those constants can be saved into variables too (they're just integers AFAIK). So you must first check if a certain button is pressed using the scheme
Code:
if(gamepad_button_check_pressed(current_device, gp_face1); // Button (A) on X-Bone, (X) on PS.
And you can save specific buttons to your DS
Code:
global.controls[# 0, cursor_id] = gp_face1;
And later check if given button is pressed
Code:
if(gamepad_button_check_pressed(current_device, global.controls[# 0, cursor_id])){
    // gp_face1 is pressed.
}
I don't know what the values of those constants are, but it is best to assume they may overlap with keyboard button constants and not mix them. So when you are checking keyboard bindings, do not check them against gamepad constants, and vice versa. Meaning, continuing the above example, as global.controls[# 0, cursor_id] now contains a reference to gamepad button gp_face1, you should NOT check it with keyboard_check(). It might give a false positive, it might not. Best to not test your luck.
 

PlayerOne

Member
You have to use contants Chamaeleon linked to when checking buttons, and they will return a boolean (0 or 1) depending on button state. However those constants can be saved into variables too (they're just integers AFAIK). So you must first check if a certain button is pressed using the scheme
Code:
if(gamepad_button_check_pressed(current_device, gp_face1); // Button (A) on X-Bone, (X) on PS.
And you can save specific buttons to your DS
Code:
global.controls[# 0, cursor_id] = gp_face1;
And later check if given button is pressed
Code:
if(gamepad_button_check_pressed(current_device, global.controls[# 0, cursor_id])){
    // gp_face1 is pressed.
}
I don't know what the values of those constants are, but it is best to assume they may overlap with keyboard button constants and not mix them. So when you are checking keyboard bindings, do not check them against gamepad constants, and vice versa. Meaning, continuing the above example, as global.controls[# 0, cursor_id] now contains a reference to gamepad button gp_face1, you should NOT check it with keyboard_check(). It might give a false positive, it might not. Best to not test your luck.
Completed the script as suggested. I guess this topic is solved. For what it's worth I'll throw in a few more bits of information in case anyone is looking into this for help.

The keyboard keys and gamepad buttons don't overlap on GMS2. Keyboard keys are have id's that are 2 or 3 digits long and gampad buttons are 5 digits long. Overlap doesn't appear to be a factor based on my findings.

This is assuming that each PC doesn't have different keyboard key id's or controller inputs differ between PC's and the type's of controllers used. If anyone can add additional info regarding this that would help.


That said there are 16 (0-15) controller inputs (not counting the PS4 trackpad in this list).

gamepad id's:
gp_face1 = 32769
gp_face2 = 32770
gp_face3 = 32771
gp_face4 = 32772

Gamepad inputs go in this exact order (0-15):
1) dpad_up
2) dpad_down
3) dpad_left
4) dpad_right
5) start
6) select / options
7) L3
8) R3
9) L1 / LB
10) R1 / RB
11) L2 / LT
12) R2 / RT
13) A / X
14) B / O
15) X / Square
16) Y / Triangle

And here is the script I created using if else statements to get the desired function similar to keyboard_key.

Code:
///@description gamepad_key
///@function
///@arg device
///@arg button

var device = argument0; // Controller
var check = argument1; // Use a for loop to find the desired gamepad button
var button=undefined; // Holds the button on return

if (gamepad_is_connected(device))
{

var key; // 1D Array
key[0]=gp_padu;
key[1]=gp_padd;
key[2]=gp_padl;
key[3]=gp_padr;
key[4]=gp_start;
key[5]=gp_select;
key[6]=gp_stickl;
key[7]=gp_stickr;
key[8]=gp_shoulderl;
key[9]=gp_shoulderr;
key[10]=gp_shoulderlb;
key[11]=gp_shoulderrb;
key[12]=gp_face1;
key[13]=gp_face2;
key[14]=gp_face3;
key[15]=gp_face4;

button=key[check];

}
else
{
button = "ERROR: Controller Not Connected!";
}

return button
Edit: Updated the code.
 
Last edited:
Top