Skip to content
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
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.
  • Loading branch information
Renatolo committed Apr 5, 2024
commit 09d66d2720473b3bd3fb4c07ec9f256682a27c69
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