0

Is it possible to neglect the key events in the Forge viewer, outside the viewer DOM element?

I have a separate DOM element with it's own key events, but they don't work, because the viewer events apply to the whole DOM (window). I can see that mouse events and key events are handled differently, but cannot understand why.

Lines 13015 - 13026 in viewer3D.js (v. 4.0):

// If we want to continue listenting to mouse movements outside of the window
  // we need to tie our event listener to the window

  this.domElement.addEventListener( 'mousewheel',     this.mousewheel, false );
  this.domElement.addEventListener( 'DOMMouseScroll', this.mousewheel, false ); // firefox

  //** this.domElement.addEventListener( 'touchstart', function( event ) { _this.touchstart( event )}, false );
  //** this.domElement.addEventListener( 'touchmove', function( event ) { _this.touchmove( event )}, false );

  window.addEventListener( 'keydown', this.keydown, false );
  window.addEventListener( 'keyup',   this.keyup,   false );
  window.addEventListener( 'blur', this.blur, false );

There is probably a good intension here, but it would be really nice if it could be changed. If there is a smooth workaround please tell me, but I am stuck.

torjuss
  • 11
  • 4
  • I believe you can still add other listeners, right? how does it affect your app? – Augusto Goncalves Mar 14 '18 at 21:32
  • Hi! Found out that the library that was supposed to handle key events in my other DOM element doesn't work properly. If I bind a 'keydown' event to that element myself the event is fired and works fine. However I still would like the viewer key events to only bind to the viewer DOM element, because I need to listen to arrow up/down and left/right. These conflict with the zoom/orbit controls in the viewer and it is annoying because I only want my own behaviour outside the viewer, and only the viewer behaviour inside the viewer. Thanks! – torjuss Mar 15 '18 at 08:36
  • the events should work in parallel. As the Viewer is not an input, it needs to track keys on the window level. – Augusto Goncalves Mar 15 '18 at 10:52
  • I can see the problem, but I found a simple solution to it in the post below. By setting tabindex on the viewer div it will work: https://stackoverflow.com/questions/3149362/capture-key-press-or-keydown-event-on-div-element – torjuss Mar 15 '18 at 15:35

1 Answers1

0

You should be able to register a custom tool with a high priority (eg. 1000) and absorb those events before all other viewer tools. Take a look at the changelog for more details about tool.getPriority:

// in your custom tool ...
this.handleKeyDown = function(event, keyCode) {

  return true; // absorbed event ...
};

this.getPriority = function() {
   return 1000; // Default is 0, 
   //higher numerical value results in higher priority.
};

Se also this article: Creating custom tool

Felipe
  • 4,325
  • 1
  • 14
  • 19