From 09d66d2720473b3bd3fb4c07ec9f256682a27c69 Mon Sep 17 00:00:00 2001 From: Renato Marques Date: Fri, 5 Apr 2024 14:58:50 +0100 Subject: [PATCH] Fix #6847: Update mouseButton value when one mouse button is released - Modified src/events/mouse.js to correctly update the mouseButton value upon releasing one mouse button. - Updated documentation in src/events/mouse.js accordingly. - Added unit test in test/unit/events/mouse.js to cover scenario described in the issue. --- src/events/mouse.js | 3 ++- test/unit/events/mouse.js | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/events/mouse.js b/src/events/mouse.js index afb2ac619f..e69b84b0da 100644 --- a/src/events/mouse.js +++ b/src/events/mouse.js @@ -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 @@ -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) { diff --git a/test/unit/events/mouse.js b/test/unit/events/mouse.js index fd3003ce02..6a1340b982 100644 --- a/test/unit/events/mouse.js +++ b/test/unit/events/mouse.js @@ -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;