How to check for both keyboard and mouse?

Discussion in 'Programming' started by WolfHybrid23, Jan 3, 2018.

Tags:
  1. WolfHybrid23

    WolfHybrid23 Member

    Joined:
    Apr 8, 2017
    Posts:
    62
    Hello, I am making a game and I want the controls to be changeable by the user, I am not new to GameMaker: Studio, In fact on a scale from 1 to 10 I would say that I am around a 9, I need help with something I need to check for either keyboard or mouse input. The reason this is is because if the user wants to change a control to be a mouse button then I want to check for a mouse button rather than check for a keyboard button (and vice versa).
     
  2. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,870
    I'm not really sure what your asking. Do you just want something like:

    click = keyboard_check_pressed(vk_enter) || mouse_check_button_pressed(mb_left)?

    A lot depends upon how you've set other things up. For example, if it is a menu and you can use the keyboard or click on with the mouse then you might want to do something like this:

    select = keyboard_check_pressed(vk_enter) || (place_meeting(mouse_x, mouse_y, obj_menu_button) && mouse_check_button_pressed(mb_left));

    Or do you want to accept only keyboard or mouse but not both at the same time? Or are you asking how to 'rebind' controls?
     
  3. MIchael PS

    MIchael PS Member

    Joined:
    Aug 12, 2017
    Posts:
    88
    Check keyboard_set_map in gamemaker.
    It might help...
     
  4. WolfHybrid23

    WolfHybrid23 Member

    Joined:
    Apr 8, 2017
    Posts:
    62
    Well, Let me give you some background on this game, I am making it for desktop, it's a fan game for a DS game and I want the experience to be like a ds, so instead of doing normal controls I am basing all of the controls of the game on the original ds buttons here is what I am talking about:
    Code:
    enum buttons{
        a, b, x, y, left, right, up, down, left_trigger, right_trigger, start, select, volume_up, volume_down, screen_touch
    }
    globalvar Buttons;
    Buttons[buttons.a] = ord("D");
    Buttons[buttons.b] = ord("S");
    ///ect until here
    Buttons[buttons.screen_touch] = mb_any;
    
    But in case the user changes the screen_touch control to a keyboard key, How do I handle that, The documentation says nothing about the function throwing an error if the key is not a key with the function "keyboard_check_*" but I just want to be sure.
     
  5. WolfHybrid23

    WolfHybrid23 Member

    Joined:
    Apr 8, 2017
    Posts:
    62
    And I have am making a script that checks for the buttons.
    Code:
    /// @function button_check_pressed( Button )
    /// @argument Button - The button id to check for
    return keyboard_check_pressed( Buttons[argument0] );
    But I don't know how it would react to a mouse constant aside from a keyboard constant or character byte.
     
  6. WolfHybrid23

    WolfHybrid23 Member

    Joined:
    Apr 8, 2017
    Posts:
    62
    Maybe it would help if I understood what numerical value was assigned to these constants?
     
  7. WolfHybrid23

    WolfHybrid23 Member

    Joined:
    Apr 8, 2017
    Posts:
    62
    Ah, yes, Just debug messaged the constants values and I believe I have a solution to this:
    The unicode for "A" was 65, and for "Z" was 90 and the value for mb_left was 1 and mb_any was -1 so in theory if I check to see if the value in in those ranges and switch between mouse_check and keyboard_check.
     
  8. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,870
    You can assign numerical values to enums if you want. They "can only be real numbers or expressions with previous enums, and by default are numbered from 0 upwards." See the help document for examples. keyboard_check_pressed returns true or false. I believe ord("D") and the like return ascii values though I'm not positive. I don't know what mb_any or any of the other mb_left etc return. keyboard_check_pressed does not work with mb_* constants so your script will not work as it is.

    However, you could fairly easily modify your script as noted in my initial reply to work. I believe even something like:

    Code:
    
    /// @function button_check_pressed( Button )
    /// @argument Button - The button id to check for
    return keyboard_check_pressed( Buttons[argument0] ) || (place_meeting(mouse_x, mouse_y, obj_menu_button) && mouse_check_button_pressed(mb_left));
    
    
    Would work assuming that the game was set up so that place meeting would work. You can easily use the idea there to modify it however you like.
     
  9. WolfHybrid23

    WolfHybrid23 Member

    Joined:
    Apr 8, 2017
    Posts:
    62
    Did you even read my previous post, I was using enumerators. Or did you just read it and tell me what I already did to try and make others think I'm dumb or something?
     
  10. WolfHybrid23

    WolfHybrid23 Member

    Joined:
    Apr 8, 2017
    Posts:
    62
    Whatever it doesn't matter I fixed the issue already.
     
  11. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,870
    I apologize. I hadn't read the two posts previous to my last post as I was typing that post at the time. I'm glad you solved it.
     
  12. WolfHybrid23

    WolfHybrid23 Member

    Joined:
    Apr 8, 2017
    Posts:
    62
    I can tell you how I solved it. So the keyboard input beings at unicode 32 while mouse input begins at -1 and ends at 2 so I checked if the value was below 32 and if so made the game check for mouse input rather than keyboard input and if not it checks for keyboard input
    Code:
    /// @function button_check_pressed( Button )
    /// @argument Button - The button to check for (Use the enumerator buttons to reference the button)
    if(Buttons[argument0] < 32)return mouse_check_button_pressed( Buttons[argument0] );
    else return keyboard_check_pressed( Buttons[argument0] );
    
     
    samspade likes this.
  13. dirkinz

    dirkinz Member

    Joined:
    Jul 14, 2016
    Posts:
    21
    Not to throw a wrench in your plans but I was trying to figure out the same issue and as it turns out some of the keyboard constants do go below 32.
    vk_alt => 18
    vk_anykey => 1
    vk_backspace => 8
    vk_control => 17
    vk_enter => 13
    vk_escape => 27
    vk_nokey => 0
    vk_pause => 19
    vk_return => 13
    vk_shift => 16
    vk_space => 32
    vk_tab => 9

    This means that:
    Code:
    mouse_check_button(vk_anykey ) // returns true if the left mouse button is pressed - vk_anykey = 1 = mb_left
    mouse_check_button(vk_nokey) // returns true if no mouse buttons are being pressed - vk_nokey = 0 = mb_none
    
    Also:
    Code:
    keyboard_check(mb_none) // returns true if no key is being pressed - mb_none = 0 = vk_nokey
    
    however
    Code:
    keyboard_check_direct(mb_none) // never returns true as no individual key has a id of 0
    
    With all that said, your logic should hold true with a couple modifications.
    1. Rather then checking if(Buttons[argument0] < 32) you should check if(Buttons[argument0] <= 3) as the highest mouse button is id:3.
    2. Additionally, this code snippet can produce undesired results if its ever passed a key grouping (ie. vk_anykey or mb_none) so only use it if you don't need to use those groups
     

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