Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Jul 9, 2024. It is now read-only.

Commit 02e3731

Browse filesBrowse files
Merge pull request #72 from patrickhlauke/touch-AND-mouse-note-issue71
note about avoiding conditional "touch OR mouse/keyboard" event handling
2 parents f33df73 + 183c313 commit 02e3731
Copy full SHA for 02e3731

File tree

Expand file treeCollapse file tree

1 file changed

+37
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+37
-0
lines changed

‎index.html

Copy file name to clipboardExpand all lines: index.html
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,41 @@ <h2>Interaction with Mouse Events and <code>click</code></h2>
960960
</ol>
961961
</section>
962962

963+
<section class="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+
<pre class="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+
<pre class="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+
...
963991

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 <a href="#dfn-canceled-event">cancel</a> the touch event, suppressing the generation of any further mouse or click events. Alternatively, see the <a href="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>
964998
</section>
965999

9661000
<section>
@@ -1116,6 +1150,9 @@ <h2>Changes Since Last Publication</h2>
11161150
<li>
11171151
<a href="https://github.com/w3c/touch-events/pull/67">Indicate that all events should be "composed".</a>
11181152
</li>
1153+
<li>
1154+
<a href="https://github.com/w3c/touch-events/pull/72">Note about avoiding conditional "touch OR mouse/keyboard" event handling</a>
1155+
</li>
11191156
</ul>
11201157
</section>
11211158

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.