Handling both mouse and touch events in Forge viewer applications

To follow up on the last post – where we talked about adding a custom tool to provide better support for pinch gestures – today we’re going to talk about some other possibilities for supporting touch in the Forge viewer. Once again we’re using Autodesk.Viewing.ToolController & ToolInterface.

The Forge viewer uses Hammer.js internally to support touch, so that’s one good place to look for clues on how things work. (Groan – I’ve just realised why it’s called “Hammer”… “you CAN touch this!” 🙂

I was looking into how to support both “touch-capable” (i.e. they also have mouse & keyboard input) and “touch-only” (your typical phone or tablet) devices. Going from how Hammer.js works – and some info on Stack Overflow – I ended up creating these two utility functions in TypeScript:

 

export function isTouchDevice(): boolean {

  return ‘ontouchstart’ in window || !!navigator.maxTouchPoints;

}

 

export function isTouchOnlyDevice(): boolean {

  Read more