Skip to content

Commit

Permalink
Fix processing#6847: Update mouseButton value when one mouse button i…
Browse files Browse the repository at this point in the history
…s 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
1 parent 3754ce6 commit 09d66d2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
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

0 comments on commit 09d66d2

Please sign in to comment.