A way to detect "any" button pressed on the gamepad?

yuigoto

Member
Hi! My question is quite simple (well, I hope it is :p).

Is there a way to detect "any" button pressed on the gamepad? I mean, some sort of shortcut for this?

I'm building some sort of control mapping screen for a test project here, and I'm trying to do a "press any button on your gamepad to start mapping" sorta thing. Is it possible?

Thanks in advance! :D
 
W

whale_cancer

Guest
Not that I am aware of, I think you need to just check for every possible input sequentially.
 

yuigoto

Member
Thanks, I was reading GM docs and ended up with this conclusion too! :)

Thanks for taking the time to answer! ;)
 
M

montiedragon

Guest
I think each of the buttons are constants that represent numbers that happen to be sequential. If my guess is right, you could do this.
Code:
any_pressed = 0
for (i=gp_face1; i<gp_axisrv; i++){
    if gamepad_button_check(0, i){
        any_pressed = 1;
        exit;
        }
    }
Edit: grammar
 
Last edited by a moderator:

Yal

šŸ§ *penguin noises*
GMC Elder
I'd write a script that checks the gamepad's all buttons sequentially and returns the ID of the button that was pressed (if any, otherwise it returns something like noone or -1). When checking for 'any button', just check for a return value other than the "nothing pressed" one. And then you can use that script to make the actual mapping part as well. :)
 

yuigoto

Member
@montiedragon: that's a waaaay shorter than the script I built (I ended up with a chain of if/else if using all the constants :p)

@Yal: that's what I wanted to achieve! I ended up with a big chain of conditionals, but didn't realize I could do as @montiedragon said :)

So I ended up with:
Code:
for ( var i = gp_face1; i < gp_axisrv; i++ ) {
    if ( gamepad_button_check( 0, i ) ) return i;
}
return false;
Thank you all for the replies! :)
 

Slyddar

Member
Reviving this thread just to say the script above does not work for axis movement, so here's a better solution for anyone who stumbled here from google. It allows you to pass a pad number to check, so call it from a loop if you need to check multiple pad numbers.
GML:
function gamepad_check_input(_pad_num) {
    ///@desc    checks for gamepad input on the passed pad number
    ///@arg    pad_num    real    pad number to check
    
    for ( var i = gp_face1; i <= gp_padr; i++ ) {
        if ( gamepad_button_check( _pad_num, i ) ) return i;
    }
    for ( var i = gp_axislh; i <= gp_axisrv; i++ ) {
        if abs( gamepad_axis_value( _pad_num, i ) ) return i;
    }
}
 

kburkhart84

Firehammer Games
Reviving this thread just to say the script above does not work for axis movement, so here's a better solution for anyone who stumbled here from google. It allows you to pass a pad number to check, so call it from a loop if you need to check multiple pad numbers.
GML:
function gamepad_check_input(_pad_num) {
    ///@desc    checks for gamepad input on the passed pad number
    ///@arg    pad_num    real    pad number to check
   
    for ( var i = gp_face1; i <= gp_padr; i++ ) {
        if ( gamepad_button_check( _pad_num, i ) ) return i;
    }
    for ( var i = gp_axislh; i <= gp_axisrv; i++ ) {
        if abs( gamepad_axis_value( _pad_num, i ) ) return i;
    }
}
That works for your basic XInput styled gamepads....but it doesn't check older style direct input things fully, where there can be way more buttons than there are gp_ constants for, and 10 axes too, plus 4 POV hats. On Windows, I don't even use those constants for devices 4 and up(of course I use them for 0 - 3). So I sacrifice the possible good descriptions mapping knowing what is a face button, etc... but I support everything under the sun in exchange.
 
Top