You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/3-event-details/1-mouse-events-basics/article.md
+27-11
Original file line number
Diff line number
Diff line change
@@ -41,26 +41,42 @@ Click the button below and you'll see the events. Try double-click too.
41
41
42
42
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.
43
43
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.
45
45
46
46
<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>
47
47
```
48
48
49
-
## Mouse button: "which"
49
+
## Mouse button
50
50
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.
52
52
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.
54
54
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".
56
56
57
-
There are the three possible values:
57
+
The possible values of `event.button` are:
58
58
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 |
62
66
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
+
```
64
80
65
81
## Modifiers: shift, alt, ctrl and meta
66
82
@@ -183,7 +199,7 @@ Surely the user has access to HTML-source of the page, and can take the content
183
199
184
200
Mouse events have the following properties:
185
201
186
-
- Button: `which`.
202
+
- Button: `button`.
187
203
- Modifier keys (`true` if pressed): `altKey`, `ctrlKey`, `shiftKey` and `metaKey` (Mac).
188
204
- 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)`.
0 commit comments