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
{{ message }}
This repository was archived by the owner on Jul 9, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: index.html
+37Lines changed: 37 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -960,7 +960,41 @@ <h2>Interaction with Mouse Events and <code>click</code></h2>
960
960
</ol>
961
961
</section>
962
962
963
+
<sectionclass="note">
964
+
<p>Even if a user agent supports Touch Events, this does not necessarily mean that a touchscreen is the only input mechanism available to users. Particularly in the case of touch-enabled laptops, or traditional "touch only" devices (such as phones and tablets) with paired external input devices, users may use the touchscreen in conjunction with a trackpad, mouse or keyboard. For this reason, developers should avoid binding event listeners with "either touch or mouse/keyboard" conditional code, as this results in sites/application that become touch-exclusive, preventing users from being able to use any other input mechanism.</p>
965
+
<preclass="example"><code>
966
+
// conditional "touch OR mouse/keyboard" event binding
967
+
// DON'T DO THIS, as it makes interactions touch-exclusive
968
+
// on devices that have both touch and mouse/keyboard
969
+
970
+
if ('ontouchstart' in window) {
971
+
// set up event listeners for touch
972
+
target.addEventListener('touchend', ...);
973
+
...
974
+
} else {
975
+
// set up event listeners for mouse/keyboard
976
+
target.addEventListener('click', ...);
977
+
...
978
+
}
979
+
</code></pre>
980
+
<p>Instead, developers should handle different forms of input concurrently.</p>
981
+
<preclass="example"><code>
982
+
// concurrent "touch AND mouse/keyboard" event binding
983
+
984
+
// set up event listeners for touch
985
+
target.addEventListener('touchend', function(e) {
986
+
// prevent compatibility mouse events and click
987
+
e.preventDefault();
988
+
...
989
+
});
990
+
...
963
991
992
+
// set up event listeners for mouse/keyboard
993
+
target.addEventListener('click', ...);
994
+
...
995
+
</code></pre>
996
+
<p>To avoid processing the same interaction twice for touch (once for the touch event, and once for the compatibility mouse events), developers should make sure to <ahref="#dfn-canceled-event">cancel</a> the touch event, suppressing the generation of any further mouse or click events. Alternatively, see the <ahref="http://wicg.github.io/InputDeviceCapabilities/">InputDeviceCapabilities API</a> for a way to detect mouse events that were generated as a result of touch events.</p>
997
+
</section>
964
998
</section>
965
999
966
1000
<section>
@@ -1116,6 +1150,9 @@ <h2>Changes Since Last Publication</h2>
1116
1150
<li>
1117
1151
<ahref="https://github.com/w3c/touch-events/pull/67">Indicate that all events should be "composed".</a>
1118
1152
</li>
1153
+
<li>
1154
+
<ahref="https://github.com/w3c/touch-events/pull/72">Note about avoiding conditional "touch OR mouse/keyboard" event handling</a>
0 commit comments