javascript - Calling the toolController.deactiveTool method in a forge viewer custom tool is not triggering on windows while it

admin2025-04-18  2

I am trying to create a custom forge viewer tool that only enables the box-selection extension while the shift key is pressed down. This works as expected on Mac devices but on Windows it enables the box-selection but never deactivates the box-selection once the shift key is released.

Both the handleKeyDown and handleKeyUp event handlers are triggered as expected on both platforms. So it seems to me an internal issue with the deactiveTool function. I tried debugging my way through this and I see there is some Mac specific logic in the functions but the root cause of the issue is beyond me.

Here is my custom box-selection tool.

/**
 * Custom forge viewer tool to restrict box-selection to the shift key
 * @param viewer Reference to the forge viewer instance
 * @see {@link  | Forge Documentation}
 */
export const getCustomBoxSelectionTool = (viewer) => {
  if (!viewer?.toolController) return null;

  const customTool = {
    active: false,
    getName: () => 'custom-box-selection',
    getNames: () => ['custom-box-selection'],
    getPriority: () => 1, // Set priority in the tool chain
    activate: () => {
      customTool.active = true;
    },
    deactivate: () => {
      customTool.active = false;
    },
    handleKeyDown: (event, keyCode) => {
      // Prevent the default box-selection behaviour
      if (event.key === 'Control' || keyCode === 17) {
        viewer.toolController.deactivateTool('box-selection');
        return true; // Stop propagation
      }

      // Enable new box-selection behaviour
      if (event.key === 'Shift' || keyCode === 16) {
        viewer.toolController.activateTool('box-selection');
      }

      return false; // Allow propagation
    },
    handleKeyUp: (event, keyCode) => {
      // Disable box-selection after shift is released
      if (event.key === 'Shift' || keyCode === 17) {
        viewer.toolController.deactivateTool('box-selection');
      }

      return false; // Allow propagation
    }
  };

  return customTool;
};

Note I added checks for the event.key and keyCode as that was a suggested difference between the two platforms but this has made no difference. Does anyone have any insights to this?

I am trying to create a custom forge viewer tool that only enables the box-selection extension while the shift key is pressed down. This works as expected on Mac devices but on Windows it enables the box-selection but never deactivates the box-selection once the shift key is released.

Both the handleKeyDown and handleKeyUp event handlers are triggered as expected on both platforms. So it seems to me an internal issue with the deactiveTool function. I tried debugging my way through this and I see there is some Mac specific logic in the functions but the root cause of the issue is beyond me.

Here is my custom box-selection tool.

/**
 * Custom forge viewer tool to restrict box-selection to the shift key
 * @param viewer Reference to the forge viewer instance
 * @see {@link https://aps.autodesk.com/blog/custom-tools-forge-viewer | Forge Documentation}
 */
export const getCustomBoxSelectionTool = (viewer) => {
  if (!viewer?.toolController) return null;

  const customTool = {
    active: false,
    getName: () => 'custom-box-selection',
    getNames: () => ['custom-box-selection'],
    getPriority: () => 1, // Set priority in the tool chain
    activate: () => {
      customTool.active = true;
    },
    deactivate: () => {
      customTool.active = false;
    },
    handleKeyDown: (event, keyCode) => {
      // Prevent the default box-selection behaviour
      if (event.key === 'Control' || keyCode === 17) {
        viewer.toolController.deactivateTool('box-selection');
        return true; // Stop propagation
      }

      // Enable new box-selection behaviour
      if (event.key === 'Shift' || keyCode === 16) {
        viewer.toolController.activateTool('box-selection');
      }

      return false; // Allow propagation
    },
    handleKeyUp: (event, keyCode) => {
      // Disable box-selection after shift is released
      if (event.key === 'Shift' || keyCode === 17) {
        viewer.toolController.deactivateTool('box-selection');
      }

      return false; // Allow propagation
    }
  };

  return customTool;
};

Note I added checks for the event.key and keyCode as that was a suggested difference between the two platforms but this has made no difference. Does anyone have any insights to this?

Share Improve this question asked Jan 29 at 14:52 Michael RaynerMichael Rayner 12 bronze badges 7
  • I tried to reproduce it with this sample on Windows 10 with both Chrome and Edge and it seemed to work as expected: github.com/adamenagy/Container/blob/master/… See video: youtu.be/BZJlZwq0oDs – Adam Nagy Commented Mar 4 at 8:27
  • Thanks @AdamNagy Bizarrely still, I ran your code on my Windows 11 computer. With Chrome and Edge and I still get the issue. Exact same code. It still works as expected on my Macbook. I also made this codepen yesterday and tried it on the windows machines of a few colleagues and got the same issue. The only link so far is the different Windows versions? – Michael Rayner Commented Mar 5 at 13:18
  • For me the codepen project does not work, maybe that code tries to activate your tool too early? adamenagy.com/Container/shared-model-box-selection.html works fine (you can box select with Shift once your tool is activated) on my Windows 11 machine on Parallels - but the cursor never changes
转载请注明原文地址:http://anycun.com/QandA/1744958854a90041.html