mouse_button reports 0 when mouse is pressed?

Discussion in 'Programming' started by bsabiston, Jun 25, 2019.

  1. bsabiston

    bsabiston Member

    Joined:
    Jun 21, 2016
    Posts:
    207
    I'm trying to use the mouse_button constant to detect when the mouse is pressed, but it's reporting 0 when my left mouse button is definitely pressed down. It's supposed to always report which button is pressed, isn't it?

    I have walk direction buttons which react to Mouse Down. Sometimes, when the player walks into a door, I change rooms. In that case, I want to wait until the mouse is released, so that the player does not immediately continue walking when they enter the new room.

    I believe this used to work. But now, I'm getting the mouse down events passed to the buttons, but in my script where I wait for the release, 'mouse_button' reports 0. Does this 'mouse_button' get reset to 0 sometimes even when the mouse is still pressed down???

    EDIT: I also tried the 'mouse_clear' function to reset the mouse so that it would not keep reporting mousedown. But that doesn't appear to work either?
     
    Last edited: Jun 25, 2019
  2. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,220
    Post your code.

    If you are not already doing so, use the mouse constants (mb_left, mb_right, mb_middle, mb_none) instead of checking for 0.

    Alternatively use the function mouse_check_button_released(mb_left)
     
  3. The Reverend

    The Reverend Member

    Joined:
    Sep 8, 2016
    Posts:
    555
  4. bsabiston

    bsabiston Member

    Joined:
    Jun 21, 2016
    Posts:
    207
    It doesn’t work for me on Mac. It doesn’t even work if I open a new project, create an empty object, and print the value of mouse_button in the object’s Step. It always returns 0.

    I’d be interested to know if someone else with a Mac could test this. I already reported a bug to YoYo with that sample project included.
     
  5. Tony Brice

    Tony Brice Member

    Joined:
    Feb 4, 2017
    Posts:
    67
    You need to read what the previous posts have said.

    Code:
    
    if (mouse_check_button(mb_left) || mouse_check_button(mb_right)) {
      // Do whatever...
    }
    
    
    I use this code all the time on my mac and it works just fine. It will check if the left or right mouse button has been used.
     
  6. bsabiston

    bsabiston Member

    Joined:
    Jun 21, 2016
    Posts:
    207
    mouse_check_button resets itself after reporting a button down. It can’t be used to poll the mouse for when the button is released.
     
  7. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,220
    It doesn't reset itself, so much as it is polling the mouse button every step though. If the mouse button is released, the next step, mouse_check_button will report false.

    If you want to know in the same step, use mouse_check_button_released()

    mouse_button is also updated every step, so there is little difference between them.

    What code have you tried so far?
     
  8. bsabiston

    bsabiston Member

    Joined:
    Jun 21, 2016
    Posts:
    207
    Ah I was looking at check_mouse_button_pressed(). I will try check_mouse_button(). Still I wonder why the mouse_button constant does not work...

    Thanks
     
  9. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,220
    Just to add some more information, I tested on GMS 2.2.3.425 Beta:

    Value of mouse_button:

    No buttons pressed : 0
    Left mouse button held down : 1
    Right mouse button held down : 2
    Middle mouse button held down : 3

    If I let go of the mouse button, the value returns to 0.

    If I am holding down the left mouse button(and getting a value of 1) and then at the same time (still holding down left mouse button) I also hold down the right mouse button, the value changes to 2. If I then release the right mouse button ( still keeping the left mouse button pressed ) the value then changes to 0.

    This was the only case where I could find that the value returned by mouse_button returned to 0 when a mouse button was still actually held down.

    Otherwise it seems to work as indicated in the manual.
     
  10. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,531
    It's a bit of an edge case, but I'd report that as a bug.

    For example a game where you hold the LMB for a character to move towards the cursor with firing a weapon mapped to the RMB.
    Whilst moving, if you fired your weapon your player would also stop moving.

    Although in saying that I've never used the mouse_button variable directly and have always used the mouse_check_* functions so it could be worked around.
     
  11. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,639
    mouse_button is analogous to keyboard_key, both in name and spirit, as they both return the key/button code of the last key/button that was pressed and is still being held down. If it's released, it resets to 0. You could use this to let players enter their name without having to check for key presses of all letters and special characters sequentially without having to mess with post-filtering out unwanted characters from keyboard_string, for example. They both work the way @IndianaBones described.

    There are also the variants keyboard_lastkey and mouse_lastbutton which do not reset themselves back to 0 after the last pressed key/button has been released.

    I'd be more inclined to call this a case where mouse_button is not the optimal solution to the problem rather than a bug, at least for the replies to this topic. @bsabiston's case sounds like an actual bug nonetheless.
     
    rIKmAN likes this.
  12. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,220
    @rIKmAN Yeah, I'd go with the mouse_check_* functions as well, and given the explanation from @TsukaYuriko, I'd probably leave the mouse_button as is - it can only report a single button at a time, so my edge case wouldn't apply, it shouldn't be used to try and detect multiple buttons in the first place.
     
    rIKmAN likes this.
  13. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,531
    Yeah I agree, as I said I've never used that variable directly since I started using GMS - there is no need to with the available mouse functions.
     
    IndianaBones likes this.
  14. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,220
    Same, I had honestly never been aware of mouse_button variable until this post.
     
    rIKmAN likes this.
  15. bsabiston

    bsabiston Member

    Joined:
    Jun 21, 2016
    Posts:
    207
    I am going to try check_mouse_button, but as for mouse_button, it never reports anything other than 0 for me. I already heard back from YoYo (surprise!) and they verified that it does not work (on Mac at least) and entered it into the database.
     
  16. bsabiston

    bsabiston Member

    Joined:
    Jun 21, 2016
    Posts:
    207
    Unfortunately, mouse_check_button(mb_left) also always reports 0 on the Mac. That's a pretty basic thing to not be working!

    I guess I'll make do with mouse_check_button_pressed and released. Those do work at least.
     
    Last edited: Jun 26, 2019
  17. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,906
    What about this code (it's a debug code, so you'll need the console visible to see the debug messages):
    Code:
    for(var d=0; d<4; d++)   //If 4 is too high (causes errors), use a smaller value
    for(var b=0; b<3; b++)    //Shouldn't be any issues with 3 buttons)
        if device_mouse_check_button(d,b) 
            show_debug_message("Device # "+string(d)+" button "+string(b));
    
    See if you can get any of the buttons to register a debug message with that code. If so, let us know.
     
  18. bsabiston

    bsabiston Member

    Joined:
    Jun 21, 2016
    Posts:
    207
    I only have one device, the mouse, but yes that works!
     

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