Gamepad is recognized as a keyboard stroke?

Discussion in 'Programming' started by Shy Lev-Ari, Dec 29, 2018.

  1. Shy Lev-Ari

    Shy Lev-Ari Member

    Joined:
    Dec 29, 2018
    Posts:
    13
    So I want my game to be able to detect a gamepad, and switch the graphic from keyboard hnts to gamepad hints, and vice versa.

    I have this code:

    Code:
    for ( var i = gp_face1; i < gp_axisrv; i++ )
    {
        if ( gamepad_button_check( 0, i ) )
        {
            global.keyboard = false;
        }
    }
    
    if    (keyboard_check(vk_anykey))
    {
        global.keyboard = true;
    }
    When I test it, the first top works. global.keyboard is set to true by default, and after pressing a gamepad btn it is set to false.
    When I add the second part (if... anykey) then the global.keyboard is always true. Does GMS detect the gamepad press as "vk_anykey"?

    How else would you guys write this code?
     
  2. Shy Lev-Ari

    Shy Lev-Ari Member

    Joined:
    Dec 29, 2018
    Posts:
    13
    Can anybody help with this? It is driving me mad
    I have also tried this:

    Code:
    if (global.keyboard)
    {
        for ( var i = gp_face1; i < gp_axisrv; i++ )
        {
            if ( gamepad_button_check_pressed( 0, i ) )
            {
                global.keyboard = false;
            }
        }
    }
    
    
    if (!global.keyboard)
    {
        if ( keyboard_check_pressed(vk_anykey))
        {
            global.keyboard = true;
        }
    }
    but it seems that when I use the left analog stick, it is recognized as a keyboard stroke :(
     
  3. RangerX

    RangerX Member

    Joined:
    Jun 20, 2016
    Posts:
    2,583
    Ok its not really a solution for you maybe but I cut this problem really short in my game The Life Ruby.
    When a gamepad is detected by the asynchronous event meant for it, I put a variable to true and when the gamepad is removed, I put it to false.
    Thing is, if the player is having a gamepad plugged in its because he'll play your game with it. I stopped my thinking right there.
     
  4. BattleRifle BR55

    BattleRifle BR55 Member

    Joined:
    Jun 24, 2016
    Posts:
    869
    You don't happen to have XPadder or anything like it accidentally running in the background, do you?
     
  5. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    985
    Try reversing the order:
    Code:
    if    (keyboard_check(vk_anykey))
    {
       global.keyboard = true;
    }
    for ( var i = gp_face1; i < gp_axisrv; i++ )
    {
       if ( gamepad_button_check( 0, i ) )
       {
           global.keyboard = false;
       }
    }
    If anykey is returning true when the gamepad is used, this should set it to false right after, meaning that if the user presses an actual key it will be set to true (as the gamepad code won't run), but if the user uses the gamepad, it'll be set to true then false. Kinda hacky but it should work.
     
  6. Shy Lev-Ari

    Shy Lev-Ari Member

    Joined:
    Dec 29, 2018
    Posts:
    13
    thanks for he suggestion. my game is small, kinda my first really. i will not publish i or anything, so this is just me trying to solve a puzzle :) a learning excersize, if you will
     
  7. Shy Lev-Ari

    Shy Lev-Ari Member

    Joined:
    Dec 29, 2018
    Posts:
    13
    nope, not to my knoledge. it is an xbox360 gamepad. I did notice, however, that when the gamepad is turned on, the my cursor starts to drift towards the bottom left corner of the screen (if that matters).
     
    Gamebot likes this.
  8. Shy Lev-Ari

    Shy Lev-Ari Member

    Joined:
    Dec 29, 2018
    Posts:
    13
    Thanks for the attempt. The result is the same. I fire with the gamepad. The reload prompt I set up shows up. The proper asset shows up (the gamepad's A button). BUT if I use the left analog stick, the second asset (Keyboard's Ctrl key) will show up. Just to be clear, this is the code to show which asset:
    Code:
    if (!global.keyboard) draw_sprite_ext(spr_gamepad_a,0,room_width/2,room_height/2-(room_height*8/100),image_xscale,image_yscale,0,c_white,tempAlpha);
    if (global.keyboard) draw_sprite_ext (spr_ctrl,0,room_width/2,room_height/2-(room_height*8/100),image_xscale,image_yscale,0,c_white,tempAlpha); 
    It's on the draw event, nothing fancy, very straightforward. And because of this I have to assume that some of the gamepad's keys are read as keyboard keys.
    To stress this even further, my movement input has only been configured for keyboard vk_left / vk_right, and NOT for gamepad, and the gamepad sort of just "works". I want to say that THAT might be the problem, but the "Ctrl" asset also shows up when I tap left bumper, for example, and other random keys. Seems like only keys I defined (A,B and right trigger) DO NOT cause the system to think i is a keyboard (and right analog stick, for some reason. I have a feeling the right analog stick is simulating a mouse cursor and tht's why the system doesn't think it is a keyboard).

    BTW, this is the game:
    https://gamejolt.com/games/skyinvaders/332074

    I can't publish it because I don't own any of the music :) (assets are all me though).
     
  9. Gamebot

    Gamebot Member

    Joined:
    Jul 3, 2016
    Posts:
    162
    It moves the cursor most likely because you didnt set the deadzone correctly for the controller. Controllers are sensative and NOT perfectly straight. Therefore, your computer thinks your moving the right or left axis even though your not. I remember not setting it once...oops off the screen it went.

    Try using: gamepad_set_deadzone in the create event.
     
  10. Shy Lev-Ari

    Shy Lev-Ari Member

    Joined:
    Dec 29, 2018
    Posts:
    13
    yeah, no, regardless of Gamemaker. Turn on the gamepad - windows cursor hovers off
     
  11. BattleRifle BR55

    BattleRifle BR55 Member

    Joined:
    Jun 24, 2016
    Posts:
    869
    The windows cursor moves when it's plugged in? Make sure to check for any accessibility programs, too. By default, a plugged-in controller shouldn't have any effect.
     
  12. Gamebot

    Gamebot Member

    Joined:
    Jul 3, 2016
    Posts:
    162
    Oh that. Mine too. Happened after two updates ago. Stupid...

    Thats why I dont plug them in until I am already running a seperate game or gm game. Seems to work that way.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice