Skip to main content

Command Palette

Search for a command to run...

Detect if user is on touch screen

Published
1 min readView as Markdown
Detect if user is on touch screen
V

Engineer working in Cerebras. Latest work is Cerebras Inference. I love writing code in React, TypeScript, C++, Python. I spend most of my work hours tinkering with code.

Sometimes, it is useful to check if the user is on a touch screen. An example would be NOT to focus the textarea in touch screen. This is because the keyboard does not appear when user taps on the textarea.

Some code to detect touchscreen is shown below

const isTouchScreen =
      'ontouchstart' in window ||
      navigator.maxTouchPoints > 0 ||
      // For Internet Explorer
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      (navigator as any).msMaxTouchPoints > 0

PS: Code snippets for using them in the future.

More from this blog

V

Vijay Thirugnanam

38 posts

Short technical tips. Mostly stuff that I can refer to later.