Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #6847: Update mouseButton value when one mouse button is released #6943

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/events/mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ p5.prototype.pwinMouseY = 0;
/**
* p5 automatically tracks if the mouse button is pressed and which
* button is pressed. The value of the system variable mouseButton is either
* LEFT, RIGHT, or CENTER depending on which button was pressed last.
* LEFT, RIGHT, or CENTER depending on which button was pressed/released last.
* Warning: different browsers may track mouseButton differently.
*
* @property {Constant} mouseButton
Expand Down Expand Up @@ -734,6 +734,7 @@ p5.prototype._onmouseup = function(e) {
const context = this._isGlobal ? window : this;
let executeDefault;
this._setProperty('mouseIsPressed', false);
this._setMouseButton(e);

// _ontouchend triggers first and sets this.touchend
if (this.touchend) {
Expand Down
8 changes: 8 additions & 0 deletions test/unit/events/mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,14 @@ suite('Mouse Events', function() {
assert.deepEqual(count, 1);
});

test('mouseButton should be "left" on left mouse button release', async function() {
// both mouse buttons pressed
window.dispatchEvent(new MouseEvent('mousedown', { button: 0 }));
window.dispatchEvent(new MouseEvent('mousedown', { button: 2 }));
window.dispatchEvent(new MouseEvent('mouseup', { button: 0 }));
assert.strictEqual(myp5.mouseButton, 'left');
});

test('mouseReleased functions on multiple instances must run once', async function() {
let sketchFn = function(sketch, resolve, reject) {
let count = 0;
Expand Down