Skip to content

Commit 5d3aa7f

Browse files
committed
event.button
1 parent 6e1e244 commit 5d3aa7f

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

Diff for: 2-ui/3-event-details/1-mouse-events-basics/article.md

+27-11
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,42 @@ Click the button below and you'll see the events. Try double-click too.
4141
4242
On the teststand below all mouse events are logged, and if there is more than a 1 second delay between them they are separated by a horizontal ruler.
4343
44-
Also we can see the `which` property that allows to detect the mouse button.
44+
Also we can see the `button` property that allows to detect the mouse button, it's explained below.
4545
4646
<input onmousedown="return logMouse(event)" onmouseup="return logMouse(event)" onclick="return logMouse(event)" oncontextmenu="return logMouse(event)" ondblclick="return logMouse(event)" value="Click me with the right or the left mouse button" type="button"> <input onclick="logClear('test')" value="Clear" type="button"> <form id="testform" name="testform"> <textarea style="font-size:12px;height:150px;width:360px;"></textarea></form>
4747
```
4848

49-
## Mouse button: "which"
49+
## Mouse button
5050

51-
Click-related events always have the `which` property, which allows to get the exact mouse button.
51+
Click-related events always have the `button` property, which allows to get the exact mouse button.
5252

53-
It is not used for `click` and `contextmenu` events, because the former happens only on left-click, and the latter -- only on right-click.
53+
We usually don't use it for `click` and `contextmenu` events, because the former happens only on left-click, and the latter -- only on right-click.
5454

55-
But if we track `mousedown` and `mouseup`, then we need it, because these events trigger on any button, so `which` allows to distinguish between "right-mousedown" and "left-mousedown".
55+
From the other hand, `mousedown` and `mouseup` handlers we may need `event.button`, because these events trigger on any button, so `button` allows to distinguish between "right-mousedown" and "left-mousedown".
5656

57-
There are the three possible values:
57+
The possible values of `event.button` are:
5858

59-
- `event.which == 1` -- the left button
60-
- `event.which == 2` -- the middle button
61-
- `event.which == 3` -- the right button
59+
| Button state | `event.button` |
60+
|--------------|----------------|
61+
| Left button (primary) | 0 |
62+
| Middle button (auxillary) | 1 |
63+
| Right button (secondary) | 2 |
64+
| X1 button (back) | 3 |
65+
| X2 button (forward) | 4 |
6266

63-
The middle button is somewhat exotic right now and is very rarely used.
67+
Most mouse devices only have the left and right buttons, so possible values are `0` or `2`. Touch devices also generate similar events when one taps on them.
68+
69+
Also there's `event.buttons` property that has all currently pressed buttons as an integer, one bit per button. In practice this property is very rarely used, you can find details at [MDN](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons) if you ever need it.
70+
71+
```warn header="The outdated `event.which`"
72+
Old code may use `event.which` property that's an old non-standard way of getting a button, with possible values:
73+
74+
- `event.which == 1` – left button,
75+
- `event.which == 2` – middle button,
76+
- `event.which == 3` – right button.
77+
78+
As of now, `event.which` is deprecated, we shouldn't use it.
79+
```
6480
6581
## Modifiers: shift, alt, ctrl and meta
6682
@@ -183,7 +199,7 @@ Surely the user has access to HTML-source of the page, and can take the content
183199

184200
Mouse events have the following properties:
185201

186-
- Button: `which`.
202+
- Button: `button`.
187203
- Modifier keys (`true` if pressed): `altKey`, `ctrlKey`, `shiftKey` and `metaKey` (Mac).
188204
- If you want to handle `key:Ctrl`, then don't forget Mac users, they usually use `key:Cmd`, so it's better to check `if (e.metaKey || e.ctrlKey)`.
189205

Diff for: 2-ui/3-event-details/1-mouse-events-basics/head.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
function logMouse(e) {
2626
let evt = e.type;
2727
while (evt.length < 11) evt += ' ';
28-
showmesg(evt + " which=" + e.which, 'test')
28+
showmesg(evt + " button=" + e.button, 'test')
2929
return false;
3030
}
3131

0 commit comments

Comments
 (0)