Commands such as Delete or Paste or Undo are implemented by the CommandHandler class.
Keyboard events, like mouse and touch events, always go to the Diagram.currentTool. The current tool, when the user is not performing some gesture, is the same as the Diagram.defaultTool, which normally is the Diagram.toolManager. The ToolManager handles keyboard events by delegating them to the Diagram.commandHandler.
Basically, the diagram handles a keyboard event, creates an InputEvent describing it, and then calls ToolManager.doKeyDown. That in turn just calls CommandHandler.doKeyDown. The same sequence happens for key-up events.
Please note that the handling of keyboard commands depends on the diagram getting focus and then getting keyboard events. Do not apply any styling such as
canvas:focus { display: none; } /* DO NOT DO THIS! */
.
The CommandHandler implements the following command bindings for keyboard input:
On a Mac the Command key is used as the modifier instead of the Control key.
At the current time there are no keyboard bindings for commands such as CommandHandler.collapseSubGraph, CommandHandler.collapseTree, CommandHandler.expandSubGraph, or CommandHandler.expandTree.
If you want to have a different behavior for the arrow keys, consider using the sample class extended from CommandHandler: DrawCommandHandler, which implements options for having the arrow keys move the selection or change the selection.
That DrawCommandHandler extension also demonstrates a customization of the Copy and Paste commands to automatically shift the location of pasted copies.
The CommandHandler class implements pairs of methods: a method to execute a command and a predicate that is true when the command may be executed. For example, for the Copy command, there is a CommandHandler.copySelection method and a CommandHandler.canCopySelection method.
Keyboard event handling always calls the "can..." predicate first. Only if that returns true does it actually call the method to execute the command.
There are a number of properties that you can set to affect the CommandHandler's standard behavior. For example, if you want to allow the user to group selected parts together with the CommandHandler.groupSelection, you will need to set CommandHandler.archetypeGroupData to a group node data object:
diagram.commandHandler.archetypeGroupData =
{ key: "Group", isGroup: true, color: "blue" };
That data object is copied and added to the model as the new group data object by CommandHandler.groupSelection.
If you want to add your own keyboard bindings, you can override the CommandHandler.doKeyDown method. For example, to support using the "T" key to collapse or expand the currently selected Group:
myDiagram.commandHandler.doKeyDown = function() { // must be a function, not an arrow =>
const e = this.diagram.lastInput;
if (e.code === "KeyT") { // could also check for e.control or e.shift
if (this.canCollapseSubGraph()) {
this.collapseSubGraph();
} else if (this.canExpandSubGraph()) {
this.expandSubGraph();
}
} else {
// call base method with no arguments
go.CommandHandler.prototype.doKeyDown.call(this);
}
};
Do not forget to call the base method in order to handle all of the keys that your method does not handle.
Note that calling the base method involves getting the base class's prototype's method. If the base method takes arguments, be sure to pass arguments to the call to the base method.
It is common to have HTML elements outside of the diagram that invoke commands. You can use the CommandHandler's "can..." predicates to enable or disable UI that would invoke the command.
// allow the group command to execute
diagram.commandHandler.archetypeGroupData =
{ key: "Group", isGroup: true, color: "blue" };
// modify the default group template to allow ungrouping
diagram.groupTemplate.ungroupable = true;
const nodeDataArray = [
{ key: "Alpha" },
{ key: "Beta" },
{ key: "Delta", group: "Epsilon" },
{ key: "Gamma", group: "Epsilon" },
{ key: "Epsilon", isGroup: true }
];
const linkDataArray = [
{ from: "Alpha", to: "Beta" },
{ from: "Beta", to: "Beta" },
{ from: "Gamma", to: "Delta" },
{ from: "Delta", to: "Alpha" }
];
diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
// enable or disable a particular button
function enable(name, ok) {
const button = document.getElementById(name);
if (button) button.disabled = !ok;
}
// enable or disable all command buttons
function enableAll() {
const cmdhnd = diagram.commandHandler;
enable("SelectAll", cmdhnd.canSelectAll());
enable("Cut", cmdhnd.canCutSelection());
enable("Copy", cmdhnd.canCopySelection());
enable("Paste", cmdhnd.canPasteSelection());
enable("Delete", cmdhnd.canDeleteSelection());
enable("Group", cmdhnd.canGroupSelection());
enable("Ungroup", cmdhnd.canUngroupSelection());
enable("Undo", cmdhnd.canUndo());
enable("Redo", cmdhnd.canRedo());
}
// notice whenever the selection may have changed
diagram.addDiagramListener("ChangedSelection", e => enableAll());
// notice when the Paste command may need to be reenabled
diagram.addDiagramListener("ClipboardChanged", e => enableAll());
// notice whenever a transaction or undo/redo has occurred
diagram.addModelChangedListener(e => {
if (e.isTransactionFinished) enableAll();
});
// perform initial enablements after everything has settled down
setTimeout(enableAll, 100);
// make the diagram accessible to button onclick handlers
myDiagram = diagram;
// calls enableAll() due to Model Changed listener
myDiagram.undoManager.isEnabled = true;
Each button is implemented in the following fashion:
<input id="SelectAll" type="button"
onclick="myDiagram.commandHandler.selectAll()" value="Select All" />
Whenever the selection changes or whenever a transaction or undo or redo occurs, the enableAll function is called to update the "disabled" property of each of the buttons.
Since GoJS is based on the HTML Canvas element, making an app that is accessible to screen-readers or other accessibility devices is a matter of generating fallback content outside of GoJS, just as you would generate fallback content separate from any HTML Canvas application.
Although much of the predefined functionality of the CommandHandler is accessible with keyboard commands or the default context menu, not all of it is, and the functionality of the Tools mostly depends on mouse or touch events. We recommend that you implement alternative mechanisms specific to your application for those tools that you want your users to access without a pointing device.
Stop CTRL+Z/CTRL+Y from doing an undo/redo, but still allow CommandHandler.undo and CommandHandler.redo to be called programmatically:
myDiagram.commandHandler.doKeyDown = function() { // must be a function, not an arrow =>
const e = this.diagram.lastInput;
// The meta (Command) key substitutes for "control" for Mac commands
const control = e.control || e.meta;
const code = e.code;
const key = e.key.toLowerCase();
// Quit on any undo/redo key combination:
if (control && (code === 'KeyZ' || key === 'z' || code === 'KeyY' || key === 'y')) return;
// call base method with no arguments (default functionality)
go.CommandHandler.prototype.doKeyDown.call(this);
};