How to reset Diagram.lastInput?

Is it possible to either A) programmatically trigger a keydown input event or B) programmatically reset Diagram.lastInput?

I tried the following, and am seeing weird behavior where lastInput keeps going back and forth between the new value and its old value. For example, if I had pressed “F” prior to calling this code, lastInput.key will print out as “S” then “F” some seconds later then “S” again etc.

Diagram.lastInput = new go.InputEvent();
Diagram.lastInput.key = “S”;

The Robot extension shows how to do that.
See the example: Simulating Input Events
And see the keyDown and keyUp methods in https://gojs.net/latest/extensions/Robot.js

Is it possible to do this without using the Robot extension?

You can dispatch events directly to the Diagram’s canvas, though you’ll need to set Event.which instead of KeyboardEvent.key. So for “S” you’d need:

  const event = new Event("keydown");
  event.which = 83; // "S"
  event.key = 'S';
  myDiagram.div.firstChild.dispatchEvent(event);

Or for the delete key:

  const event = new Event("keydown");
  event.which = 46; // Del key
  event.key = 'Delete';
  myDiagram.div.firstChild.dispatchEvent(event);

We intend to switch to looking at KeyboardEvent.key in a future version though (GoJS preferred which only for compatibility for some time), so it’s best if you set both in your synthetic events.

I’m unfortunately still seeing the same behavior where lastInput.key keeps going back and forth between the new and old value. Any idea if I’m missing something? I’m overriding linkingTool.doActivate to include this logic and checking lastInput.key while in link drawing mode if you think that is impacting it?

Wait, what are you doing exactly? Or what’s the end goal? Maybe there’s an easier way to accomplish it.

I’m trying to update the temporary link styling when in drawing mode on keydown. For example, pressing “S” should change the fill to green, “E” to red, etc. The reason I’d like to reset lastInput when you begin link drawing mode is to reset the link color to its original color rather than the last one you selected via shortcut.

Thank you so much for your assist on this!

So while the LinkingTool is active (you’re clicking and holding down the pointer/mouse for the LinkingTool), you want to press a key, and you want that key press to change the color of the temporary link?

What you should probably do is override LinkingTool.doKeyDown, something like:

myDiagram.toolManager.linkingTool.doKeyDown = function () {
  const key = myDiagram.lastInput.key;
  if (key === 'S') {
    // change the temp link here
    // for example this would change it to red (permanently! until its changed back)
    myDiagram.toolManager.linkingTool.temporaryLink.elt(0).stroke = 'red';
  }
  go.LinkingTool.prototype.doKeyDown.call(this);
}

Yes, your understanding is correct. I didn’t know there was a doKeyDown method in LinkingTool, so I was able to use that combined with my existing logic to implement the mentioned shortcuts. Thank you again!