mouse_button reports 0 when mouse is pressed?

bsabiston

Member
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:
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)
 

bsabiston

Member
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.
 

Tony Brice

Member
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.
 

bsabiston

Member
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.
 
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.
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?
 

bsabiston

Member
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
 
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.
 

rIKmAN

Member
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.
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.
 

TsukaYuriko

☄️
Forum Staff
Moderator
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.
 
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.
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 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.
 

bsabiston

Member
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.
 

bsabiston

Member
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:

TheouAegis

Member
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.
 
D

dualface

Guest
I have same problem.

my code:

show_debug_message(mouse_button);

----

When I press mouse left button and hold on, mouse_button should be 1.

My code working on Windows, but not working on MacOS. mouse_button alwasy is 0 on MacOS.
 
H

Homunculus

Guest
When I press mouse left button and hold on, mouse_button should be 1.
I can confirm this, mouse_lastbutton seems affected as well. Runtime v2.2.4.374 on MacOS Catalina. It's already in the bug tracker though #0031088

Meanwhile, you should be able to find a workaround using the other mouse functions, those at least seem to work properly.
 
Top